Computer Science 121
Assignment 3
Due: February 25, 2004

Create a Java applet that will draw figures of various sizes and colors.  Place them where you wish on the applet.  Run the applet from an html file like the one below.  The red circle and blue rectangle defined in the file are only examples.  You may keep them or not as you wish.  But definitely add several more figures with different sizes and colors.  You can draw a square by making the width and height equal.

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

// A class that draws several figures, a red circle and a blue rectangle.
public class Figures extends Applet
{
     private Circle redCircle;
     private Rectangle blueRectangle;
 
     public void init ()
     {
          redCircle = new Circle (Color.red, 100, 100, 50);
          blueRectangle = new Rectangle (Color.blue, 200, 100, 60, 40);
     } // method init
 
     public void paint (Graphics g)
     {
          redCircle.drawCircle (g);
          blueRectangle.drawRectangle (g);
     } // method paint
} // class Figures
 
// A class that can display a circle.
class Circle
{
     private Color color;
     private int x, y, diameter;
 
     Circle (Color c, int xValue, int yValue, int d)
     {
          color = c;
          x = xValue;
          y = yValue;
          diameter = d;
     } // constructor
 
     public void drawCircle (Graphics g)
     {
          g.setColor (color);
          g.fillOval (x, y, diameter, diameter);
     } // method drawCircle
} // class Circle

// A class that can display a rectangle.
class Rectangle
{
     private Color color;
     private int x, y, width, height;
 
     Rectangle (Color c, int xValue, int yValue, int w, int h)
     {
          color = c;
          x = xValue;
          y = yValue;
          width = w;
          height = h;
     } // constructor
 
     public void drawRectangle (Graphics g)
     {
          g.setColor (color);
          g.fillRect (x, y, width, height);
     } // method drawRectangle
} // class Rectangle

The following is an html file that can be used to display the applet above.  It can also be displayed using a browser such as Internet Explorer or Netscape.  This html file is actually using xhtml, extended hypertext markup language.  In xhtml, all tags use lower case, all opening tags have corresponding closing tags, and all attributes are in quotes.  Tags that do not come with closing tags, such as <br> are changed to <br />.  Xhtml was designed to be compatible with XML, extended markup language.

<html>
     <head> <title>Figures</title> </head>
     <body>
          <h1> Figures </h1>
          <applet code = "Figures.class"  width = "300"  height = "300" ></applet>
     </body>
</html>