CPSC 170 Spring 2004
Program 2: Building Blocks
In this assignment you will write a program with a nice graphical
user interface that allows the user to
create, rearrange, and delete multicolored shapes. Your program should
have the following capabilities:
- A row of color buttons that allow the user to change the color of the
next shape(s) drawn. When a color button is selected it should somehow
be distinguished from the others (e.g., its background might change
color), and the shapes created should be
that color until a different color is selected. Previously created shapes
do not change color. There should be at least 7 color choices,
and it should be easy to extend this number.
- A row of shape buttons that allow the user to change the shape that will
be drawn. As for the color buttons, the selected shape button should be
somehow distinguished and will determine what shape is drawn next. There
at least 3 shape choices, and it should be easy to extend this number.
- A delete button. As for the others, this button should
be distinguished when it has
been selected. When the delete button is selected, any shape that is clicked
in should go away.
- When the delete button is not selected, the user should be able to
click in any shape and drag it around the window. If two shapes are overlapping and the overlapping portion is clicked in,
only the top shape should get dragged.
So when the GUI starts, the user sees some buttons and an otherwise blank
window. The user creates shapes by clicking in the window, changing color
and shape as desired. The user moves a shape by pressing the mouse button
in that shape and dragging it.
Structure your program as follows:
- Store the color buttons and shape buttons and their
associated information in arrays. This makes
for clean processing
and makes it easy to add more options if you wish.
- Write a Shape class that represents any kind of shape, and write
subclasses for each of the shapes you allow in your program. Think
carefully about what methods should go where -- put as much functionality
as you can in the Shape class to be inherited by the others.
- Each of your shapes
can be a uniform size -- for example, a square might always be 100 pixels
on a side. Thus the size need not be an option for the user.
- You may find it convenient to store your shapes in an ArrayList object.
We will discuss ArrayLists in class, and you can read more
in the online
- You can approximate the "isInside" calculation for your shapes by
saying that anything inside the smallest bounding rectangle is inside the
shape. You may want to do better when you can (e.g., for a circle),
but this makes it easier for you to define a variety of irregular shapes.
START EARLY!! I know I always say this, but I mean it! Do this project
a piece at a time, testing carefully as you go. GUIs can get big and
rather messy, so be sure you break your program into logical pieces and
carefully document methods, classes, and logical portions of code. And
remember, just because it works doesn't mean it's well written; a significant
portion of your grade will depend on good programming style.