javaxsw i ngT i mer Timerdelay int listener ActionListener

Javaxsw i ngt i mer timerdelay int listener

This preview shows page 12 - 14 out of 24 pages.

javax.sw i ng.T i mer +Timer(delay: int, listener: ActionListener) +addActionListener(listener: ActionListener): void +start(): void +stop(): void +setDelay(delay: int): void Creates a Timer object with a specified delay in milliseconds and an ActionListener . Adds an ActionListener to the timer. Starts this timer. Stops this timer. Sets a new delay value for this timer. F IGURE 16.18 A Timer object fires an ActionEvent at a fixed rate. F IGURE 16.19 Two messages move in the panels. L ISTING 16.11 AnimationDemo.java 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class AnimationDemo extends JFrame { 6 public AnimationDemo() { 7 // Create two MovingMessagePanel for displaying two moving messages 8 this .setLayout( new GridLayout( 2 , 1 )); 9 add( new MovingMessagePanel( "message 1 moving?" )); 10 add( new MovingMessagePanel( "message 2 moving?" )); 11 } 12 13 /** Main method */ 14 public static void main(String[] args) { 15 AnimationDemo frame = new AnimationDemo(); 16 frame.setTitle( "AnimationDemo" ); 17 frame.setLocationRelativeTo( null ); // Center the frame 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setSize( 280 , 100 ); 20 frame.setVisible( true ); 21 } 22 23 // Inner class: Displaying a moving message 24 static class MovingMessagePanel extends JPanel { 25 private String message = "Welcome to Java" ; 26 private int xCoordinate = 0 ; 27 private int yCoordinate = 20 ; 28 private Timer timer = new Timer( 1000 , new TimerListener()); create message panel create timer
Image of page 12
16.11 Animation Using the Timer Class 627 29 30 public MovingMessagePanel(String message) { 31 this .message = message; 32 33 // Start timer for animation 34 35 36 // Control animation speed using mouse buttons 37 this .addMouseListener( new MouseAdapter() { 38 @Override 39 public void mouseClicked(MouseEvent e) { 40 int delay = timer.getDelay(); 41 if (e.getButton() == MouseEvent.BUTTON1) 42 43 else if (e.getButton() == MouseEvent.BUTTON3) 44 45 } 46 }); 47 } 48 49 @Override /** Paint the message */ 50 protected void paintComponent(Graphics g) { 51 super .paintComponent(g); 52 53 if (xCoordinate > getWidth()) { 54 xCoordinate = -20 ; 55 } 56 xCoordinate += 5 ; 57 g.drawString(message, xCoordinate, yCoordinate); 58 } 59 60 61 @Override 62 public void actionPerformed(ActionEvent e) { 63 repaint(); 64 } 65 } 66 } 67 } Two instances of MovingMessagePanel are created to display two messages (lines 9–10). The MovingMessagePanel class extends JPanel to display a message (line 24). This class is defined as an inner class inside the main class, because it is used only in this class. Further- more, the inner class is defined as static, because it does not reference any instance members of the main class. An inner class listener is defined in line 60 to listen for ActionEvent from a timer. Line 28 creates a Timer for the listener, and the timer is started in line 34. The timer fires an ActionEvent every 1 second initially, and the listener responds in line 62 to repaint the panel. When a panel is painted, its x -coordinate is increased (line 56), so the message is dis- played to the right. When the x -coordinate exceeds the bound of the panel, it is reset to -20 (line 54), so the message continues moving from left to right circularly.
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 24 pages?

  • Spring '15
  • Dr. Asim Noor
  • Computer keys, ActionEvent

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture