Computer Science 121
Assignment 3
Due: February 12, 2003

Create a Java applet that will draw circles of various sizes and colors.  Place them where you wish on the applet.  Run the applet from an html file like the one on the other side of this page.  The red circle defined in the file is only an example.  You may keep it or not as you wish.  But definitely add several more circles with different sizes and colors.  You may also include squares and other figures if you wish.  Refer to the appendix in the text book for information about the contents of the Graphics class.

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

public class Circles extends Applet
{
     private Circle redCircle;
     // Add declarations for some more circles.

     public void init ()
     {
          redCircle = new Circle (Color.red, 100, 30, 20);
          // Get new instances of the circles.
     } // method init

     public void paint (Graphics g)
     {
          redCircle.drawCircle (g);
          // Draw the additional circles.
     } // method paint
} // class Circles

// A class that can display a circle.
class Circle
{
 private Color circleColor;
 private int x, y, diameter;

     Circle (Color color, int xValue, int yValue, int d)
     {
          circleColor = color;
          x = xValue;
          y = yValue;
          diameter = d;
     } // constructor

     public void drawCircle (Graphics g)
     {
          g.setColor (circleColor);
          g.fillOval (x, y, diameter, diameter);
     } // method drawCircle
} // class Circle
 
 

<HTML>
 <HEAD>
  <TITLE>Circles</TITLE>
 </HEAD>
 <BODY>
  <H1>Circles</H1>
  <APPLET CODE="Circles.class" WIDTH=300 HEIGHT=300></APPLET>
 </BODY>
</HTML>