package animationtest; import java.awt.*; import java.util.*; import javax.swing.*; public class Animation { private ArrayList scenes; private int sceneIndex; private int movieTime; private int totalTime; //CONSTRUCTOR public Animation() { scenes = new ArrayList(); totalTime = 0; start(); } //add scene to arraylist and set time for each scene public synchronized void addScene(Image i, long t) { totalTime += t; scenes.add(new OneScene(i, totalTime)); } // Start animation from beggining public synchronized void start() { movieTime = 0; sceneIndex = 0; } //Changing scenes public synchronized void update(long timePassed) { if (scenes.size() > 1) { movieTime += timePassed; if (movieTime >= totalTime) { movieTime = 0; sceneIndex = 0; } while (movieTime > getScene(sceneIndex).endTime) { sceneIndex++; } } } public synchronized Image getImage() { if (scenes.isEmpty()) { return null; } else { return getScene(sceneIndex).pic; } } private OneScene getScene(int x) { return (OneScene) scenes.get(x); } ///////PRIVATE INNER CLASS ////////// private class OneScene { Image pic; long endTime; public OneScene(Image pic, long endTime) { this.pic = pic; this.endTime = endTime; } } } package animationtest; import java.awt.*; import java.awt.image.BufferStrategy; import javax.swing.*; public class AnimationTest extends JFrame{ public static void main(String[] args) { MainConfig m = new MainConfig(); m.run(); } } package animationtest; import animationtest.Mario.MarioFrames; import animationtest.Mario.MarioMoves; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferStrategy; import javax.swing.*; import java.awt.event.KeyListener; public class MainConfig extends JFrame { public MarioMoves mm = new MarioMoves(); public Sprite mario_anim = new MarioMoves().currentSprite(); public Image backimage; final void run() { this.setTitle("AnimationTest8"); this.setResizable(false); this.setSize(550, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.createBufferStrategy(2); this.addKeyListener(new AKL()); loadImages(); movieLoop(); } //load images and add scenes public void loadImages() { backimage = new ImageIcon(this.getClass().getResource("/animationtest/blank gray background.PNG")).getImage(); mm.allFrames(); } //play movie public void movieLoop() { long startingTime = System.currentTimeMillis(); long cumTime = startingTime; while (cumTime - startingTime < cumTime) { long timePassed = System.currentTimeMillis() - cumTime; cumTime += timePassed; Graphics2D g = getGraphics2D(); draw(g); g.dispose(); mario_anim.update(timePassed); updateGraphics(); try { Thread.sleep(20); } catch (Exception ex) { } } } public void updateGraphics() { BufferStrategy buffer = this.getBufferStrategy(); if (!buffer.contentsLost() || buffer.contentsRestored()) { buffer.show(); } } public Graphics2D getGraphics2D() { BufferStrategy buffer = this.getBufferStrategy(); return (Graphics2D) buffer.getDrawGraphics(); } //draw graphics public void draw(Graphics g) { g.drawImage(backimage, 0, 0, null); g.drawImage(mario_anim.getImage(), 275 + Math.round(mario_anim.getX()), 250 + Math.round(mario_anim.getY()), null); } private class AKL extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { mm.keyPressed(e); } } } package animationtest; import java.awt.Image; public class Sprite { private Animation a; private float x; private float y; private float vx; private float vy; //CONSTRUCTOR public Sprite(Animation a) { this.a = a; } //change posistion public void update(long timePassed) { x += vx * timePassed; y += vy * timePassed; a.update(timePassed); } public float getX() { return x; } //get y position public float getY() { return y; } //set sprite y position public void setX(float x) { this.x = x; } public void setY(float y) { this.y = y; } //get sprite width public int getWidth() { return a.getImage().getWidth(null); } //get sprite height public int getHeight() { return a.getImage().getHeight(null); } // get horizantal velocity public float getVelocityX() { return vx; } // get vertical velocity public float getVelocityY() { return vy; } //set horizantal velocity public void setVelocityX(float vx) { this.vx = vx; } //set vertical velocity public void setVelocityY(float vy) { this.vy = vy; } // get sprite / image public Image getImage(){ return a.getImage(); } }