A good answer might be:

drawOval( 100-50, 300-50, 2*50, 2*50 )

Of course the method would work if you did the arithmetic and put the results in the method call, but why bother?

Drawing Rectangles

To draw a rectangle, use the drawRect() method of the Graphics object. This method looks like:

drawRect(int  x, int  y, int  width, int  height)

It draws the outline of a rectangle using the current pen color. The left and right edges of the rectangle are at x and x + width respectively. The top and bottom edges of the rectangle are at y and y + heigth respectively.

Of course, if you want a square you would use this method also. Here is an applet that surrounds the entire drawing area with a rectangle, then puts another rectangle in the center.

import java.applet.Applet;
import java.awt.*;

// assume that the drawing area is 150 by 150
public class SquareAndRectangle extends Applet
  final int areaSide = 150 ;
  final int width = 100, height = 50;

  public void paint ( Graphics gr )
    setBackground( Color.green );
    gr.setColor( Color.blue );

    // outline the drawing area
    gr.drawRect( 0, 0, areaSide-1, areaSide-1 );

    // draw interiour rectange.
    gr.drawRect( areaSide/2 - width/2 ,
        areaSide/2 - height/2, width, height );

Here is what it draws on your screen:

Not all browsers can run applets. If you see this, yours can not.

When drawing the outer square, one was subtracted from its width and height so that the edges would be inside the drawing area.


What do you suppose the following method does?

gr.setColor( Color.blue );