「java生成椭圆」java类和对象之构造方法与对象创建之求椭圆面积

博主:adminadmin 2022-12-21 18:21:09 72

本篇文章给大家谈谈java生成椭圆,以及java类和对象之构造方法与对象创建之求椭圆面积对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java或js实现动态生成椭圆电子章图片(非窗体程序)

现成写好的印章生成小工具源码,还支持椭圆、私章等。直通车:

java问题:用drawOval方法或fillOval方法绘制椭圆怎么会不成功的?

方法一:

将paintComponent()改为paint()就好了。

方法二:

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Color;

import java.awt.Graphics;

改为

import javax.swing.*;

import java.awt.*;

你的程序有一个错误,你在一个程序中定义了两个公共类,应将后一个

public class OvalsPanel extends JPanel 的public 去掉。

java 椭圆算法

以下代码,将输出一个椭圆,再有问题,我可远程助你。如下:

/**

 *(300,100)(400,100)

 *

*/

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Lipse 

{

 public static void main(String[] args)

 {

  new MainFrame();

 }

}

class MainFrame extends JFrame implements ActionListener

{

 JPanel pane=new JPanel();

 JTextField T_a,T_b;

 JButton Draw,Show;

 JLabel L_a,L_b;

 int a,b;

 MainFrame()

 {

  super("DrawLipse Window");

  Container con=this.getContentPane();

  con.setLayout(null);

  

  pane.setBounds(20,20,850,550);

  pane.setBackground(new Color(100,156,200));

  con.add(pane);

  

  L_a=new JLabel("请输入长半径:a");

  L_a.setBounds(180,580,100,20);

  con.add(L_a);

  

  L_b=new JLabel("请输入短半径:b");

  L_b.setBounds(180,630,100,20);

  con.add(L_b);

  

  

  T_a=new JTextField();

  T_a.setBounds(300,580,50,20);

  con.add(T_a);

  

  T_b=new JTextField();

  T_b.setBounds(300,630,50,20);

  con.add(T_b);

  

  Draw=new JButton("画椭圆");

  Draw.setBounds(550,580,90,30);

  Draw.addActionListener(this);

  con.add(Draw);

  

  Show=new JButton("显示坐标");

  Show.setBounds(550,620,90,30);

  Show.addActionListener(this);

  con.add(Show);

  

  this.addWindowListener(new CloseWindow());

  this.setBounds(20,20,900,700);

  this.setVisible(true);

  this.setResizable(false);

  

 }/*MainFrame()*/

 public void actionPerformed(ActionEvent e)

 {

     if(e.getSource()==Draw)

     {

      a=Integer.parseInt(T_a.getText().trim());

      b=Integer.parseInt(T_b.getText().trim());

      Line line=new Line(this);

      line.drawLipse(a,b); 

     }

     if(e.getSource()==Show)

     {

       Graphics g1=this.pane.getGraphics();

          g1.setColor(Color.PINK);

          g1.drawLine(0,300,920,300);//----x---

          g1.drawLine(410,0,410,720);//----y---

          g1.dispose();

     }

  

 }/*method actionPerformed*/

}

class Line

{

 MainFrame jb;

 Line(MainFrame jb)

 {

  this.jb=jb;

 }

 public void drawLipse(int a,int b)

