Way2Java

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

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();
  }
}

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.