Java Graphics Draw Arcs


A segment of an oval is an arc. The arc angle can be positive (sweeps anti-clockwise) or negative (sweeps clockwise). As with other figures, the arcs can be outline or solid.

Supporting methods from java.awt.Graphics class

  1. void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): draws an outline arc.
  2. void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): draws a filled (solid) arc.

where

  • x and y: Indicates the x and y coordinates where the arc is to be drawn
  • width and height: Indicates the width and height of the arc
  • startAngle: Dictates the starting angle of the arc
  • arcAngle: The angular extent of the arc (relative to the start angle)

The following program uses the above two methods.

import java.awt.*;
public class ArcsDrawing extends Frame
{
  public ArcsDrawing()
  {
    setTitle("Arcs Drawing");
    setSize(525, 300);
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawArc(60, 70, 200, 200, 0, 180);
    g.fillArc(300, 70, 200, 200, 0, 270);
  }
  public static void main(String args[])
  {
    new ArcsDrawing();
  }
}

Java Graphics Draw Arcs

g.drawArc(60, 70, 200, 200, 0, 180);

An arc is drawn with starting points 60 and 70. From this point, the arc sweeps anti-clockwise direction as the angle 180 is positive. As the starting angle is 0 and ends with 180, the arc takes the shape of a semicircle. The arc width and height are 200 pixels.

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

CHECK OUT FOR YOUR ONE STOP DESTINATION

All AWT Components – At a Glance.

Drawing All Java Geometrical Figures.

Leave a Comment

Your email address will not be published.