I'm stuck.

I'm trying to wirte a very small animation applet. It is suppose to import 15 BMPs and then of course play them back in an animation. I have the BMP's stored in a seperate directory but think I have the path right. It seems I am continually getting an "java.lang.ArrayIndexOutOfBoundsException: 15"

There are a lot of extra lines I have added to try and troubleshoot this but am not having any luck.

Any hel would be greatly appreciated.

package imageassignment;

/**
 * 
 */

import java.applet.Applet;
import java.awt.*;
import java.lang.Object;

public class Chapter21 extends Applet 
{
	private Image MC[];
	private int totalImages = 15,  // total number of images
	currentImage = 0,  // current image subscript
	sleepTime = 10;    // milliseconds to sleep
	MediaTracker imageTracker; // load the images when the applet begins executing
	Aframe myframe;
	

   	public void init()
	{
		//myframe=new Aframe(this);
		//myframe.setVisible(true);
   		//System.out.println(getDocumentBase());
   		MC = new Image[ totalImages ];
		imageTracker = new MediaTracker( this );
		for ( int i = 0; i < MC.length; i++ ) 
		{
			MC[ i ] = getImage( getDocumentBase(),
			"images/IMG" + (i+1) + ".bmp" );
			imageTracker.addImage( MC[ i ], i );// track loading image
		}
	try 
	{
		imageTracker.waitForID( 0 ); } catch( InterruptedException e ) { }
	}
   	
   	public void stop()
   	{
   		myframe.setVisible(false);
   	}
   	
   	public void start(Graphics g)
	{  
   		//myframe.setVisible(true);
   		System.out.println("start");
   		g.drawImage(MC[0], 0,0, 300, 300,this);
		currentImage = 1;
	}
	public void paint( Graphics g )
	{
		if ( imageTracker.checkID( currentImage, true ) ) 
		{
			System.out.println("paint");
			g.fillRect(0,0,300,300);
			// draw new image in buffer
			for (int cntr=0; cntr<totalImages;cntr++)
				{
					g.drawImage(MC[ currentImage ], 0, 0, 300, 300, this );
					currentImage = ++currentImage;
				}
		}
		else
			System.out.println("else");
			postEvent( new Event( this, Event.MOUSE_ENTER, "" ) );
		try 
		{
			System.out.println("try");
			Thread.sleep( sleepTime );
		}
		catch ( InterruptedException e ) 
		{
			System.out.println("catch");
			showStatus( e.toString() );
		}
		repaint();  // display buffered image
	}
	// override update to eliminate flicker
	
	public void update( Graphics g )
	{
		paint( g );
	}
}

hello allang Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 15 i have same error , but i have no images.
You must have html file and images folder in same place.
Name of images folder is "images".
Inside image folder You must have image files with names:
IMG1.bmp,IMG2.bmp,...,IMG15.bmp
Try...
Also try find in the forum other threads with Applet - key.
quuba

Hi Allang,
I do not kow you so I will say "java.lang.ArrayIndexOutOfBoundsException: 15" means it is counting beyound your 16 images (0 thru 15). To make sure you are hitting your image folder put some System.out.printin( [image name[ ); just to see if it is reading your images and it canprint the names of them. I am currently working on somrthing similar, well, a Media Tracker. I am using JNLP so my code to access my image folder is a little different. My problem is I need to scale each image to fit a particular size window and no image is the same size. I am having fun and learning a lot from it. I had it working except for the scaling and that really screwed me up. Sorry I really couldn't help you. but good luck
Ricg

Please to use the code tag so that the number of line could be mentioned.
I guess that your problem is the control index "currentImage", the global variable, that is out of the bound after completing the first run of the paint(), i.e. after the 15 (totalImages) pictures are displayed. So do insert the code line:
currentImage = currentImage%totalImages;
after the code line
currentImage = ++currentImage;

An animation is executed in the way where Thread.sleep( sleepTime ) should be called after each image is displayed. In your code, the sleep method is called each time after 15 images have been displayed. This is improper.

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.