 {

  int x,y;

  double d1,d2;

  x=0;  y=b;

  d1=b*b+a*a*(-b+0.25);

  Graphics g=jb.pane.getGraphics();

  g.setColor(Color.red);

  g.drawLine(x+410,y+300,x+410,y+300);

  g.drawLine(-x+410,-y+300,-x+410,-y+300);

  g.drawLine(-x+410,y+300,x+410,-y+300);

  g.drawLine(x+410,-y+300,x+410,-y+300);

  try

  {

   while(b*b*(x+1)a*a*(y-0.5))

               {

            if(d1=0)

              {

              d1+=b*b*(2*x+3);

              x++;

              }

              else

              {

              d1+=(b*b*(2*x+3)+a*a*(-2*y+2));

              x++;

              y--;

              }

              g.drawLine(x+410,y+300,x+410,y+300);

              g.drawLine(-x+410,-y+300,-x+410,-y+300);

              g.drawLine(-x+410,y+300,x+410,-y+300);

              g.drawLine(x+410,-y+300,x+410,-y+300);

              Thread.sleep(30);

         }// top of while 

     }catch(Exception e){}

     

  d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;

  try

  {

   while(y0)

      {

     if(d2=0)

       {

       d2+=b*b*(2*x+2)+a*a*(-2*y+3);

       x++;

          y--;

        }

        else

        {

        d2+=a*a*(-2*y+3);

        y--;

        }

        g.drawLine(x+410,y+300,x+410,y+300);

           g.drawLine(-x+410,-y+300,-x+410,-y+300);

           g.drawLine(-x+410,y+300,x+410,-y+300);

           g.drawLine(x+410,-y+300,x+410,-y+300);

           Thread.sleep(30);

        }/* bottom of while*/

   

     }catch(Exception e){}

  

 } /*DrawLipse*/ 

  

}

class CloseWindow extends WindowAdapter

{

 public void windowClosing(WindowEvent e)

 {

  System.exit(0);

 }

}

java椭圆按钮怎么设置,最好举一个简单的例子,只要有窗体中一个椭圆按钮就可以,最好有注释,谢谢,好的

椭圆按钮要重新绘制按钮才行,对你来说可能会比较难。

import javax.swing.ImageIcon;

import javax.swing.JButton;

public class EllipseButton extends JButton {

static final long serialVersionUID=80;

public EllipseButton(String text){

super(text);

// 创建自己的UI

this.setUI(EllipseButtonUI.createUI(this));

// 取消按钮的边框

this.setBorder(null);

// 不用内容窗格的背景

this.setContentAreaFilled(false);

// 设定边缘

this.setMargin(new Insets(8, 14, 8, 14));

}

public EllipseButton(ImageIcon image){

super(image);

// 创建自己的UI

this.setUI(EllipseButtonUI.createUI(this));

// 取消按钮的边框

this.setBorder(null);

// 不用内容窗格的背景

this.setContentAreaFilled(false);

// 设定边缘

this.setMargin(new Insets(-1,-1,-1,-1));

}

}

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.RenderingHints;

import java.awt.Stroke;

import java.awt.geom.Arc2D;

import java.awt.geom.Ellipse2D;

import javax.swing.AbstractButton;

import javax.swing.ButtonModel;

import javax.swing.JComponent;

import javax.swing.UIManager;

import javax.swing.plaf.ComponentUI;

import javax.swing.plaf.basic.BasicButtonUI;

