Java Draw Ovals

Java Draw Ovals

Summary: In this tutorial "Java Draw Ovals", you learn drawing of drawing ovals in Java graphics.

After learning rectangles drawing, let us see the oval. Oval (Latin: Ovum meaning egg) is an elliptical shape rounded at both ends.

Supporting methods from java.awt.Graphics class

  1. void drawOval(int x, int y, int width, int height);
  2. void fillOval(int x, int y, int width, int height);

Observe the parameters of drawOval() are the same as drawRect(). Infact, the Graphics object first draws a rectangle as per the parameters and inscribes an oval, largest possible, in it. That is, the oval coordinates are that of bounded rectangle. Let us be clearer with an example.

import java.awt.*;
public class OvalsDrawing extends Frame
{
  public OvalsDrawing()
  {
    setTitle("Ovals Drawing");
    setSize(450, 200);
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawOval(50, 80, 150, 100);
    g.fillOval(230, 80, 150, 100);
  }
  public static void main(String args[])
  {
    new OvalsDrawing();
  }
}	


Java Draw Ovals
Output of OvalsDrawing.java of Java Draw Ovals

g.drawOval(50, 80, 150, 100);

First a rectangle is drawn with x, y coordinates of 50 and 80 with a width and height of 150 and 100. In this rectangle, an oval is inscribed.

Java Draw Ovals

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code.

Leave a Comment

Your email address will not be published.