/****************************************************************** * @(#) Animator.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, 25 September 2007 * @author John Miller */ import static java.lang.System.*; 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 Animator extends JFrame implements Runnable { private static final Dimension dim = new Dimension (640, 350); private static final int tau = 20; // at 50 Hz private final Thread displayer; private final Point2D.Double ballPos; private final Ellipse2D.Double ball; /************************************************************** * 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.red); ball.setFrame (ballPos.x, ballPos.y, 20, 20); gr2.fill (ball); } // paintComponent } // DisplayPanel inner class /************************************************************** * Construct an animator frame. * @param title Title of frame */ public Animator (String title) { super (title); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 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 (); } // Animator /************************************************************** * Run method for the display thread. * Repeatedly update coordinates, sleep and repaint. */ public void run () { for ( ; ballPos.x <= 600; ballPos.x += 2) { ballPos.y = ((ballPos.x - 300) * (ballPos.x - 300)) / 300; // out.println (" ( " + ballPos.x + " , " + ballPos.y + " ) "); try { displayer.sleep (tau); } catch (InterruptedException ex) { out.println ("Animator.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 Animator ("Animator"); } // main } // Animator class