import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AnimationDemo extends JFrame {
  /* Inner class extending JPanel to draw message */
  static class MovingMessagePanel extends JPanel {
    private String message;
    private int xCoordinate = 0;
    private int yCoordinate = 20;

    public MovingMessagePanel(String message) {
      this.message = message;

      // Create a timer, add listener for timer ActionEvents
      Timer timer = new Timer(1000, new TimerListener());
      timer.start();
    }

    /** Inner Class to Handle ActionEvent */
    class TimerListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        // Note: this is getWidth() of the AnimationDemo (JFrame) class.
        if (xCoordinate > getWidth()) {
          xCoordinate = 0;
        } else {
            xCoordinate += 10;
        }
        System.out.println(e.getWhen() + " " + e.getSource());
        repaint();
      }
    }

    /** Paint message */
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
          g.drawString(message, xCoordinate, yCoordinate);
    }

  }

  // Constructor
  public AnimationDemo() {
    add(new MovingMessagePanel("Message on the move"));
 
    setTitle("AnimationDemo");
    setLocationRelativeTo(null); // Center the frame
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 90);
  }

  /** Main method */
  public static void main(String[] args) {
    AnimationDemo frame = new AnimationDemo();
    frame.setVisible(true);
  }

}
