📜  Java AWT |椭圆二维

📅  最后修改于: 2022-05-13 01:54:56.145000             🧑  作者: Mango

Java AWT |椭圆二维

Ellipse2D 类存在于Java.awt.geom 包中,它用于通过声明其框架矩形来定义椭圆。这个类只是所有存储二维椭圆的对象的抽象超类。

  • Ellipse2D.Double 定义具有双精度的椭圆。
  • Ellipse2D.Float 定义具有浮点精度的椭圆。

类的构造函数是:

  1. Ellipse2D.Double() :在位置 0, 0 和大小 0, 0 创建一个椭圆。
  2. Ellipse2D.Double(double x, double y, double w, double h) :在位置 x、y 和宽度 w 和高度 h 处创建一个椭圆。
  3. Ellipse2D.Float() :在位置 0, 0 和大小 0, 0 创建一个椭圆。
  4. Ellipse2D.Float(Float x, Float y, Float w, Float h) :在位置 x、y 和宽度 w 和高度 h 创建一个椭圆。

常用方法

methodexplanation
contains(double x, double y)returns whether the point is inside the ellipse or not
contains(double x, double y, double w, double h)returns whether the rectangle is inside the ellipse or not
equals(Object o)Determines whether or not the specified Object is equal to this Ellipse2D.
intersects(double x, double y, double w, double h)returns whether the rectangle intersects the ellipse or not
setFrame(double x, double y, double w, double h)Sets the location and size of the framing rectangle of this Shape to the specified rectangular values.

下面的程序说明了 Ellipse2D 类:

  1. Java程序创建两个椭圆并将它们绘制到Java小程序:为了在Java小程序上创建椭圆形状,我们将初始化名为“ed”和“ed1”的 Ellipse2d 类对象。 “ed”对象的构造函数中传入的4个参数分别是边框左上角的X坐标、边框左上角的Y坐标、边框的宽度和边框框架矩形的高度。在“ed”的构造函数中,我们什么都不传递,这意味着椭圆被初始化为位置 (0, 0) 和大小 (0, 0)。为了在屏幕上显示它们,我们创建了一个 Graphics2d 类“g1”的对象并调用 g1.draw() 方法。
    // java program to create two ellipse and 
    // draw them to a java applet
    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
    public class solve1 extends JApplet {
        public void init()
        {
            setSize(300, 300);
        }
      
        // paint the applet
        public void paint(Graphics g)
        {
            // create a ellipse2d
            Ellipse2D ed = new Ellipse2D.Double(100.0d, 100.0d, 
                                                 120.0d, 80.0d);
      
            // create another ellipse2d
            Ellipse2D ed1 = new Ellipse2D.Double();
      
            // set framing rectangle of ellipse
            ed1.setFrame(100.0d, 100.0d, 80.0d, 120.0d);
      
            Graphics2D g1 = (Graphics2D)g;
      
            g1.setColor(Color.red);
      
            // draw the first ellipse
            g1.draw(ed);
      
            g1.setColor(Color.blue);
      
            // draw the first ellipse
            g1.draw(ed1);
        }
    }
    

    输出:

  2. 创建两个椭圆并检查一个点或一个矩形是否包含在该椭圆中或与之相交的Java程序:要检查一个点或一个矩形是否包含在该椭圆中或与两个椭圆相交,我们首先创建两个椭圆以我们上面创建的类似方式。然后我们通过在 Graphics2d 对象“g1”上调用方法 drawRect() 创建 2 个矩形。 drawRect()方法中的参数指定要绘制的矩形的x坐标、要绘制的矩形的y坐标、要绘制的矩形的宽度和要绘制的矩形的高度。要检查它们是否包含它,我们调用 ed.contains() 方法并传递其中的点或矩形的坐标,并在消息对话框中显示结果。
    // java program to create two ellipse and check whether
    // a point or a rectangle is contained in that ellipse
    // or intersected by it
    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
    public class solve2 extends JApplet {
        public void init()
        {
            setSize(300, 300);
        }
      
        // paint the applet
        public void paint(Graphics g)
        {
            // create a ellipse2d
            Ellipse2D ed = new Ellipse2D.Double(100.0d, 100.0d,
                                                 120.0d, 80.0d);
      
            // create another ellipse2d
            Ellipse2D ed1 = new Ellipse2D.Double();
      
            // set framing rectangle of ellipse
            ed1.setFrame(100.0d, 100.0d, 80.0d, 120.0d);
      
            Graphics2D g1 = (Graphics2D)g;
      
            g1.setColor(Color.red);
      
            // draw the first ellipse
            g1.draw(ed);
      
            g1.setColor(Color.blue);
      
            // draw the first ellipse
            g1.draw(ed1);
      
            g1.setColor(Color.black);
      
            // draw a rectangle
            g.drawRect(100, 100, 80, 100);
      
            g1.setColor(Color.orange);
      
            // draw a rectangle
            g.drawRect(150, 150, 10, 10);
      
            // does it contain point
            JOptionPane.showMessageDialog(this, "ellipse 1 contains point 150, 150  " 
                                                            + ed.contains(150, 150));
      
            // does it contain rectangle
            JOptionPane.showMessageDialog(this, "ellipse 1 contains rectangle at" +
            "  150, 150 of width 10 and height 10 " + ed.contains(150, 150, 10, 10));
      
            // does it contain rectangle
            JOptionPane.showMessageDialog(this, "ellipse 1 contains rectangle at "+
             " 150, 150 of width 80 and height 100 " + ed.contains(150, 150, 80, 100));
      
            // does it  intersect rectangle
            JOptionPane.showMessageDialog(this, "ellipse 1 intersect rectangle at "+
            " 150, 150 of width 80 and height 100 " + ed.intersects(150, 150, 80, 100));
        }
    }
    

    输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。

参考: https: Java/awt/geom/Ellipse2D.html