19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

634 Chapter 16 Event-Driven <strong>Programming</strong><br />

***16.25 (Game: hit balloons) Write a program that displays a balloon in a random<br />

position in a panel (Figure 16.27a). Use the left- and right-arrow keys <strong>to</strong> point<br />

the gun left or right <strong>to</strong> aim at the balloon (Figure 16.27b). Press the up-arrow<br />

key <strong>to</strong> fire a small ball from the gun (Figure 16.27c–d). Once the ball hits the<br />

balloon, the debris is displayed (Figure 16.27e) and a new balloon is displayed<br />

in a random location (Figure 16.27f). If the ball misses the balloon, the<br />

ball disappears once it hits the boundary of the panel. You can then press the<br />

up-arrow key <strong>to</strong> fire another ball. Whenever you press the left- or the rightarrow<br />

key, the gun turns 5 degrees left or right. (Instruc<strong>to</strong>rs may modify the<br />

game as follows: 1. Display the number of the balloons destroyed; 2. display<br />

a countdown timer (e.g., 60 seconds) and terminate the game once the time<br />

expires; and/or 3. allow the balloon <strong>to</strong> rise dynamically.)<br />

(a) (b) (c)<br />

(d) (e) (f)<br />

FIGURE 16.27 (a) A balloon is displayed in a random location. (b) Press the left-/rightarrow<br />

keys <strong>to</strong> aim at the balloon. (c) Press the up-arrow key <strong>to</strong> fire a ball. (d) The ball moves<br />

straight <strong>to</strong>ward the balloon. (e) The ball hits the balloon. (f) A new balloon is displayed in a<br />

random position.<br />

**16.26 (Move a circle using mouse) Write a program that displays a circle with radius 10<br />

pixels. You can point the mouse inside the circle and drag (i.e., move with mouse<br />

pressed) the circle wherever the mouse goes, as shown in Figure 16.28a–b.<br />

(a) (b) (c) (d)<br />

FIGURE 16.28 (a–b) You can point, drag, and move the circle. (c) When you click a circle, a new circle is displayed at a<br />

random location. (d) After 20 circles are clicked, the time spent is displayed in the panel.<br />

***16.27 (Game: eye-hand coordination) Write a program that displays a circle of radius<br />

10 pixels filled with a random color at a random location on a panel, as shown<br />

in Figure 16.28c. When you click the circle, it disappears and a new randomcolor<br />

circle is displayed at another random location. After twenty circles are<br />

clicked, display the time spent in the panel, as shown in Figure 16.28d.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!