1624 Control a clock Modify Listing 1612 ClockAnimationjava to add the two

1624 control a clock modify listing 1612

This preview shows page 35 - 38 out of 40 pages.

*16.24 ( Control a clock ) Modify Listing 16.12, ClockAnimation.java, to add the two methods start() and stop() to start and stop the clock. Write a program that lets the user control the clock with the Start and Stop buttons, as shown in Figure 16.26d.
Image of page 35
634 Chapter 16 Event-Driven Programming ***16.25 ( Game: hit balloons ) Write a program that displays a balloon in a random position in a panel (Figure 16.27a). Use the left- and right-arrow keys to point the gun left or right to aim at the balloon (Figure 16.27b). Press the up-arrow key to fire a small ball from the gun (Figure 16.27c–d). Once the ball hits the balloon, the debris is displayed (Figure 16.27e) and a new balloon is dis- played in a random location (Figure 16.27f). If the ball misses the balloon, the ball disappears once it hits the boundary of the panel. You can then press the up-arrow key to fire another ball. Whenever you press the left- or the right- arrow key, the gun turns 5 degrees left or right. (Instructors may modify the game as follows: 1. Display the number of the balloons destroyed; 2. display a countdown timer (e.g., 60 seconds) and terminate the game once the time expires; and/or 3. allow the balloon to rise dynamically.) **16.26 ( Move a circle using mouse ) Write a program that displays a circle with radius 10 pixels. You can point the mouse inside the circle and drag (i.e., move with mouse pressed) the circle wherever the mouse goes, as shown in Figure 16.28a–b. ***16.27 ( Game: eye-hand coordination ) Write a program that displays a circle of radius 10 pixels filled with a random color at a random location on a panel, as shown in Figure 16.28c. When you click the circle, it disappears and a new random- color circle is displayed at another random location. After twenty circles are clicked, display the time spent in the panel, as shown in Figure 16.28d. (a) (b) (c) (d) (e) (f) F IGURE 16.27 (a) A balloon is displayed in a random location. (b) Press the left-/right- arrow keys to aim at the balloon. (c) Press the up-arrow key to fire a ball. (d) The ball moves straight toward the balloon. (e) The ball hits the balloon. (f) A new balloon is displayed in a random position. (a) (b) (c) (d) F IGURE 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 random location. (d) After 20 circles are clicked, the time spent is displayed in the panel.
Image of page 36
Programming Exercises 635 ***16.28 ( Simulation: self-avoiding random walk ) A self-avoiding walk in a lattice is a path from one point to another that does not visit the same point twice. Self- avoiding walks have applications in physics, chemistry, and mathematics. They can be used to model chain-like entities such as solvents and polymers. Write a program that displays a random path that starts from the center and ends at a point on the boundary, as shown in Figure 16.29a, or ends at a dead- end point (i.e., surrounded by four points that have already been visited), as shown in Figure 16.29b. Assume the size of the lattice is 16 by 16 .
Image of page 37
Image of page 38

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture