// Rotate.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Rotate extends JPanel
implements ActionListener {
//Set up animation parameters.
static final int FPS_INIT = 30; //initial frames per second
int radius = 70;
double theta = 0.0;
double delta = 360.0/120;
int x, y;
int delay;
Timer timer;
boolean frozen = false;
static ImageIcon picture;
public Rotate() {
delay = 1000 / FPS_INIT;
//Set up a timer that calls this object's action handler.
timer = new Timer(delay, this);
timer.setInitialDelay(0);
timer.setCoalesce(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
picture.paintIcon(this,g,x,y);
}
public void startAnimation() {
//Start (or restart) animating!
timer.start();
frozen = false;
}
public void stopAnimation() {
//Stop the animating thread.
timer.stop();
frozen = true;
}
//Called when the Timer fires
public void actionPerformed(ActionEvent e) {
final double radians_per_degree=0.017453292;
double arc;
Dimension rect = getSize();
theta += delta;
if (theta>360.0+delta/2.0) theta = delta;
arc = radians_per_degree*theta;
x = (int)(rect.width/2 + radius*Math.cos(arc));
y = (int)(rect.height/2 + radius*Math.sin(arc));
x -= picture.getIconWidth()/2;
y -= picture.getIconHeight()/2;
repaint();
}
public static void main(String[] args)
{
String str = (args.length>0? args[0]: "java_85.gif");
picture = new ImageIcon(str);
if (picture==null) System.exit(1);
final Rotate animator = new Rotate();
JFrame frame = new JFrame("Rotate");
frame.setContentPane(animator);
//Add a listener for window events
frame.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
animator.stopAnimation();
}
public void windowDeiconified(WindowEvent e) {
animator.startAnimation();
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300,300);
frame.show();
animator.startAnimation();
}
}
Source: Rotate.java
Default image:
Maintained by John Loomis, last updated 20 June 2000