19.09.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16.11 Animation Using the Timer Class 625<br />

68<br />

69 /** Enlarge the circle */<br />

70 public void enlarge() {<br />

71 radius++;<br />

72 repaint();<br />

73 }<br />

74<br />

75 /** Shrink the circle */<br />

76 public void shrink() {<br />

77 if (radius >= 1) radius— –;<br />

78 repaint();<br />

79 }<br />

80<br />

81 @Override<br />

82 protected void paintComponent(Graphics g) {<br />

83 super.paintComponent(g);<br />

84 g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius,<br />

85 2 * radius, 2 * radius);<br />

86 }<br />

87 }<br />

88 }<br />

A listener for MouseEvent is created <strong>to</strong> handle mouse-clicked events in lines 34–42. If the<br />

left mouse but<strong>to</strong>n is clicked, the circle is enlarged (lines 37–38); if the right mouse but<strong>to</strong>n is<br />

clicked, the circle is shrunk (lines 39–40).<br />

A listener for KeyEvent is created <strong>to</strong> handle key-pressed events in lines 45–53. If the UP<br />

arrow key is pressed, the circle is enlarged (lines 48–49); if the DOWN arrow key is pressed,<br />

the circle is shrunk (lines 50–51).<br />

Invoking setFocusable on canvas makes canvas focusable. However, once a but<strong>to</strong>n is<br />

clicked, the canvas is no longer focused. Invoking canvas.requestFocusInWindow()<br />

(lines 22, 30) resets the focus on canvas so that canvas can listen for key events.<br />

16.18 What method do you use <strong>to</strong> get the timestamp for an action event, a mouse event, or<br />

a key event?<br />

16.19 What method do you use <strong>to</strong> get the key character for a key event?<br />

16.20 How do you set focus on a <strong>com</strong>ponent so it can listen for key events?<br />

16.21 Does every key in the keyboard have a Unicode? Is a key code in the KeyEvent class<br />

equivalent <strong>to</strong> a Unicode?<br />

16.22 Is the keyPressed handler invoked after a key is pressed? Is the keyReleased<br />

handler invoked after a key is released? Is the keyTyped handler invoked after any<br />

key is typed?<br />

16.11 Animation Using the Timer Class<br />

A Timer is a source object that fires ActionEvent at a fixed rate.<br />

Not all source objects are GUI <strong>com</strong>ponents. The javax.swing.Timer class is a source<br />

<strong>com</strong>ponent that fires an ActionEvent at a predefined rate. Figure 16.18 lists some of the<br />

methods in the class.<br />

A Timer object serves as the source of an ActionEvent. The listeners must be instances<br />

of ActionListener and registered with a Timer object. You create a Timer object using its<br />

sole construc<strong>to</strong>r with a delay and a listener, where delay specifies the number of milliseconds<br />

between two action events. You can add additional listeners using the addActionListener<br />

method and adjust the delay using the setDelay method. To start the timer, invoke the<br />

start() method; <strong>to</strong> s<strong>to</strong>p the timer, invoke the s<strong>to</strong>p() method.<br />

MouseEvent<br />

KeyEvent<br />

setFocusable<br />

requestFocusInWindow()<br />

✓Point✓ Check<br />

Key<br />

Point

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

Saved successfully!

Ooh no, something went wrong!