Java Reference
In-Depth Information
that automatically displays the slides repeatedly. Each slide is shown for a sec-
ond. The slides are displayed in order. When the last slide finishes, the first
slide is redisplayed, and so on. ( Hint : Place a label in the frame and set a slide
as an image icon in the label.)
**16.14
( Raise flag ) Write a Java program that animates raising a flag, as shown in
Figure 16.1. (See Section 13.10, Displaying Images, for how to display images.)
**16.15
( Racing car ) Write a Java program that simulates car racing, as shown in
Figure 16.23a. The car moves from left to right. When it hits the right end, it
restarts from the left and continues the same process. You can use a timer to
control animation. Redraw the car with a new base coordinates (x, y), as
shown in Figure 16.23b. Also let the user pause/resume the animation with a
button press/release and increase/decrease the car speed by pressing the UP
and DOWN arrow keys.
VideoNote
Animate a rising flag
x
x+20
x+40
y-30
y-20
y-10
y
(x,y)
(a)
(b)
F IGURE 16.23
(a) Exercise 16.15 displays a moving car. (b) You can redraw a car with a new base point.
*16.16
( Display a flashing label ) Write a program that displays a flashing label. ( Hint :
To make the label flash, you need to repaint the panel alternately with the label
and without it (a blank screen) at a fixed rate. Use a boolean variable to control
the alternation.)
*16.17
( Control a moving label ) Modify Listing 16.11, AnimationDemo.java, to con-
trol a moving label using the mouse. The label freezes when the mouse is
pressed, and moves again when the button is released.
Comprehensive
*16.18
( Move a circle using keys ) Write a program that moves a circle up, down, left, or
right using the arrow keys.
**16.19
( Geometry: inside a circle? ) Write a program that draws a fixed circle centered
at ( 100 , 60 ) with radius 50 . Whenever the mouse is moved, display a message
indicating whether the mouse point is inside the circle at the mouse point or out-
side of it, as shown in Figure 16.24a.
VideoNote
Check mouse point location
(a)
(b)
(c)
F IGURE 16.24
Detect whether a point is inside a circle, a rectangle, or a polygon.
Search WWH ::




Custom Search