import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; public class TransAnim extends JApplet { public static void main(String args[]) { JFrame f = new JFrame("TransAnim"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); JApplet applet = new TransAnim(); f.getContentPane().add(applet, BorderLayout.CENTER); applet.init(); f.setSize(new Dimension(330,330)); f.setVisible(true); } public void init() { getContentPane().setLayout(new BorderLayout()); TransAnimPanel animator = new TransAnimPanel(); animator.setSize(330, 330); getContentPane().add(animator); animator.setVisible(true); animator.startAnimation(); } } class TransAnimPanel extends JPanel implements Runnable { Thread animatorThread; int delay; AffineTransform mytrans; Shape myshape; double theta=0.0; public TransAnimPanel() { mytrans = new AffineTransform(); myshape = new Arc2D.Float(-10,-10,20,20,45,270, Arc2D.PIE);// Its an Arc2D so you can see it spinning! delay = 100; } //Draw the current frame of animation. public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; theta +=0.1; mytrans.setToTranslation(150.0,150.0); // Rough center of the screen mytrans.rotate(theta); // Orbit angle g2.draw(mytrans.createTransformedShape(myshape)); } public void startAnimation() { //Start the animating thread. if (animatorThread == null) { animatorThread = new Thread(this); } animatorThread.start(); } public void stopAnimation() { //Stop the animating thread. animatorThread = null; } public void run() { //Just to be nice, lower this thread's priority //so it can't interfere with other processing going on. Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //Remember the starting time. long startTime = System.currentTimeMillis(); //Remember which thread we are. Thread currentThread = Thread.currentThread(); //This is the animation loop. while (currentThread == animatorThread) { //Advance the animation frame. //Display it. repaint(); //Delay depending on how far we are behind. try { startTime += delay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break; } } } }