CSC 171 LAB #9
FALL 2001
GOAL:
1.
Become
familiar with inheritance in class definitions
TASKS:
1.
Modify
the post-modern art drawing Applet.
(does this
make it a “post-post-modern art drawing Applet?)
·
Use
an inheritance hierarchy to define the key classes
·
Change
the Applet to a Jframe application
BACKGROUND:
In lab #8 you created a drawing
applet that randomly draws lines, rectangles and ovals using a set of “smart”
shape classes: MyLine, MyRect, and MyOval. If you were remotely conscious
during that effort, then you probably noticed a large amount of similarity
between these three classes. When we see similarity between classes, we
naturally consider factoring out the common code elements into a
super-class. The process of analyzing code, and implementing it in a more
elegant object-oriented design goes by the term re-factoring.
In this lab, you are to
implement the same drawing applet with a few changes. Modify the MyLine,
MyRect, and MyOval classes to create the class hierarchy shown below
Java.lang.Object
|
MyShape
/ \
MyLine MyBoundedShape
/ \
MyOval MyRect
The
classes of the MyShape hierarchy should be “smart” – they should know how to
draw themselves. The class MyShape must be abstract. The only data
representing the coordinates of the shapes in the hierarchy should be definined
in the class MyShape. Lines, rectangles, and ovals can all be drawn if you know
two points in space. Lines require x1, y1, x2, y2 coordinates. You can have the
same four coordinates for ovals and rectangles – (x1,y1) and (x2,y2) represent
diagonally opposite corners of the bounding box of the shape. You can calculate
the four arguments needed to draw the shapes from the 4 coordinates. The upper
left coordinate is the pair (min(x1,x2),min(y1,y2)). The width is
max(x1,x2)-min(x1,x2). The height is max(y1,y2)-min(y1,y2).
The class MyShape must
define at least the following public methods
public abstract void draw(Graphics g)
The MyBoundedShape is similar to
MyShape, with an important difference. In modern graphics packages, a shape can
have both a “fill color” and a “line color”. The MyBoundedShape abstract class
includes this extra color to support this feature. Constructors with two color
parameters should be supplied, and the super constructor should be used appropriately.
Your application should include only a single array
of shapes (one array of MyShape). This array must be at least 15 items long.
Fill the arrays with random shapes of the appropriate type. To determine the
type of shape to create, generate a random integer from {1,2,3}. Use switch or
if/else logic to call the appropriate constructor. Your applications paint
methods should loop through the arrays and invoke each shape’s draw method with
a call like myshapes[i].draw(g). The power of polymorphism will allow each
shape to determine it’s appropriate drawing behavior. There should be no
MyLine, MyOval, or MyRect references in the program – only MyShape references
are allowed.
STEPS:
1.
Begin
by writing the MyShape abstract class definition. It should look a lot like
your MyLine class from last week, with the changes described above.
2.
Define
abstract MyBoundedShape to extend MyShape, as described above. The constructor
references the super constructor but sets the bounding color locally.
3.
Now,
implement MyLine, which extends MyShape – all you should need to do is to write
a constructor, which references the super constructor, and implement the paint
method.
4.
Implement
MyOval and MyRect as extensions of MyBoundedShape – all you should need to do
is to write a constructor, which references the super constructor, and
implement the paint method.
5.
In
implementing the paint method for MyRect and MyOval, two draws will be
necessary. One “fill” draw for the fill region and one “draw” draw for the
boundary. You want to support null colors for the fill color and the line
color. Some shapes may be solids without boundaries (a null line color). Some
shapes may be outlines with no fill (null fill color). Test the color for a
null reference before drawing. If you find a null color reference, skip
drawing.
6.
Re-read
section 9.20 of D&D. Pay special attention to Fig 9.33 (p 503) as it will
give you a template on how to implement frame based. For this lab, you can use
swing or the vanilla awt – your choice. D&D shows a swing approach. The
plain old awt approach is little different (remove the “J”s from most of the
classes), you may have to look up the
corresponding awt classes on the Java reference site.
7.
The
example in 9.20 demonstrates the use of a private inner class for the button
listener, rather than having the Frame implement actionListener itself. This is
a minor modification.
8.
At
this point you should have 6 class files in your directory. Use the Java
archive utility to combine them into a single archive file.
9.
When
you get the paper ready call your lab TA over and demo the program for her/him.
Give the paper to your TA. This completes the hand in process. The deadline is
one week – no late assignments accepted.
10.
The
demo must run from the local machine. All that should be in the directory is
your jar file. Use a command line invocation for your application
java –classpath myjar.jar
myFrameApplication