experience modifying an existing body of work in a structured manner to
produce significant new behavior with a minimum of effort.
your hands dirty with a little Graphical User Interface (GUI pronounced
gain more experience reading Java documentation.
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
interface with it as follows:
public interface Shape extends Drawable
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.
a new project in your chosen IDE and add copies of your Program 3 source
to it except for the
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.
- 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.
- Extend the Shape
interface with Drawable.
- 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.
- 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.
- 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.
- 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.