CPSC 170 Post Lab 3: Drawing Lines
Due Tuesday, Feb 12
Problem (derived from Lewis and Loftus 6.15)
Write an applet that draws a polyline shape
using mouse clicks, where each click adds a new line segment from the
last point. Specifically, your applet should
behave as follows:
- When the user clicks the first time, nothing should be drawn (although the point is
recorded).
- When the user clicks again, a line should be drawn from the
first click to the second click.
- On each successive click, a new segment should be drawn from the last
click to the current click. So the user is building a (crooked) line based
on click points.
- If the user clicks close to end of the last segment in the line,
that segment
should go away.
Play around to see what works well for "close." It's hard
for the user to click directly on the point; within a few pixels in any direction works better.
This should work to remove successive segments, so the user could erase his
or her entire line this way.
Try my demo to be sure you understand these
behaviors.
Program Design
Your program must do the following:
- Use the drawPolyline method to draw the line.
- Start with 10-element arrays of x and y
coordinates.
Whenever the user's line exceeds the capacity of the arrays, double
the size of the arrays. (Recall that the CD example in the book does this.)
Do not increase the size of the arrays every time a point is added.
Notice that this means that you will need to keep track of the number of
points in the line separately instead of passing the length of the
arrays to the drawPolyline method.
I strongly advise that you proceed as follows:
- Write an applet that draws a polyline from the clicks entered by the
user. Don't deal with the number of points exceeding 10 or removing the last segment.
Recall that to listen for mouse events you
will have to do the following:
- Note that the applet implements the MouseListener interface in the class
header.
- Use addMouseListener to add
the current object as a MouseListener in init().
- Write a method public void mouseClicked(MouseEvent e) that contains
the code you want to execute when the mouse is clicked.
- Add null bodies for the rest of the MouseListener methods --
mousePressed, mouseReleased, mouseEntered, and
mouseExited.
To refresh your memory on how an applet that handles mouse clicks works,
look at
Dots.java
from lab 11 in CPSC 120 -- it contains all of the elements above.
- Now make the arrays double in size when
the user exceeds the capacity of the
current arrays. Since you know the
size of the arrays and the number of
points, this is easy to check for when the user
clicks. Just write a method increaseSize that increases the capacity of the arrays
that define the polyline. This is very much
like what the CDCollection class does.
- Finally add the feature where clicking near the last point removes
the last line. To do this you will need to do the following:
- In mouseClicked, determine whether the current click is close enough
to the last point in the line. Note that the x and y coordinates of the
last point are stored in the x and y arrays. But careful: it's easy to
get an ArrayIndexOutOfBounds exception here.
- If the current click is close to the last point, decrement the number of
points. This will automatically make the last segment go away; you don't have
to change anything in the arrays.
- If the current click is not close to the last point, continue as
before.
As always, document each of your methods and
any large or unclear sections of code, and
include documentation at the top that gives an overview
of the applet.
What to turn in
Turn in hardcopy and e-mail a
tar file of your postlab directory to
bloss@cs.roanoke.edu. IMPORTANT:
Put "Postlab 3" (no quotes) in the subject.