CPSC 170 PostLab 5
Drawing Circles with the Mouse
Due Monday, February 24
File Circles.java sets up a GUI that
creates and draws a circle as defined in Circle.java
of random size and color at each mouse click. Each
circle replaces the one before it. The code to handle the mouse
clicks and do the drawing is in CirclePanel.java.
Save these files to your directory,
compile them and run them and experiment with the GUI.
Then modify these files as described below.
- This program creates a new circle each time -- you can
tell because each circle is a different color and
size. Write
a method void move(Point p) for your Circle class that takes a Point
and moves the circle so its center is at
that point. Now modify your CirclesListener class (defined inside
CirclePanel) so that
instead of creating a new circle every time the user clicks,
it moves the existing circle to the current point if a circle already exists.
If
no circle exists, a new one should be created at the current point.
So now a circle of the same color and size should
move around the screen.
- Write a method boolean isInside(Point p) for your Circle class
that takes a Point and
tells whether it is inside the circle. A point is inside the
circle if its distance from the center is less than the radius. (Recall that the distance
between two points (x1,y1) and (x2,y2) is
sqrt((x2-x1)2+(y2-y1)2.)
- Now modify the mouseClicked method
of CirclesListener so that the GUI
behaves as follows:
- If there is no circle (i.e., it is null) and the user clicks
anywhere,
a new (random) circle should be drawn at that point.
- If there is a circle on the screen and the user clicks
inside that circle,
the circle should go away. (Hint: To make the circle go away, set it to
null and repaint.)
- If there is a circle on the screen and the user clicks
somewhere else,
the circle should move to that point (no change from before).
Notice that exactly one of these situations will occur at each mouse click.
- Add bodies for the mouseEntered and mouseExited methods
so that when the mouse enters the applet the background turns white,
and when
it exits the background turns blue. Remember that you can set the background
color with the setBackground method.
Turn in hardcopy of Circles.java, Circle.java, and CirclePanel.java. Tar
your post5 directory and e-mail it to me with cpsc170 post5 in the
Subject line.