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.

16.11 Animation Using the Timer Class 627<br />

29<br />

30 public MovingMessagePanel(String message) {<br />

31 this.message = message;<br />

32<br />

33 // Start timer for animation<br />

34 timer.start();<br />

35<br />

36 // Control animation speed using mouse but<strong>to</strong>ns<br />

37 this.addMouseListener(new MouseAdapter() {<br />

38 @Override<br />

39 public void mouseClicked(MouseEvent e) {<br />

40 int delay = timer.getDelay();<br />

41 if (e.getBut<strong>to</strong>n() == MouseEvent.BUTTON1)<br />

42<br />

43<br />

timer.setDelay(delay > 10 ? delay - 10 : 0);<br />

else if (e.getBut<strong>to</strong>n() == MouseEvent.BUTTON3)<br />

44<br />

45<br />

timer.setDelay(delay < 50000 ? delay + 10 : 50000);<br />

}<br />

46 });<br />

47 }<br />

48<br />

49 @Override /** Paint the message */<br />

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

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

52<br />

53 if (xCoordinate > getWidth()) {<br />

54 xCoordinate = -20;<br />

55 }<br />

56 xCoordinate += 5;<br />

57 g.drawString(message, xCoordinate, yCoordinate);<br />

58 }<br />

59<br />

60<br />

61<br />

class TimerListener implements ActionListener {<br />

@Override<br />

62 public void actionPerformed(ActionEvent e) {<br />

63 repaint();<br />

64 }<br />

65 }<br />

66 }<br />

67 }<br />

set message<br />

start timer<br />

mouse listener<br />

reset x-coordinate<br />

move message<br />

listener class<br />

event handler<br />

repaint<br />

Two instances of MovingMessagePanel are created <strong>to</strong> display two messages (lines 9–10).<br />

The MovingMessagePanel class extends JPanel <strong>to</strong> display a message (line 24). This class<br />

is defined as an inner class inside the main class, because it is used only in this class. Furthermore,<br />

the inner class is defined as static, because it does not reference any instance members<br />

of the main class.<br />

An inner class listener is defined in line 60 <strong>to</strong> listen for ActionEvent from a timer. Line<br />

28 creates a Timer for the listener, and the timer is started in line 34. The timer fires an<br />

ActionEvent every 1 second initially, and the listener responds in line 62 <strong>to</strong> repaint the<br />

panel. When a panel is painted, its x-coordinate is increased (line 56), so the message is displayed<br />

<strong>to</strong> the right. When the x-coordinate exceeds the bound of the panel, it is reset <strong>to</strong> -20<br />

(line 54), so the message continues moving from left <strong>to</strong> right circularly.<br />

A mouse listener is registered with the panel <strong>to</strong> listen for the mouse click event (lines<br />

37–46). When the left mouse but<strong>to</strong>n is clicked, a new reduced delay time is set for the timer<br />

(lines 41–42). When the right mouse but<strong>to</strong>n is clicked, a new increased delay time is set for<br />

the timer (lines 43–44). The minimum delay time is 0 and the maximum can be<br />

Integer.MAX_VALUE, but it is set <strong>to</strong> 50000 in this program (line 44).<br />

In Section 13.9, Case Study: The StillClock Class, you drew a StillClock <strong>to</strong> show<br />

the current time. The clock does not tick after it is displayed. What can you do <strong>to</strong> make the

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

Saved successfully!

Ooh no, something went wrong!