Java-Circle and Color


The Graphics class does not have any method for circles or ellipses. However, the drawOval( ) method can be used to draw a circle or an ellipse. Ovals are just like rectangles with overly rounded corners. The drawOval( ) method takes four arguments, the first two represent the top left corner of the oval and the other two represent the width and height of the oval itself. Note that if the width and height are the same, the oval becomes a circle. The fillOval( ) method draws a solid oval.


The AWT color system allows you to specify any color you want. Color is encapsulated by the Color class. The Color class defines several constants (for example,, to specify a number of common colors. By default, graphics objects are drawn in the current foreground color. You can change this color by calling the Graphics method setColor( ).



Program Source

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class Javaapp extends Applet {
    public void paint(Graphics g) {
        g.fillOval(20, 20, 100, 100);
        g.fillOval(150, 20, 50, 100);
        g.drawOval(20, 140, 100, 100);
        g.drawOval(40, 140, 60, 100);
        g.drawOval(60, 140, 20, 100);

Leave a Comment