Hi all I made this animation app and I think its all correct however i get a error message which is:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Animation.getScene(Animation.java:56)
at Animation.getImage(Animation.java:49)
at MainAnimation.draw(MainAnimation.java:64)
at MainAnimation.movieLoop(MainAnimation.java:53)
at MainAnimation.run(MainAnimation.java:34)
at MainAnimation.main(MainAnimation.java:10)
I cant figure out where am going wrong is there anyone who can help
thses are my classes
Animation.java class
import java.awt.*;
import java.util.*;
public class Animation {
private ArrayList scenes;// for the animation images
private int sceneIndex;// to chose them in order
private long movieTime;// the time for the animation, keeping track how long it runs for
private long totalTime;// keeps track of the total time your animation is allowed to play. basically will be comparing both times so when one stops it can restart the animation
//CONSTRUCTOR
public Animation(){
scenes = new ArrayList();
totalTime = 0;
start();
}
//Add scene to ArrayList and add time for each scene
public synchronized void addScene(Image i, long t){// Image is the pic your going to use, long t, is the totalTime
totalTime += t;
scenes.add(new OneScene(i, totalTime) );// OneScene is a class where going to make and we add pictures to the arrayList
}
//Start animation from beginning
public synchronized void start (){
movieTime = 0;
sceneIndex = 0;
}
//Change scene from one to another
public synchronized void update(long timePassed){// takes one parameter the time that passes from the last update
if(scenes.size()>1){
movieTime =+ timePassed;//well the movieTime is the sum of the time that passed from the last update, store the timePassed into the movieTime
if(movieTime >= totalTime){// we want to check if the time of the animation itself does not exceed the limit that it should go, and if it does exceed it when need to restart the animation so it wont freeze
movieTime = 0; // soon as its appears and finishes it should restart again
sceneIndex = 0;
}
while(movieTime>getScene(sceneIndex).endTime);
sceneIndex++;
}
}
// Get animation current scene(the pictures)
public synchronized Image getImage(){
if(scenes.size()==0){
return null;
}else{
return getScene(sceneIndex).pic;
}
}
// get scenes
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;
}
}
}
this is my main class MainAnimation.java
import java.awt.*;
import javax.swing.*;
public class MainAnimation {
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
MainAnimation m = new MainAnimation();
m.run(dm);
}
private Screen screen;
private Image bg;
private Animation a;
// loads pictures from computer to java program and adds to the arrayList for the scenes to be shown
public void loadpics(){
bg = new ImageIcon("C:\\Users\\BISON\\Desktop\\junk\\background.png").getImage();
Image face = new ImageIcon("C:\\Users\\BISON\\Desktop\\junk\\face.png").getImage();
Image face1 = new ImageIcon("C:\\Users\\BISON\\Desktop\\junk\\face1.png").getImage();
a = new Animation();// using the animation object
a.addScene(face ,250); // adding both pictures to the addScene and the time for it to play
a.addScene(face1 ,250);
}
// Main engine to run the program
public void run(DisplayMode dm){
screen = new Screen(); //now we have access to all that in our screen
try{
screen.setFullScreen(dm, new JFrame());
loadpics();
movieLoop();
}finally{
screen.restoreScreen();
}
}
//Main Movie Loop(play the movie from frame to frame)
public void movieLoop(){
long startingTime = System.currentTimeMillis(); //keeping the track of the time it begins
//System.currentTimeMillis gets that time on your computer
long cumTime = startingTime;// to keep track of cumulative time the animation been running for
while (cumTime - startingTime <5000){
long timePassed = System.currentTimeMillis() - cumTime;// gets the current time and minus it from the cumTime
cumTime += timePassed;// adding up all the time it passes during the loop, when 5secs have pass it breaks out of the loop
a.update(timePassed); //cumTime keeps getting added up and pass it in to the timePassed which is sent to the update
Graphics g = screen.getFullScreenWindow().getGraphics();
draw(g);
g.dispose();
try{
Thread.sleep(20);
}catch(Exception ex){}
}
}
public void draw(Graphics g){
g.drawImage(bg,0,0,null);
g.drawImage(a.getImage(),0,0,null);
}
}
Please guys any help