Java-Circle and Color

Circle

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.

Color

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, Color.black, Color.red) 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

0

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.setColor(Color.PINK);
        g.fillOval(20, 20, 100, 100);
        g.setColor(Color.blue);
        g.fillOval(150, 20, 50, 100);
        g.setColor(Color.red);
        g.drawOval(20, 140, 100, 100);
        g.setColor(Color.MAGENTA);
        g.drawOval(40, 140, 60, 100);
        g.setColor(Color.ORANGE);
        g.drawOval(60, 140, 20, 100);
    }
}

Leave a Comment