Java Reference
In-Depth Information
8.
Java provides two listener interfaces,
MouseListener
and
MouseMotionListener
,
to handle mouse events. Java implements the
MouseListener
interface to listen
for such actions as mouse pressed, released, clicked, entered, or exited, and the
MouseMotionListener
interface to listen for such actions as mouse dragged or
moved.
9.
A
KeyEvent
is fired when a key is pressed, released, or typed. The key value and key
character can be obtained from the key-event object.
10.
A listener's
keyPressed
handler is invoked when a key is pressed, its
keyReleased
handler is invoked when a key is released, and its
keyTyped
handler
is invoked when a Unicode character key is entered. If a key does not have a Unicode
(e.g., function keys, modifier keys, action keys, and control keys), a listener's
keyTyped
handler will be not be invoked.
11.
You can use the
Timer
class to control Java animations. A timer fires an
ActionEvent
at a fixed rate. The listener updates the painting to simulate an animation.
T
EST
Q
UESTIONS
P
ROGRAMMING
E
XERCISES
Sections 16.2-16.7
*16.1
(
Pick four cards
) Write a program that lets the user click the
Refresh
button to dis-
play four cards from a deck of 52 cards, as shown in Figure 16.21a. (
Hint
: See
Listing 6.2 on how to draw four cards randomly.)
(b)
F
IGURE
16.21
(a) Exercise 16.1 displays four cards randomly. (b) Exercise 16.3 uses the buttons to move the ball.
(c) Exercise 16.4 performs addition, subtraction, multiplication, and division on double numbers.
(a)
(c)
16.2
(
Find which button has been clicked on the console
) Add the code to Programming
Exercise 12.1 that will display a message on the console indicating which button
has been clicked.
*16.3
(
Move the ball
) Write a program that moves the ball in a panel. You should define
a panel class for displaying the ball and provide the methods for moving the ball
left, right, up, and down, as shown in Figure 16.21b. Check the boundary to prevent
the ball from moving out of sight completely.
*16.4
(
Create a simple calculator
) Write a program to perform addition, subtraction,
multiplication, and division, as shown in Figure 16.21c.