CPE 102

Winter 2007

Lab 5








Ground Rules





You will be modifying your Program 3 source so that it can actually draw shapes to your computer’s screen.  There are lots of ways to do this, some better than others.  You are going to develop a new interface called Drawable and extend the Shape interface with it as follows:


public interface Shape extends Drawable


                                      // Shape code here



You must then implement its method (specified below) in the necessary classes – note that because of inheritance this is not all of the classes – think about where the best places to implement the method so that you minimize duplication of code.  You will also implement Drawable in the WorkSpace class.  Once this is completed you can add the provided driver to your project and add code to create and add various shapes to a WorkSpace object.  You are required to create two of each shape, filled and unfilled.  The driver will draw your shapes to the screen using the newly implemented Drawable interface and its method.




  1. Create a new project in your chosen IDE and add copies of your Program 3 source to it except for the P3TestDriver.java.  Make sure it compiles before continuing.  Note: If you Program 3 is not functional ask your instructor for help in fixing it before beginning this lab.


  1. Write an interface called Drawable.  This interface should have a single method called draw that has a single parameter of java.awt.Graphics and a return type of void.


  1. Extend the Shape interface with Drawable.


  1. Implement the draw method in the minimally necessary classes so that all geometric shapes in the project can be drawn.  This method must use methods of the java.awt.Graphics class to draw your geometric shapes to your computer’s screen.  You will have to read the javadocs to discover the appropriate methods to use.


  1. Implement the Drawable interface and its draw method in WorkSpace.  In this class the method simple iterates over the collection of shapes and calls each of their draw methods.


  1. When you have completed the previous steps you can add the provided driver to your project, compile, and run.  If all is well you should see two circles drawn in a 500x500 pixel window on your screen.  Note that it may take a few seconds for the window to appear so be patient.


  1. Now you must modify the provided driver to draw at least two of each shape so they can be seen, Circle, Rectangle, Square, Triangle, and ConvexPolygon, one filled and one not filled and you must use at least three different colors (one per shape).  Be creative and compose a picture if you are so inclined.