Timeout1

Main.java  [download]

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class Main extends JFrame
implements ActionListener, KeyListener {
    /**
     * State of the animation, just a counter
     */
    private int frame = 1; //*1 Frame counter
    
    private MyCanvas canvas;

    public static void main (String [] args) {
	java.awt.EventQueue.invokeLater (new Runnable() {
            public void run() {
		new Main ();
            }
        });
    }

    public Main () {
	// Window setup
	setSize (500, 500);
	Container content = getContentPane();
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	addKeyListener (this);

	// Put a Canvas in
	canvas = new MyCanvas (this);
	content.add (canvas);

	setVisible (true);

	// Start timer
	Timer timer = new Timer (100, this); // 100 milliseconds //*2 Timer
	timer.start(); //*2
    }

    public int getFrame () { return frame; } //*1

    // Like a clock tick
    public void actionPerformed (ActionEvent e) { //*2
	frame++; //*2
	canvas.repaint (); //*2
    }

    // Methods for KeyListener
    public void keyPressed (KeyEvent e) { //*3 KeyListener
	if (e.getKeyCode()==KeyEvent.VK_ESCAPE) System.exit (0); //*3
    }
    public void keyReleased (KeyEvent e) { }
    public void keyTyped (KeyEvent e) { }

}

MyCanvas.java  [download]

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class MyCanvas extends JPanel {
    private Main parent; //*1 Saves pointer to parent

    public MyCanvas (Main parent) {
	this.parent = parent; //*1
    }

    public void paintComponent (Graphics g) {
	super.paintComponent(g);
	g.drawArc ( //*2 Redraws circle for current frame number
		// Moves by 2 pixels on each frame
		10 + 2 * parent.getFrame(), //*3 Call parent to find current frame number
		10 + 2 * parent.getFrame(), //*3
		// size
		100, 100,
		// degrees
		0, 360);
    }
}