// S.S. Java 2 - an animation demo adapted from Sun's Java Tutorial. // This version uses a swing timer to send actionEvents at regular // intervals. The animation is performed by moving a swing JLabel // (which remember can be a graphic) in a higher layer of a JLayerPane // while the background image is shown in a lower layer. // The space ship S.S. Java is moved across a star field, and the // user can click the mouse to start/stop the animation. // // The animation uses a "frame number" to advance the image // a certain number of pixels per repaint update (5). This number // is used modulo the width of the applet (which is the width of // the background image). // // Notice how when used as an appet a MediaTracker is used to // preload the image, and how the Applet start/stop methods are // used in conjunction with the boolean "frozen" to control the // animation. Also a main has been included so this program can // be run as a stand-alone as well. Notice no media tracker is // needed in this case, but certain window event handler methods // are used to control the animation, just like the applet start // and stop. // // Written 3/2001 by Wayne Pollock, Tampa Florida USA. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ssjava2 extends JApplet implements ActionListener { int frameNumber = -1; static final int pelsPerUpdate = 2; // a "pel" is a pixel Timer timer; boolean frozen = false; JLayeredPane lp; JLabel bgLabel, fgLabel; int fgHeight, fgWidth; int bgHeight, bgWidth; static String fgFile = "rocketship.gif"; static String bgFile = "starfield.gif"; // Invoked only when run as an applet: public void init() { Image bgImage = getImage( getCodeBase(), bgFile ); Image fgImage = getImage( getCodeBase(), fgFile ); buildUI( getContentPane(), bgImage, fgImage ); } // Do this when run as Applet or application: void buildUI ( Container container, Image bgImage, Image fgImage ) { final ImageIcon bgIcon = new ImageIcon( bgImage ); final ImageIcon fgIcon = new ImageIcon( fgImage ); bgWidth = bgIcon.getIconWidth(); bgHeight = bgIcon.getIconHeight(); fgWidth = fgIcon.getIconWidth(); fgHeight = fgIcon.getIconHeight(); //Set up a timer that calls this object's action handler timer = new Timer( 40, this ); //delay = 40 ms timer.setInitialDelay( 0 ); timer.setCoalesce( true ); //Create a label to display the background image. bgLabel = new JLabel( bgIcon ); bgLabel.setOpaque( true ); bgLabel.setBounds( 0, 0, bgWidth, bgHeight ); //Create a label to display the foreground image. fgLabel = new JLabel( fgIcon ); fgLabel.setBounds( -fgWidth, -fgHeight, fgWidth, fgHeight ); // Create the layered pane to hold the labels: lp = new JLayeredPane(); lp.setDoubleBuffered( true ); lp.setPreferredSize( new Dimension( bgWidth, bgHeight ) ); lp.addMouseListener( new MouseAdapter() { public void mousePressed( MouseEvent e ) { if ( frozen ) { frozen = false; startAnimation(); } else { frozen = true; stopAnimation(); } } } ); lp.add( bgLabel, new Integer(0) ); // low layer lp.add( fgLabel, new Integer(1) ); // high layer container.add( lp, BorderLayout.CENTER ); } // Invoked by the applet browser only: public void start () { startAnimation(); } // Invoked by the applet browser only: public void stop () { stopAnimation(); } public synchronized void startAnimation () { if ( ! frozen && ! timer.isRunning() ) timer.start(); } public synchronized void stopAnimation () { if ( timer.isRunning() ) timer.stop(); } public void actionPerformed ( ActionEvent e ) { frameNumber++; // Advance animation frame. int pos = ( ( frameNumber * pelsPerUpdate ) % ( fgWidth + bgWidth ) ) - fgWidth; fgLabel.setLocation( pos, ( bgHeight - fgHeight ) / 2 ); } // Invoked only when run as an application: public static void main( String [] args ) { Toolkit tk = Toolkit.getDefaultToolkit(); Image bgImage = tk.getImage( bgFile ); Image fgImage = tk.getImage( fgFile ); final Ssjava2 me = new Ssjava2(); JFrame f = new JFrame( "S. S. Java 2" ); f.addWindowListener( new WindowAdapter() { public void windowIconified ( WindowEvent e ) { me.stopAnimation(); } public void windowDeiconified ( WindowEvent e ) { me.startAnimation(); } public void windowClosing ( WindowEvent e ) { System.exit( 0 ); } } ); me.buildUI( f.getContentPane(), bgImage, fgImage ); f.setSize( 500, 125 ); f.setVisible( true ); me.startAnimation(); } }
Send comments and mail to the WebMaster. |