For your third lab, you will create a Java program that draws some
shapes.
First, create the following classes:
- Shape class, that contains the method void draw(Graphic g)
- Rectangle class, subclass of shape
- Constructor takes 5 input parametrs: ints x, y, width, height,
and Color c
- public void draw(Graphic g) method that overrides the shape
draw method
- Circle class, subclass of shape
- Constructor takes 4 input parameters: ints x, y,
radius ((x,y) is the center point of the circle), and Color C
- public void draw(Graphic g) method that overrides the shape
draw method
- Triangle class, subclass of shape
- Constructor takes 7 input parameters: ints x1, y1,
x2, y2, x3, y3 (vertices), and color C
- public void draw(Graphic g) method that overrides the shape
draw method
Next, create a new class ShapePanel, subclass of JPanel that contains
the following methods
- Constructor, sets a nice preferred size
- public void addShape(Shape s), adds the shape s to the panel
(that is, adds s to an array of shapes stored as an instance
variable). There can be a hard limit to the number of shapes that
can be added to the ShapePanel, but don't use magic numbers -- use
constants instead. This function should probably cause the panel
to be repainted (that is, call the repaint method)
- protected void paintComponent(Graphics g). Overrides the
standard paintComponent method. First, call the superclass method
(to paint background & such), and then draw all of the shapes that
have been added to this panel.
Finally, create a new class MainPanel, sublcass of JPanel that:
- Contains two panels: A ShapePanel on top, and a standard
JPanel on bottom
- The JPanel on bottom should contain 3 buttons: add circle, add
rectangle, add triangle
- Pressing one of these buttons should create the approprate random
shape with a random color
- You can use 3 different button handlers (that is,
ActionListeners) for this, if you want to (my solution uses 3 handlers)
Grading
You will be graded on several criteria:
Program decomposition: 25
Did you break your code into the required classes & subclases, did
you use polymorphism to draw the shapes correctly?
Coding standards 10
Be sure to do proper indentation & commenting. Also, no magic
numbers! Use constants instead.
See the
Coding Standards for more
information
Working
Program compiles and puts up a window: 20
Contains all required buttons: 20
Buttons correctly put up random shapes 25
Submission
All file(s) required for
your project should be in the folder
https://www.cs.usfca.edu/svn/<username>/cs112/Lab3/