public class EllipseButtonUI extends BasicButtonUI {

protected static EllipseButtonUI singleton = new EllipseButtonUI();

// 创建Stroke用于画按钮

protected static Stroke thickStroke = new BasicStroke(2.0f);

public static ComponentUI createUI(JComponent c) {

return singleton;

}

public void paint(Graphics g, JComponent c) {

// 获得Graphics2D的对象,并开启抗锯齿处理

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

// 得到按钮的大小

AbstractButton b = (AbstractButton) c;

Rectangle viewRect = new Rectangle();

viewRect.x = 0;

viewRect.y = 0;

viewRect.width = b.getWidth() - 1;

viewRect.height = b.getHeight() - 1;

// 缩小矩形以显示抗锯齿效果

viewRect.grow(-2, -2);

// 在按钮矩形内创建椭圆

Ellipse2D ellipse = new Ellipse2D.Float();

ellipse.setFrame(viewRect.getX(), viewRect.getY(), viewRect.getWidth(),

viewRect.getHeight());

// 判断按钮有无被按下

ButtonModel model = b.getModel();

boolean pressed = (model.isArmed() model.isPressed())

|| model.isSelected();

// 根据按钮按下与否设置画笔颜色

if (pressed) {

Color background = UIManager.getColor("Button.select");

g2.setPaint(background == null ? Color.gray : background);

} else

g2.setPaint(UIManager.getColor("control"));

// 填充椭圆按钮

g2.fill(ellipse);

// 根据椭圆按钮的大小来设定边框大小.

Arc2D arc = new Arc2D.Float();

arc.setFrame(viewRect.getX(), viewRect.getY(), viewRect.getWidth(),

viewRect.getHeight());

arc.setArcType(Arc2D.OPEN);

// 设定边框指定弧度的区域

arc.setAngles(viewRect.getWidth(), 0, 0, viewRect.getHeight());

g2.setStroke(thickStroke);

// 根据按钮按下与否设定画笔的颜色

g2.setPaint(pressed ? UIManager.getColor("controlDkShadow") : UIManager

.getColor("controlHighlight"));

g2.draw(arc);

arc.setAngles(0, viewRect.getHeight(), viewRect.getWidth(), 0);

g2.setPaint(pressed ? UIManager.getColor("controlHighlight")

: UIManager.getColor("controlShadow"));

g2.draw(arc);

super.paint(g, c);

// 将图形上下文恢复原来的抗锯齿属性的设置

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_OFF);

}

public Dimension getPreferredSize(JComponent c) {

AbstractButton b = (AbstractButton) c;

Dimension dim = super.getPreferredSize(c);

// 调整高度和宽度,为了更好的显示效果

dim.height += (b.getMargin().top + b.getMargin().bottom);

dim.width += (b.getMargin().left + b.getMargin().right);

return dim;

}

}

public class Test extends JFrame {

public Test() {

EllipseButton b = new EllipseButton("按钮");

add(b);

setSize(150, 100);

}

/**

* @param args

*/

public static void main(String[] args) {

new Test().setVisible(true);

}

}

==================================

老大,我都调试过了才发出来的。无语了......

你看代码了没?

里面是3个类?你要分别创建3个类,把代码分别放到各自的类里,哎~~~~~~

JAVA 任意椭圆方向画法

使用 AffineTransform 把Ellipse2D 旋转一下就可以了。

import java.awt.image.BufferedImage;

import java.awt.geom.AffineTransform;

import java.awt.geom.Ellipse2D;

import java.awt.Color;

import java.awt.Shape;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.io.File;

import javax.imageio.ImageIO;

public class DemoPNG {

    public static void main(String[] args) throws Exception {

        BufferedImage bi = new BufferedImage(600, 800, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = bi.createGraphics();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setPaint(Color.WHITE);

        g2d.fillRect(0,0,600, 800);

        g2d.setPaint(Color.RED);

        Ellipse2D s1 = new Ellipse2D.Double(100, 100, 400, 600);

        g2d.draw(s1);

        AffineTransform transform = AffineTransform.getRotateInstance(Math.PI/4, 300, 400);

        Shape r1 = transform.createTransformedShape(s1);

        g2d.fill(r1);

        AffineTransform transform2 = AffineTransform.getRotateInstance(Math.PI/2, 300, 400);

        Shape r2 = transform2.createTransformedShape(s1);

        g2d.draw(r2);

        g2d.dispose();

        ImageIO.write(bi, "PNG", new File("demo.png"));

    }

}

Java如何编译椭圆周长的程序

publicclassCircle{圆的半径,privatedoublemRadius,publicCircle(doublemRadius){,this.mRadius=mRadius,}获取圆的周长,publicdoublegetLength,{return2Math.PImRadius,},获取圆的面积publicdoublegetArea{returnMath.PImRadiusmRadius。这样即可在Java编译椭圆周长的程序。

java生成椭圆的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java类和对象之构造方法与对象创建之求椭圆面积、java生成椭圆的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。