Advertisement
SubzeroX5

AnimationTest8

Jul 17th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. package animationtest;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6.  
  7. public class Animation {
  8.  
  9.     private ArrayList scenes;
  10.     private int sceneIndex;
  11.     private int movieTime;
  12.     private int totalTime;
  13.  
  14.     //CONSTRUCTOR
  15.     public Animation() {
  16.         scenes = new ArrayList();
  17.         totalTime = 0;
  18.         start();
  19.  
  20.     }
  21.  
  22.     //add scene to arraylist and set time for each scene
  23.     public synchronized void addScene(Image i, long t) {
  24.         totalTime += t;
  25.         scenes.add(new OneScene(i, totalTime));
  26.     }
  27.  
  28.     // Start animation from beggining
  29.     public synchronized void start() {
  30.         movieTime = 0;
  31.         sceneIndex = 0;
  32.     }
  33.  
  34.     //Changing scenes
  35.     public synchronized void update(long timePassed) {
  36.         if (scenes.size() > 1) {
  37.             movieTime += timePassed;
  38.             if (movieTime >= totalTime) {
  39.                 movieTime = 0;
  40.                 sceneIndex = 0;
  41.             }
  42.             while (movieTime > getScene(sceneIndex).endTime) {
  43.                 sceneIndex++;
  44.             }
  45.         }
  46.     }
  47.  
  48.     public synchronized Image getImage() {
  49.         if (scenes.isEmpty()) {
  50.             return null;
  51.         } else {
  52.             return getScene(sceneIndex).pic;
  53.         }
  54.     }
  55.  
  56.     private OneScene getScene(int x) {
  57.         return (OneScene) scenes.get(x);
  58.     }
  59.  
  60. ///////PRIVATE INNER CLASS //////////
  61.     private class OneScene {
  62.  
  63.         Image pic;
  64.         long endTime;
  65.  
  66.         public OneScene(Image pic, long endTime) {
  67.             this.pic = pic;
  68.             this.endTime = endTime;
  69.         }
  70.     }
  71. }
  72.  
  73. package animationtest;
  74.  
  75. import java.awt.*;
  76. import java.awt.image.BufferStrategy;
  77. import javax.swing.*;
  78.  
  79. public class AnimationTest extends JFrame{
  80.  
  81.     public static void main(String[] args) {
  82.         MainConfig m = new MainConfig();
  83.         m.run();
  84.  
  85.     }
  86. }
  87.  
  88. package animationtest;
  89.  
  90. import animationtest.Mario.MarioFrames;
  91. import animationtest.Mario.MarioMoves;
  92. import java.awt.*;
  93. import java.awt.event.KeyAdapter;
  94. import java.awt.event.KeyEvent;
  95. import java.awt.image.BufferStrategy;
  96. import javax.swing.*;
  97.  
  98. import java.awt.event.KeyListener;
  99.  
  100.  
  101. public class MainConfig extends JFrame {
  102.  
  103.     public MarioMoves mm = new MarioMoves();
  104.    
  105.     public Sprite mario_anim = new MarioMoves().currentSprite();
  106.     public Image backimage;
  107.    
  108.     final void run() {
  109.         this.setTitle("AnimationTest8");
  110.         this.setResizable(false);
  111.         this.setSize(550, 500);
  112.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  113.         this.setVisible(true);
  114.         this.createBufferStrategy(2);
  115.         this.addKeyListener(new AKL());
  116.         loadImages();
  117.         movieLoop();
  118.     }
  119.     //load images and add scenes
  120.  
  121.     public void loadImages() {
  122.         backimage = new ImageIcon(this.getClass().getResource("/animationtest/blank gray background.PNG")).getImage();
  123.         mm.allFrames();
  124.     }
  125.  
  126.     //play movie
  127.     public void movieLoop() {
  128.         long startingTime = System.currentTimeMillis();
  129.         long cumTime = startingTime;
  130.         while (cumTime - startingTime < cumTime) {
  131.             long timePassed = System.currentTimeMillis() - cumTime;
  132.             cumTime += timePassed;
  133.  
  134.             Graphics2D g = getGraphics2D();
  135.             draw(g);
  136.             g.dispose();
  137.             mario_anim.update(timePassed);
  138.             updateGraphics();
  139.             try {
  140.                 Thread.sleep(20);
  141.             } catch (Exception ex) {
  142.             }
  143.         }
  144.     }
  145.  
  146.     public void updateGraphics() {
  147.         BufferStrategy buffer = this.getBufferStrategy();
  148.         if (!buffer.contentsLost() || buffer.contentsRestored()) {
  149.             buffer.show();
  150.         }
  151.     }
  152.  
  153.     public Graphics2D getGraphics2D() {
  154.         BufferStrategy buffer = this.getBufferStrategy();
  155.         return (Graphics2D) buffer.getDrawGraphics();
  156.     }
  157.  
  158.     //draw graphics
  159.     public void draw(Graphics g) {
  160.         g.drawImage(backimage, 0, 0, null);
  161.         g.drawImage(mario_anim.getImage(), 275 + Math.round(mario_anim.getX()), 250 + Math.round(mario_anim.getY()), null);
  162.     }
  163.  
  164.     private class AKL extends KeyAdapter {
  165.        
  166.         @Override
  167.         public void keyPressed(KeyEvent e) {
  168.             mm.keyPressed(e);
  169.         }
  170.  
  171.     }
  172.    
  173.  
  174. }
  175.  
  176. package animationtest;
  177.  
  178. import java.awt.Image;
  179.  
  180. public class Sprite {
  181.  
  182.     private Animation a;
  183.     private float x;
  184.     private float y;
  185.     private float vx;
  186.     private float vy;
  187.  
  188.     //CONSTRUCTOR
  189.     public Sprite(Animation a) {
  190.         this.a = a;
  191.     }
  192.  
  193.     //change posistion
  194.     public void update(long timePassed) {
  195.         x += vx * timePassed;
  196.         y += vy * timePassed;
  197.         a.update(timePassed);
  198.     }
  199.    
  200.  
  201.     public float getX() {
  202.         return x;
  203.     }
  204.  
  205.     //get y position
  206.     public float getY() {
  207.         return y;
  208.     }
  209.  
  210.     //set sprite y position
  211.     public void setX(float x) {
  212.         this.x = x;
  213.     }
  214.  
  215.     public void setY(float y) {
  216.         this.y = y;
  217.     }
  218.     //get sprite width
  219.  
  220.     public int getWidth() {
  221.         return a.getImage().getWidth(null);
  222.     }
  223.  
  224.     //get sprite height
  225.     public int getHeight() {
  226.         return a.getImage().getHeight(null);
  227.     }
  228.     // get horizantal velocity
  229.  
  230.     public float getVelocityX() {
  231.         return vx;
  232.     }
  233.     // get vertical velocity
  234.  
  235.     public float getVelocityY() {
  236.         return vy;
  237.     }
  238.     //set horizantal velocity
  239.  
  240.     public void setVelocityX(float vx) {
  241.         this.vx = vx;
  242.     }
  243.     //set vertical velocity
  244.  
  245.     public void setVelocityY(float vy) {
  246.         this.vy = vy;
  247.     }
  248.    
  249.     // get sprite / image
  250.     public Image getImage(){
  251.         return a.getImage();
  252.     }
  253.    
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement