CMPU 102 - Assignment 4 Due: by 11:59 PM on Friday, February 24th

In this assignment, you will implement classes representing simple geometric shapes that can draw themselves on a two-dimensional surface.

$Revision: 1.1 $

Basic classes and interfaces

The Canvas interface represents the drawing surface.  The Point class represents a single Cartesian point on the Canvas.  Each Point has an X and Y coordinate.  Just like in geometry, X coordinates increase in value to the right and decrease to the left, and Y coordinates increase in value going up and decrease in value going down:

Here are the methods for the Shape interface.  Each class that you write to represent a shape will implement this interface.

The Point class is extremely simple.  It has a constructor to initialize a new Point with specified X and Y coordinates, and methods getX and getY to return the value of those coordinates as double values.

The Canvas interface is what you will use in the draw method for each of your Shape classes in order to actually draw the Shape.  It has the following methods:


In addition to the simple Shape classes you will need to write, you must also complete a class called CompositeShape.  A CompositeShape is simply a collection of other Shape objects.  For example, you might define a CompositeShape composed of a rectangle and a circle to represent a picture of a tree: the rectangle being the trunk and the circle being the leaves.  Each child Shape is positioned at an offset from the CompositeShape's center, and when the position of the CompositeShape is changed, the position of each child Shape changes by the same amount.

The only methods you will need to implement in CompositeShape are draw and contains.  The draw method should simply draw all of the child Shapes in the order they were added to the CompositeShape.  The contains method should return true if it is passed a Point that is contained in any of the child Shapes, or false otherwise.


In order to create actual Shape objects, you will add code to the static methods defined in the ShapeFactory class.  Each of the methods creates a particular kind of Shape, with a specified size.  By default, each new Shape should start out with its center at the Point representing the origin (0,0), and its color set to the color java.awt.Color.BLACK.

To help you get started, a Shape class called Rectangle is provided, and ShapeFactory's createRectangle method is implemented to use it.

Here are the methods defined in ShapeFactory, and how each of them should work:

Getting started

Start by importing the file into Eclipse using the same procedure you used in Lab 1: from the menu, choose "File->Import->Existing Projects into Workspace", click "Select archive file", select "" from the file selection dialog, and then click the "Finish" button.

Your main task for the project is to implement all of the methods in the ShapeFactory class.  Ths will involve writing one or more new classes (implementing the Shape interface), and also modifying the existing CompositeShape class.)

You may want to refer to the instructions for Creating a new class in Eclipse.

Testing your project

You will need to test your project carefully.  Add tests to the ShapeTest class to test the Shape classes you have written.  The class already contains some example tests on a rectangle that you can use as a model.  ShapeTest is a JUnit test case, so you can execute tests the same way as in Lab 2.  Be sure to:

Note that it will not be very easy to test the draw method using JUnit, since it requires a Canvas to draw on.  So, to test the draw methods of your Shape classes, you can run the provided PictureDemo class.  It will show a window that looks something like this:

To test the contains and draw methods interactively, you can run the provided ContainsDemo class.  As you move the mouse, the shapes containing the location (point) of the mouse will be highlighted.  Note that the "H" shape in the upper left corner is an instance of CompositeShape: make sure that all parts of the H are highlighted whenever the mouse pointer is inside the H.

To run the main method for each of these demos, right click on the name of the source file which defines the class and choose "Run As->Java Application".

Hints and Tips

Something Fun

Once you have all of your Shape classes working correctly, try running the main method in the Invaders class (right click on "", then "Run As->Java Application").  Use the left and right arrow keys to move, and the space bar to fire.

Submitting the project

To submit the project, type the following commands in a terminal window (hitting return after each command):

cd eclipse-workspace
submit102 assign4

You may submit as many times as you wish.  The final submission prior to the deadline will be graded.