/* Simple animation with swing

   Copyright 2010-2011 Cheng Zhang

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   A copy of the GNU General Public License can be found in
   <http://www.gnu.org/licenses/>.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// a simple Swing animator that prints A, B, C recurrently
public class ABC extends JApplet implements ActionListener {
  int step = -1;
  Timer timer;
  MyCanvas canvas; // applet's content panel

  void init_gui() {
    canvas = new MyCanvas();
    canvas.setBackground(getBackground());
    setContentPane(canvas);
  }

  public void init() {
    try {
      SwingUtilities.invokeAndWait(new Runnable() {
        public void run() { init_gui(); }
      }); // start a thread
    } catch (Exception e) { return; }

    // create a timer every 300ms
    timer = new Timer(300, this); //timer.setInitialDelay(1900);
    timer.start();
  }

  // handle timer
  public void actionPerformed(ActionEvent e) {
    step++;
    canvas.step = step;
    canvas.repaint();
  }
  public void start() { timer.restart(); }
  public void stop() { timer.stop(); }
}

class MyCanvas extends JPanel {
  public int step;
  public MyCanvas() { super(); }
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (step < 0) return;
    String s = "" + (char)('A' + step % 3);
    g.drawString(s, 10, 10);
  }
}

