The ellipses or ovals are drawn by Ellipse2D class.This requires you defined to be a rectangle enclosing the boundaries between the ellipse.There the concept of a circle, so that it must be obtained by an ellipse enclosed in a square.
In Ellipse2D Java Example we are using package java.awt.geom.*. Following is a table that lists some of the Constructors and methods of Ellipse2D class:
Constructors and Methods | Description |
Ellipse2D.Double () | Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0). |
Ellipse2D.Double (double X, double Y, double W, double H) | Creates a Ellipse2D with a size of (W, H) at location (X, Y). |
Ellipse2D.Float () | Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0). |
Ellipse2D.Float (float X, float Y, float W, float H) | Creates a Ellipse2D with a size of (W, H) at location (X, Y). |
contains (double X, double Y) | Returns true if the point (X, Y) is within the Ellipse. |
contains (double X, double Y, double W, double H) | Returns true if the Ellipse (X, Y, W, H) is entirely within the Ellipse. |
intersects (double X, double Y, double W, double H) | Returns true if the Ellipse (X, Y, W, H) intersects the Ellipse. |
setFrame (double X, double Y, double W, double H) | Sets the location and size of the Ellipse. |
Drawinganellipseexampleshownthecodeas follows:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Draw2DEllipseExample extends Applet {
public static void main(String[] args) {
Frame Draw2DEllipse = new Frame("Draw 2DEllipse Example");
Draw2DEllipse.setSize(350, 250);
Applet Draw2DEllipseExample = new Draw2DEllipseExample();
Draw2DEllipse.add(Draw2DEllipseExample);
Draw2DEllipse.setVisible(true);
Draw2DEllipse.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Draw Ellipse2D Example", 50, 40);
g.setFont(new Font("Arial",Font.BOLD,10));
g.drawString("http://ecomputernotes.com", 200, 205);
super.paint(g);
Graphics2D G2D = (Graphics2D)g;
G2D.setColor(Color.orange);
G2D.setStroke(new BasicStroke(3.0f));
Ellipse2D E2D = new Ellipse2D.Float(100.0f,75.0f,50.0f,100.0f);
G2D.draw(E2D);
}
}