Animation Test 1 : Game Animation « Game « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Game » Game AnimationScreenshots 
Animation Test 1
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(80060016,
          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, 00null);

    // draw image
    g.drawImage(anim.getImage()00null);
  }

}

/**
 * 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 (AnimFrameframes.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);
  }

}
 
           
       
Related examples in the same category
1. Animation Test 2Animation Test 2
2. Mouse look TestMouse look Test
w__w_w.j__a___v_a2__s___._com___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.