Rectangles

 

To draw a rectangle on the screen you call the drawRect method available in the Java Graphics class. The drawRect method requires four arguments. The first pair of numbers specify the x and y coordinates of the left top corner within the defined applet window. The next pair of numbers specify the width and height of the rectangle. In the applet below, the left top corner of the blue rectangle is at (10,40), the width is 150, and the height is 200. The fillRect(200,100,80,80) draws a filled rectangle with the left top corner at (200,100). Since the width and height are equal, a square is drawn.

 

   
 

 

The code for this Java rectangle applet is listed below.

 

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



public class rectang1 extends
 java.applet.Applet {

  
  public void paint(Graphics g){



    g.setColor(Color.blue);
    g.drawRect(10,40,150,200);

.


    g.setColor(Color.yellow);
    g.fillRect(200,100,80,80);
   }
}
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.
.

Set the color to blue and draw a rectangle with the top left corner 10 to the right and 40 down within the applet window. The width is 150 and the height is 200.

Set the color to yellow and draw a filled rectangle with the top left corner 200 to the right and 100 down. The width and height are an equal 80 so the figure is a square.

 

To draw a rounded rectangle on the screen you call the drawRoundRect method available in the Java Graphics class. The drawRoundRect method draws rectangles with rounded edges and it requires six arguments. The first four work the same as for normal rectangles. The last pair of numbers determine how far along the edges of the rectangle the arc for the corner will start. If the numbers are large, the rectangle has a more rounded appearance like the red one below. The green rectangle has small values to determine the arc. Since the blue rectangle is a square and the arc values are equal to its width and height, a circle is drawn.

   
 

 

The code for this Java rectangle applet is listed below.

 

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

.

public class rectang2 extends
 java.applet.Applet {

  public void paint(Graphics g) {

.

   g.setColor(Color.red);
   g.drawRoundRect(10,40,150,200,40,60);



.


   g.setColor(Color.green);
   g.fillRoundRect(200,20,60,100,10,10);


.


   g.setColor(Color.blue);
   g.drawRoundRect(210,210,80,80,80,80);
 

  }
}
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.
.

Set the color to red and draw a rectangle with the top left corner at (10,40) within the applet window. The width is 150 and the height is 200. The arc for the rounded edge is contained in a 40x60 pixel rectangle.

Set the color to green and draw a filled rectangle with the top left corner at (200, 20). The width is 60 and the height is 100. The arc for the rounded edge is contained in a 10x10 pixel square.

Set the color to blue and draw a rectangle with the top left corner at (210, 210). The width and length are 80 and the arc for the rounded edge is contained in an 80x80 pixel square. Notice that the figure is a circle.

 

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 accommodate 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: Circles

Back to the Table of Contents