Mega Code Archive

 
Categories / Java / Game
 

Animation Test 1

/* DEVELOPING GAME IN JAVA  Caracteristiques Editeur : NEW RIDERS  Auteur : BRACKEEN  Parution : 09 2003  Pages : 972  Isbn : 1-59273-005-1  Reliure : Paperback  Disponibilite : Disponible a la librairie  */   import java.awt.DisplayMode; import java.awt.Graphics; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.Window; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JFrame; public class AnimationTest1 {   public static void main(String args[]) {     DisplayMode displayMode;     if (args.length == 3) {       displayMode = new DisplayMode(Integer.parseInt(args[0]), Integer           .parseInt(args[1]), Integer.parseInt(args[2]),           DisplayMode.REFRESH_RATE_UNKNOWN);     } else {       displayMode = new DisplayMode(800, 600, 16,           DisplayMode.REFRESH_RATE_UNKNOWN);     }     AnimationTest1 test = new AnimationTest1();     test.run(displayMode);   }   private static final long DEMO_TIME = 5000;   private SimpleScreenManager screen;   private Image bgImage;   private Animation anim;   public void loadImages() {     // load images     bgImage = loadImage("images/background.jpg");     Image player1 = loadImage("images/player1.png");     Image player2 = loadImage("images/player2.png");     Image player3 = loadImage("images/player3.png");     // create animation     anim = new Animation();     anim.addFrame(player1, 250);     anim.addFrame(player2, 150);     anim.addFrame(player1, 150);     anim.addFrame(player2, 150);     anim.addFrame(player3, 200);     anim.addFrame(player2, 150);   }   private Image loadImage(String fileName) {     return new ImageIcon(fileName).getImage();   }   public void run(DisplayMode displayMode) {     screen = new SimpleScreenManager();     try {       screen.setFullScreen(displayMode, new JFrame());       loadImages();       animationLoop();     } finally {       screen.restoreScreen();     }   }   public void animationLoop() {     long startTime = System.currentTimeMillis();     long currTime = startTime;     while (currTime - startTime < DEMO_TIME) {       long elapsedTime = System.currentTimeMillis() - currTime;       currTime += elapsedTime;       // update animation       anim.update(elapsedTime);       // draw to screen       Graphics g = screen.getFullScreenWindow().getGraphics();       draw(g);       g.dispose();       // take a nap       try {         Thread.sleep(20);       } catch (InterruptedException ex) {       }     }   }   public void draw(Graphics g) {     // draw background     g.drawImage(bgImage, 0, 0, null);     // draw image     g.drawImage(anim.getImage(), 0, 0, null);   } } /**  * The Animation class manages a series of images (frames) and the amount of  * time to display each frame.  */ class Animation {   private ArrayList frames;   private int currFrameIndex;   private long animTime;   private long totalDuration;   /**    * Creates a new, empty Animation.    */   public Animation() {     frames = new ArrayList();     totalDuration = 0;     start();   }   /**    * Adds an image to the animation with the specified duration (time to    * display the image).    */   public synchronized void addFrame(Image image, long duration) {     totalDuration += duration;     frames.add(new AnimFrame(image, totalDuration));   }   /**    * Starts this animation over from the beginning.    */   public synchronized void start() {     animTime = 0;     currFrameIndex = 0;   }   /**    * Updates this animation's current image (frame), if neccesary.    */   public synchronized void update(long elapsedTime) {     if (frames.size() > 1) {       animTime += elapsedTime;       if (animTime >= totalDuration) {         animTime = animTime % totalDuration;         currFrameIndex = 0;       }       while (animTime > getFrame(currFrameIndex).endTime) {         currFrameIndex++;       }     }   }   /**    * Gets this Animation's current image. Returns null if this animation has    * no images.    */   public synchronized Image getImage() {     if (frames.size() == 0) {       return null;     } else {       return getFrame(currFrameIndex).image;     }   }   private AnimFrame getFrame(int i) {     return (AnimFrame) frames.get(i);   }   private class AnimFrame {     Image image;     long endTime;     public AnimFrame(Image image, long endTime) {       this.image = image;       this.endTime = endTime;     }   } } /**  * The SimpleScreenManager class manages initializing and displaying full screen  * graphics modes.  */ class SimpleScreenManager {   private GraphicsDevice device;   /**    * Creates a new SimpleScreenManager object.    */   public SimpleScreenManager() {     GraphicsEnvironment environment = GraphicsEnvironment         .getLocalGraphicsEnvironment();     device = environment.getDefaultScreenDevice();   }   /**    * Enters full screen mode and changes the display mode.    */   public void setFullScreen(DisplayMode displayMode, JFrame window) {     window.setUndecorated(true);     window.setResizable(false);     device.setFullScreenWindow(window);     if (displayMode != null && device.isDisplayChangeSupported()) {       try {         device.setDisplayMode(displayMode);       } catch (IllegalArgumentException ex) {         // ignore - illegal mode for this device       }     }   }   /**    * Returns the window currently used in full screen mode.    */   public Window getFullScreenWindow() {     return device.getFullScreenWindow();   }   /**    * Restores the screen's display mode.    */   public void restoreScreen() {     Window window = device.getFullScreenWindow();     if (window != null) {       window.dispose();     }     device.setFullScreenWindow(null);   } }