CSE 413 -- Assignment 4 -- Java Warmup

Due at the beginning of lecture, Monday, May 3, 1999. You should hand in a printed listing of your Java source code.  We also want you to turn in an electronic copy of your source program, but have not worked out the details yet.  Watch this space and the course mailing list as more information becomes available.


Program Description

Write a small Java program that allows users to place colored shapes on a drawing window with the mouse.  The user interface should include two sets of buttons:   one to select the current shape (circle, square, and anything else you care to add), and another to select the current color (red, green, blue, etc.).  Each time the mouse is clicked in the window, a solid shape of the current kind and color should appear in the window at the mouse location.

The exact look of the user interface is up to you, but it should be clearly labeled so it is obvious how it works.  You are welcome to experiment and extend your program.   For example, you might want to have a shape appear when you press the mouse button, and be able to drag it around until you release the button.  Or you might want to include the ability to move or delete existing shapes from the screen.

You may use any Java development environment you wish, however, your program should work properly with Sun's JDK 1.1.x or 1.2.x.  The program should be an application, not an applet.

Implementation Issues

Although much of this assignment  revolves around the Java AWT (graphics, and event handling), there is a bit of programming to do.  When a shape is added (or moved, dragged, or erased), repaint() should be called to schedule the window to be redrawn.  That will eventually generate a call to paint(), which is responsible for (re-)drawing the window.  The difficulty is, how does paint() know what to draw?

The answer is that as each shape is added to the window, information about it needs to be stored somewhere. A good object-oriented way to store this information is to create an object for each shape and store those objects in a list, or Vector.  Each object in this list should contain instance variables describing it (position, color), and should contain a method draw() that will draw the object on the screen at its location using the proper color.

Common behavior for all shapes should be defined in a parent class Shape.   Among other things, every Shape should contain (pick one) the center or corner coordinates of the shape plus its color (an instance of class Color).   Class Shape should include an abstract method draw().  This needs to be abstract, because there is no generic draw method that makes sense for all Shapes.

You should include extended classes for each particular Shape (i.e., public class Circle extends Shape, and similarly for squares and any others you define).  The extended classes should include any additional instance variables needed to describe the particular shape (diameter, height and width, etc.), and should include an implementation of draw() that is appropriate for the shape.  Then, for each shape s, method paint() only has to execute s.draw() to draw the shape at the proper location with the proper color.