/** * Animator.java * * @author Jeff Ondich * @version 3/3/05 * * An illustration of how you can do animation within a JFrame. Note * a couple notable features of this demo: * * 1. This class is set up to respond to mouse clicks and keystrokes. * 2. The animation loop in the run() method is pretty bone-headed * in that it checks the current time every time through the main loop * instead of using some more sophisticated Java-provided timer mechanism. */ import java.awt.*; import java.awt.Rectangle; import java.awt.event.*; import javax.swing.*; public class Animator extends JFrame implements MouseListener, KeyListener { // Animation control data. private final long mAnimationDelay = 100; private long mMostRecentDrawTime; // For more sophisticated animations, you would undoubtedly want separate // classes for your drawable objects. In an aquarium simulation, for // example, you would define a Fish class and just hold an array or // ArrayList of Fish references here. Because this is a simple demo, I have // stored all the Box and Ball data here in the Animator class. private final int mDefaultBoxWidth = 50; private final int mDefaultBoxLeft = 40; private final int mDefaultBoxTop = 40; private final int mDefaultBoxSpeed = 3; private int mBoxWidth; private int mBoxLeft; private int mBoxTop; private int mBoxSpeed; private final Color mDefaultColor = Color.red; private Color mColor; private final int mDefaultBallDiameter = 60; private final int mDefaultBallLeft = 100; private final int mDefaultBallTop = 150; private final int mDefaultBallSpeed = -5; private int mBallDiameter; private int mBallLeft; private int mBallTop; private int mBallSpeed; public Animator( String title ) { // Arrange for events to be sent to this window. addMouseListener( this ); addKeyListener( this ); // Initialize the window. setSize( 500, 600 ); setLocation( 20, 20 ); setTitle( title ); setBackground( Color.white ); toFront(); setVisible( true ); mColor = mDefaultColor; // Initialize the locations and directions of the ball and the box. mBoxWidth = mDefaultBoxWidth; mBoxLeft = mDefaultBoxLeft; mBoxTop = mDefaultBoxTop; mBoxSpeed = mDefaultBoxSpeed; mBallDiameter = mDefaultBallDiameter; mBallLeft = mDefaultBallLeft; mBallTop = mDefaultBallTop; mBallSpeed = mDefaultBallSpeed; // Timing mMostRecentDrawTime = System.currentTimeMillis(); } public void paint( Graphics g ) { g.clearRect( 0, 0, getWidth(), getHeight() ); g.setColor( mColor ); g.fillRect( mBoxLeft, mBoxTop, mBoxWidth, mBoxWidth ); g.fillOval( mBallLeft, mBallTop, mBallDiameter, mBallDiameter ); } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseDragged( MouseEvent e ) { } public void keyTyped( KeyEvent e ) { switch( e.getKeyChar() ) { case 'r': mColor = Color.red; break; case 'b': mColor = Color.blue; break; default: mColor = Color.green; break; } repaint(); } public void keyPressed( KeyEvent e ) { } public void keyReleased( KeyEvent e ) { } public void run() { while( true ) { if( System.currentTimeMillis() > mMostRecentDrawTime + mAnimationDelay ) { mMostRecentDrawTime = System.currentTimeMillis(); // Move the box. mBoxLeft += mBoxSpeed; if( mBoxLeft < 0 || mBoxLeft >= getWidth() ) { mBoxSpeed = -mBoxSpeed; mBoxLeft += 2*mBoxSpeed; } // Move the ball. mBallLeft += mBallSpeed; if( mBallLeft < 0 || mBallLeft >= getWidth() ) { mBallSpeed = -mBallSpeed; mBallLeft += 2*mBallSpeed; } // Draw the modified data. repaint(); } } } public static void main( String[] args ) { Animator window = new Animator( "Animation" ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.run(); } }