The Circle

To draw a circle on the screen you call the drawOval method available in the Java Graphics class. The drawOval method requires four numerical arguments described below.

   

g.drawOval(50,60,100,150) creates the applet pictured on the right. The four numbers define a rectangle within the applet window. This rectangle forms a boundary about the desired oval in the following way. The first pair of numbers define the left top corner of the boundary rectangle. The next two numbers designate the rectangle width and length, respectively. The oval is then drawn within the rectangle. If a circle is desired, the last two numbers must be equal.

View Code

The code for the circle applet below is a good way to begin. Copy it in a text editor and place it in a file called "circle.java".

 

   
 

 

The code for this circle applet is listed below.

 

import java.awt.Graphics;
import java.awt.Color;


public class circles 
extends java.applet.Applet {


  public void paint(Graphics g) {



    g.setColor(Color.red);
    g.drawOval(50,50,150,150);
    


    g.setColor(Color.blue);
    g.fillOval(150,150,50,50);
    
  }
}
This allows us to use the Graphics methods of the Java Abstract Windowing toolkit.


We want to generate an applet .


The paint method is the way an applet draws objects on the screen.

The first circle will be red and inscribed inside a 150 x 150 square. The top left corner of the square is 50 pixels to the right and 50pixels down inside the defined applet window.

The second circle is blue and filled. It is inscribed inside a 50 x 50 square. The top left corner of the square is 150 pixels to the right and 150 pixels down inside the defined applet window.

After compiling this program we need to create an HTML page with a reference to the class file. It must run in a graphic window that is large enough to accomodate the pixels in the applet.

Save the HTML code in the same directory as the class file.

When you load the HTML file in your browser the applet will execute within the window.

 

Go to the next topic: Drawing Graphics Files

Back to the Table of Contents