Java Reference
In-Depth Information
**16.20
(
Geometry: inside a rectangle?
) Write a program that draws a fixed rectangle
centered at (
100
,
60
) with width
100
and height
40
. Whenever the mouse is
moved, display a message indicating whether the mouse point is inside the
rectangle at the mouse point or outside of it, as shown in Figure 16.24b. To
detect whether a point is inside a rectangle, use the
MyRectangle2D
class
defined in Programming Exercise 10.13.
**16.21
(
Geometry: inside a polygon?
) Write a program that draws a fixed polygon with
points at (
40
,
20
), (
70
,
40
), (
60
,
80
), (
45
,
45
), and (
20
,
60
). Whenever the
mouse is moved, display a message indicating whether the mouse point is inside
the polygon at the mouse point or outside of it, as shown in Figure 16.24c. To
detect whether a point is inside a polygon, use the
contains
method defined in
the
Polygon
class (see Figure 13.13).
***16.22
(
Game: bean-machine animation
) Write a program that animates the bean
machine introduced in Programming Exercise 6.21. The animation terminates
after ten balls are dropped, as shown in Figure 16.25.
F
IGURE
16.25
The balls are dropped into the bean machine.
***16.23
(
Geometry: closest pair of points
) Write a program that lets the user click on
the panel to dynamically create points. Initially, the panel is empty. When a
panel has two or more points, highlight the pair of closest points. Whenever a
new point is created, a new pair of closest points is highlighted. Display the
points using small circles and highlight the points using filled circles, as
shown in Figure 16.26a-c. (
Hint
: store the points in an
ArrayList
.)
(a)
(b)
(c)
(d)
F
IGURE
16.26
Exercise 16.23 allows the user to create new points with a mouse click and highlights the pair of the closest
points. Exercise 16.24 allows the user to start and stop a clock.
*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.