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

You added scene twice in your main() but you attempt to iterate through scene more than 2 times. The index is 2, but your scene array has only 2 elements (0 & 1 index). You need to check for the scene size as well while you are iterating in your Animation class line 38...

thanks man i think i fixed it
cheers :)

Please mark the thread as solved. :)

before i click problem solved i like to upload my new main class
i hope this is working I got no errors but
its not really doing what i wanted it to

the aim of this project, was to get two pictures "face and face1" to keep swapping.
as in one "face" is a picture with eyes open and the other picture "face1" with eyes shut. i was trying to learn stuff on animation

but i just cant get this working cause it only shows the first face only
MAnimation.java class

import java.awt.*;

import javax.swing.*;
public class MAnimation {
	public static void main(String []args){
		MAnimation ma = new MAnimation();
		ma.run();
	}
	
	private ScreenManager s;
	private Image bg;
	private Animation a;
	
	public static final DisplayMode mode1[] ={
		new DisplayMode(800,600,32,0),
		new DisplayMode(800,600,24,0),
		new DisplayMode(800,600,16,0),
		new DisplayMode(640,480,32,0),
		new DisplayMode(640,480,24,0),
		new DisplayMode(640,480,16,0),

	};
	
	//load images and add scenes
	public void loadImages(){
		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 method call from main
	public void run(){
		s = new ScreenManager();
		try{
			DisplayMode dm = s.findFirstCompatiableMode(mode1);
			s.setFullScreen(dm);
			loadImages();
			movieLoop();
			
		}finally{
			s.restoreScreen();
		}
	}
	// movie loop play movie now
	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 <6000){
		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
		
		// draw and update the screen
		Graphics2D g = s.getGraphics();
		draw(g);
		g.dispose();
		s.update();
		
		try{
			Thread.sleep(20);
		}catch(Exception ex){}
			
			
		}
	 }
	// draws graphics 
	public void draw(Graphics g){
		g.drawImage(bg, 0, 0, null);
		g.drawImage(a.getImage(), 0, 0, null);
		
	}
	
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.