Timeout1
File: java/Timeout1/Main.java
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;
private MyCanvas canvas;
public static void main (String [] args) {
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
timer.start();
}
public int getFrame () { return frame; }
// Like a clock tick
public void actionPerformed (ActionEvent e) {
frame++;
canvas.repaint ();
}
// Methods for KeyListener
public void keyPressed (KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ESCAPE) System.exit (0);
}
public void keyReleased (KeyEvent e) { }
public void keyTyped (KeyEvent e) { }
}
File: java/Timeout1/MyCanvas.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class MyCanvas extends JComponent {
private Main parent;
public MyCanvas (Main parent) {
this.parent = parent;
}
public void paintComponent (Graphics g) {
g.drawArc (
// Moves by 2 pixels on each frame
10 + 2 * parent.getFrame(),
10 + 2 * parent.getFrame(),
// size
100, 100,
// degrees
0, 360);
}
}