Java Reference
In-Depth Information
fied in milliseconds). Modify your solution to Exercise 13.18 to remove the call to
repaint
from
method
paintComponent
. Declare your class to implement
ActionListener
. (The
actionPerformed
method should simply call
repaint
.) Declare an instance variable of type
Timer
called
timer
in your
class. In the constructor for your class, write the following statements:
timer =
new
Timer(
1000
,
this
);
timer.start();
This creates an instance of class
Timer
that will call
this
object's
actionPerformed
method every
1000
milliseconds (i.e., every second).
13.20
(Screen Saver for a Random Number of Lines)
Modify your solution to Exercise 13.19 to
enable the user to enter the number of random lines that should be drawn before the application
clears itself and starts drawing lines again. Use a
JTextField
to obtain the value. The user should be
able to type a new number into the
JTextField
at any time during the program's execution. Use an
inner class to perform event handling for the
JTextField
.
13.21
(Screen Saver with Shapes)
Modify your solution to Exercise 13.20 such that it uses ran-
dom-number generation to choose different shapes to display. Use methods of class
Graphics
.
13.22
(Screen Saver Using the Java 2D API)
Modify your solution to Exercise 13.21 to use classes
and drawing capabilities of the Java 2D API. Draw shapes like rectangles and ellipses, with randomly
generated gradients. Use class
GradientPaint
to generate the gradient.
13.23
(Turtle Graphics)
Modify your solution to Exercise 7.21—
Turtle Graphics
—to add a
graphical user interface using
JTextField
s and
JButton
s. Draw lines rather than asterisks (
*
). When
the turtle graphics program specifies a move, translate the number of positions into a number of
pixels on the screen by multiplying the number of positions by 10 (or any value you choose). Im-
plement the drawing with Java 2D API features.
13.24
(Knight's Tour)
Produce a graphical version of the Knight's Tour problem (Exercise 7.22,
Exercise 7.23 and Exercise 7.26). As each move is made, the appropriate cell of the chessboard
should be updated with the proper move number. If the result of the program is a
full tour
or a
closed
tour
, the program should display an appropriate message. If you like, use class
Timer
(see
Exercise 13.19) to help animate the Knight's Tour.
13.25
(Tortoise and Hare)
Produce a graphical version of the
To r t o i s e a n d H a r e
simulation
(Exercise 7.28). Simulate the mountain by drawing an arc that extends from the bottom-left corner
of the window to the top-right corner. The tortoise and the hare should race up the mountain. Im-
plement the graphical output to actually print the tortoise and the hare on the arc for every move.
[
Hint:
Extend the length of the race from 70 to 300 to allow yourself a larger graphics area.]
13.26
(Drawing Spirals)
Write an application that uses
Graphics
method
drawPolyline
to draw
a spiral similar to the one shown in Fig. 13.33.
13.27
(Pie Chart)
Write a program that inputs four numbers and graphs them as a pie chart. Use
class
Arc2D.Double
and method
fill
of class
Graphics2D
to perform the drawing. Draw each piece
of the pie in a separate color.
13.28
(Selecting Shapes)
Write an application that allows the user to select a shape from a
JCombo-
Box
and draws it 20 times with random locations and dimensions in method
paintComponent
. The
first item in the
JComboBox
should be the default shape that is displayed the first time
paintCompo-
nent
is called.
13.29
(Random Colors)
Modify Exercise 13.28 to draw each of the 20 randomly sized shapes in a
randomly selected color. Use all 13 predefined
Color
objects in an array of
Color
s.
13.30
(
JColorChooser
Dialog)
Modify Exercise 13.28 to allow the user to select the color in
which shapes should be drawn from a
JColorChooser
dialog.