Interactive Shapes
CPS 004.1, 19 July 2002
You are to write a Java applet that draws shapes in reaction to being clicked on. When you
add a Behavior to any kind of shape, GP automatically calls its
Step method repeatedly (until the
behavior is done). Similarly, you can add a Reaction
to any kind of shape that allows it to react to
someone using your applet (e.g., by clicking the mouse button or pressing a key). Once you add a
Reaction to a shape, GP will automatically call its
React method when the given event occurs within
the shape. Note, if you add a reaction directly to the applet, the
React method will be called when an
event occurs within the applet, but outside of any other shape visible in the applet.
This exercise is meant to familiarize you with the following concepts discussed in class and your
reading:
- Interacting with the user of your program
- Randomness
Specifications
You are to make an applet that creates a shape centered at the mouse's current
position when the user clicks the mouse button within the applet (but outside of any other shape). The particular shape created should depend on where
in the applet this mouse was clicked. (This simplest example is that
an oval is created if clicked in the top half and a rectangle is created
if clicked in the bottom half.)
Different kinds of shapes should
react differently when clicked upon. For example, a rectangle may change color when clicked, a
triangle may spin around, and an oval may create a second shape that falls to the bottom of the
applet.
You can be creative in determining how each shape responds to the user, but you must implement at
least one of each of the following kinds of reactions:
- changes an attribute of the shape (e.g., darkens it, turns it, moves it, grows it, hides it, etc.)
- adds a conditional (not perpetual) behavior to the shape (i.e., spins it once around, explodes
(grows) it to a specific size, moves it around in a circle)
- creates a new kind of shape that has a behavior (i.e., a spinning triangle, a falling drop, etc.)
On your applet's web page, you should provide directions that tell the user what behavior to expect
when clicking on each shape.
An example can be seen online
here.
A simpler example is worked out
here.
Note, the React method for an object that extends
GP.Reactions.MouseClicked
is never called directly within your code; instead it is called by GP only
when the user has pressed and released the mouse button within the given shape to which you have
added the reaction.
Extra Credit
You are to make an applet that creates a random kind of shape centered at the mouse's current
position when the user clicks the mouse button within the applet (but outside of any other shape). The
shape's other attributes should also be randomly chosen.
As above, different kinds of shapes should react differently when clicked upon.
What to Submit
When you are finished, you and your partner should transfer the entire project folder created by
Visual J++ to your respective public_html/cps4 folder in the acpub system and update your course web
page to link to the newly transferred project using the guidelines given
here.