/****************************************************************** * @(#) Animator3.java 1.0 * * Copyright (c) 2007 John Miller * All Right Reserved *----------------------------------------------------------------- * Permission to use, copy, modify and distribute this software and * its documentation without fee is hereby granted provided that * this copyright notice appears in all copies. * WE MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WE SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY ANY USER AS A RESULT OF USING, * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. *----------------------------------------------------------------- * * @version 1.0, 13 November 2007 * @author John Miller */ import static java.lang.System.*; import static java.lang.Math.*; import java.awt.*; import java.awt.geom.*; import javax.swing.*; /****************************************************************** * The Animator class demonstrates how the Java 2D API shapes can * be put into motion to create a simple animation. */ public class Animator3 extends JFrame implements Runnable { private static final Dimension dim = new Dimension (600, 500); private static final int tau = 20; // at 50 Hz private final Thread displayer; private final Ellipse2D.Double circle; private final Ellipse2D.Double ball; private final Point2D.Double ballPos; /************************************************************** * The DisplayPanel inner class is used to place shapes in the * drawing region. */ public class DisplayPanel extends JPanel { /********************************************************** * Paint the display panel component. * @param gr The graphics context. */ public void paintComponent (Graphics gr) { super.paintComponent (gr); Graphics2D gr2 = (Graphics2D) gr; // use hi-res gr2.setPaint (Color.blue); gr2.draw (circle); gr2.setPaint (Color.red); ball.setFrame (ballPos.x - 10, ballPos.y - 10, 20, 20); gr2.fill (ball); } // paintComponent } // DisplayPanel inner class /************************************************************** * Construct an animator frame. * @param title Title of frame */ public Animator3 (String title) { super (title); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); circle = new Ellipse2D.Double (200, 200, 200, 200); ballPos = new Point2D.Double (0, 300); ball = new Ellipse2D.Double (); getContentPane ().add (new DisplayPanel ()); setLocation (100, 100); setSize (dim); setVisible (true); displayer = new Thread (this); displayer.start (); } // Animator3 /************************************************************** * Run method for the display thread. * Repeatedly update coordinates, sleep and repaint. */ public void run () { double theta = 0; for ( ; ; ) { theta += 0.05; ballPos.x = 300 + 100 * cos (theta); ballPos.y = 300 + 100 * sin (theta); out.println (" ( " + ballPos.x + " , " + ballPos.y + " ) "); try { displayer.sleep (tau); } catch (InterruptedException ex) { out.println ("Animator3.run: sleep failed"); } // try repaint (); } // for } // run /************************************************************** * Main method for invoking the animator. * @param args Command-line arguments */ public static void main (String [] args) { new Animator3 ("Animator"); } // main } // Animator3 class