Java Graphics  «Prev  Next»

Drawing Graphics Primitives - Exercise

Objective: Use the AWT Graphics class to draw graphics primitives.

Instructions:

The Olympic symbol consists of five interlocking circles of differing colors (blue, black, red, yellow, and green).
Use the AWT Graphics class to create an applet named Olympics that draws the Olympic symbol. Here is an example of how your applet could look after completing this exercise:

Olympic circles
Olympic circles

You can use the constant Color objects Color.blue, Color.black, Color.red, Color.yellow, and Color.green with the setColor() method in the Graphics class to change the color for each circle to be drawn.
Keep in mind you will also need to override the paint() method of the applet.
Following is the source code for an applet that draws some simple graphics, if you would like to see an example.
This applet demonstrates overriding the paint() method to create a simple drawing using the methods setColor() and drawRect() from the Graphics class:

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

public class Blockhead extends Applet { 

  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.drawRect(30, 30, 50, 50);
    g.drawRect(35, 35, 10, 10);
    g.drawRect(65, 35, 10, 10);
    g.drawRect(50, 50, 10, 10);
    g.drawRect(40, 65, 30, 10);
  }

}

Source files

A prepared HTML file in the download file has been made available from the Resources page.
The specific HTML file for this exercise can be found in the 03-03 folder.

Exercise scoring

The full credit for this exercise is 10 points. To receive full credit, you will need to successfully create the source code for the applet.

What to submit

In the text box below, cut and paste the source code for the Olympics applet.
Click, Submit to submit the code.