PurposeThis assignment is a chance to learn a bit about the Java Swing package, and explore
some of the design issues involved in structuring programs around models and views.
It is also a chance to gain some experience designing classes and figuring out
how to distribute code in a class hierarchy. In this assignment, you'll
construct the model and view part of a graphics drawing program.
Next time, we'll extend the program to add a controller.
There is no starter code this time, but you'll find several useful examples
posted on the class calendar and in Lab #3.
The goal is that when the program runs, a frame will be displayed with a number
of shapes drawn on it. (See the shapes below).
Organize your code using a model-view pattern similar to the one explained in the examples done in class. The main difference is that you must include an abstract class
to represent generic shapes, and create specific shapes by extending that class.
The abstract class gives you a place to put code that is common to all shapes,
without having to duplicate it in each specific class.
Your code should be organized using the following classes and interfaces:
- Interface Shape. The minimal interface of a shape. This should include,
at least, a method to draw the Shape on a java Graphics object. As the
program evolves, you may need to add additional methods to this interface,
but keep it as small as possible for now. (At this point, there should not be any code to handle events. Event handling will be included in the next installment of this assignment.)
- Class AbstractShape. This should be an abstract class that
implements interface Shape. Add instance variables, additional
methods and default implementations here once it becomes clear that all Shapes
need to have such fields and methods.
- Specific concrete shape classes that extend AbstractShape:
The constructor of the fibonacci square should take 5 parameters: int x, int y, Color c, int quadrant and int n.
(x, y) are the coordinates of the location of the square (you can choose which point the coordinates (x,y) represent, e.g. the top left corner of the square, or the center of the square).
is the color of the square and the arc.
quadrant is the corresponding quadrant of the arc (= a quarter circle.) There are four ways to draw the arc within the square. We will label the four possible arcs by the quadrant they would be in if the arc was part of a full circle centered at the origin, that is:
is an arc in quadrant 1
is an arc in quadrant 2
is an arc in quadrant 3
is an arc in quadrant 4
Finally, n refers to the nth fibonacci number Fn in the fibonacci sequence, which we take to be the length of the side of the square. Fibonacci numbers are defined by the sequence F1=1, F2=1, F3=2, F4=3, F5=5, F6=8, etc. The nth fibonacci number Fn of the sequence is defined as the sum of the n-1 and n-2 fibonacci numbers Fn-1 and Fn-2, i.e. Fn= Fn-1 + Fn-2. For instance, F4 = 3 = F3 + F2 = 2 + 1. Fibonacci numbers have fascinating properties which you can read about on wikipedia.
In hw4, you will create a tiling of the fibonacci squares to draw a fibonacci spiral.
- HShape: .
The constructor of the H shape should take 4 parameters: int x, int y, Color c, and int size.
(x, y) are the coordinates of the location of the shape (as for the fibonacci square, the precise location of the point with coordinates (x,y) is your choice).
c is the color the H shape
size is the length of the side of the square that contains the H shape.
To draw the H, divide the square that contains the H into 9 identical smaller squares
and color 7 of them as shown below.
In hw4, the H will be divided into smaller H's to create a fractal pattern.
The two classes FibonacciSquare and HShape should also have a method that returns a deep copy of its calling object (this is so that the model can return a deep copy of its lists of shapes. See below). Do so by implementing a method such as public Shape deepCopy() whose declaration could be written in the Shape interface. And of course, make sure that the copy is a deep copy.
You can also choose to make some other classes if you
like, such as diamonds, ovals, rectangles, composite shapes, etc.
- Class DrawingModel. This is the model that holds the state of the
drawing, and will probably be the most complex class. It should maintain
a collection of Shapes. The model should look at all of the shapes as having just a Shape type. It should not have to distinguish between Triangles and Squares (and other types of shapes if you have them). When requested by a view, the model should make a deep copy of the collection of its shapes ( use the deep copy method of the Shape interface implemented in the Triangle and Square classes). Other behaviors it should support are
- A client should be able to add a Shape to the model
- A viewer should be able to register with the model
- It should notify all viewers when something in the model changes.
(For now, the only changes will be when a new shape is added to the drawing.)
- Interface View. This interface should describe the minimal
information about a viewer needed for the DrawingModel to interact with it
(probably just an update method).
- A viewer class that extends JPanel and implements View. When
notified of changes in the DrawingModel, it should draw all shapes from the
model within the panel.
- MainClass program. Create a class with a main method that creates
the DrawingModel and Viewer and sets them up so that the state of the DrawingModel
is displayed within a JFrame. Create some fibonacci squares and H's
(and any other shapes you have) with different colors, sizes, and locations
and have them displayed to the user (by adding them to the model.) For the fibonacci squares, make sure that all four possible arcs are displayed.
Make sure you have documented your public interfaces well. Remember,
you are building these classes from scratch. No one has any idea of
what they do except you. You need to communicate these ideas to the
Written Report (typed as a pdf file):You must turn in a short typed report that discusses your program, describes the class
design, and discusses issues you encountered while working on it. Your report
- Planning: How did you plan and organize your program? What does it
- Implementation: How is your program organized? What are the major
classes? How do the objects interact? Draw a diagram.
- Testing: How did you test your code? What sort of bugs did you encounter?
Are there any unresolved problems in the code?
- Evaluate this project. What did you learn from it? Was it worth
the effort? This could include things you learned about specifications
and interfaces, design issues, Java language issues, debugging, etc.
Turn in your report as a pdf file along with your java files on the turn-in page.