/****************************************************************** * @(#) Animator2.java 1.0 * * Copyright (c) 2002 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, 6 March 2002 * @author John Miller */ import java.awt.*; import java.awt.event.*; 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 Animator2 extends JFrame implements Runnable { private static final Dimension dim = new Dimension (600, 350); private static final int tau = 50; private final Thread displayer; private Point2D.Double ballPos; private Ellipse2D.Double ball1; private Ellipse2D.Double ball2; /************************************************************** * 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 Graphics context. */ public void paintComponent (Graphics gr) { super.paintComponent (gr); Graphics2D gr2 = (Graphics2D) gr; gr2.setColor (Color.red); ball1.setFrame (ballPos.x, ballPos.y, 15, 15); gr2.fill (ball1); gr2.setColor (Color.blue); ball2.setFrame (ballPos.x + 30, ballPos.y, 15, 15); gr2.fill (ball2); } // paintComponent } // DisplayPanel inner class /************************************************************** * Construct an animator frame. * @param title Title of frame */ public Animator2 (String title) { super (title); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit (0); } }); ballPos = new Point2D.Double (5, 300); ball1 = new Ellipse2D.Double (); ball2 = new Ellipse2D.Double (); getContentPane ().add (new DisplayPanel ()); setLocation (100, 100); setSize (dim); setVisible (true); displayer = new Thread (this, "displayer"); displayer.start (); } // Animator2 /************************************************************** * Run method for the display thread. * Repeatedly update coordinates, sleep and repaint. */ public void run () { for ( ; ballPos.x < 500; ballPos.x += 3) { ballPos.y = 50 + ((ballPos.x - 250) * (ballPos.x - 250)) / 250; //System.out.println (" ( " + ballPos.x + " , " + ballPos.y + " ) "); try { displayer.sleep (tau); } catch (InterruptedException ex) { System.err.println ("Animator2.run: sleep failed"); } // try repaint (); } // for for ( ; ballPos.x > 5; ballPos.x -= 3) { ballPos.y = 50 + ((ballPos.x - 250) * (ballPos.x - 250)) / 250; //System.out.println (" ( " + ballPos.x + " , " + ballPos.y + " ) "); try { displayer.sleep (tau); } catch (InterruptedException ex) { System.err.println ("Animator2.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 Animator2 ("Animator2"); } // main } // Animator2 class