CSE 160
HW #11
Due Thursday April
30, 2009
Reading: Read Chapter 11 in the Objects
First
text. Work along
with it, using BlueJ. You will also find the Java
Tutorial on Swing to be very helpful.
HW: The first exercise expands
on
the frame animation diversion we made in class. The second extends the
image viewer.
A) Moving Frame.
1) Peruse the JavaDocs for the GUI components discussed in the text and
in class, including JFrame, JLabel, JButton, JMenu, etc. With the
ImageViewer0.3 from the text book as your starting point, create an
application with a frame that has five menus, a label, and a
button. The button says "Go" and the label initially says "Small
fast counterclockwise square".
- The leftmost menu is labeled "File" and has one menu item:
"Exit".
- The second menu is labeled "Size" and has two menu items: "Big"
and "Small". Clicking either causes the first word of the label to
change accordingly.
- The third menu item is labeled "Speed" and has two menu items:
"Fast" and "Slow". Clicking either causes the third word of the label
to change accordingly.
- The fourth menu item is labeled "Direction" and has two menu
items: "Clockwise"
and "Counterclockwise". Clicking either causes the fourth word of the
label to
change accordingly.
- The fifth menu item is labeled "Shape" and has two menu items:
"Circle"
and "Square". Clicking either causes the fourth word of the label to
change accordingly.
- When the Go button is clicked, the entire frame moves around the
screen in a big or small circle or square, ending where it started. It
moves at a speed and in a direction according to the displayed text.
When the menu items are clicked, the frame does not move, but the state
records how it should move for the next time that the Go button is
clicked.
Paste the following method into your class and use it to control the
speed. The try, catch, and exception bits
will be explained in a future chapter.
/**
* Time delay puts program to sleep, for pause
between animation frames
*
* @param n number of
milliseconds to sleep
*/
public void sleep(int n)
{
try { Thread.sleep(n);
}
catch (Exception e) {}
}
You may find the static methods Math.sin and Math.cos
to be useful.
For extra credit, add another interesting user-controllable behavior to
its motion (but don't eliminate any of the above capabilities).
B) Image Viewer extensions
In CH 11 of the text, do exercises 11.37 (mirror, p. 242) and 11.39
(smoothing, p. 243).
Submit your .jar files for parts A and B.