Java Draw Circles Graphics Applets


No special or predefined method exists with Graphics class to draw circles. But still, circles can be drawn in three styles using other methods.

  1. Using drawRoundRect()
  2. Using drawOval() – most preferred
  3. Using drawArc()

All the earlier programs like drawing rectangles are illustrated with Frame. Now let us go for applet. Applet includes init(), start() and paint() methods etc. The migration steps from graphics application to applet are available in "Java AWT Radio Buttons – Applet GUI".

Knowledge of "applets" is required to do with this program.

Applet File Name: ThreeStyles.java

import java.awt.Graphics;
import java.applet.Applet;    
public class ThreeStyles extends Applet
{
  public void paint (Graphics g)
  {			                 // using drawRoundRect()
    g.drawRoundRect(40, 50, 90, 90, 200, 200);
    g.fillRoundRect(40, 160, 90, 90, 200, 200);
			                 // using drawOval()
    g.drawOval(150, 50, 90, 90);
    g.fillOval(150, 160, 90, 90);
			                // using drawArc()
    g.drawArc(270, 50, 90, 90, 0, 360);
    g.fillArc(270, 160, 90, 90, 0, 360);
  }
}

HTML file to run the above applet

File Name: ManyStyles.html


Java Draw Circles Graphics Applets

Observe in all the above methods, width and height are kept the same as 90, 90. Output is shown in both browser and appletviewer.

For more practice, another program on graphics with applets is available at Drawing Polygons.

CHECK OUT FOR YOUR ONE STOP DESTINATION

All AWT Components – At a Glance.

Drawing All Java Geometrical Figures.

1 thought on “Java Draw Circles Graphics Applets”

Leave a Comment

Your email address will not be published.