applet multithreading

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

rana2103

Thread Starter
Joined
Apr 5, 2008
Messages
1
i hav recently started learning java and was trying a multithreading applet with the foll code.....i want to display 12 pics wid names pic1.gif,pic2.gif......till pic12.gif....its not working and giving me foll exceptions....:confused:

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException:
12
at s1.update(s1.java:83)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
at sun.awt.RepaintArea.paint(RepaintArea.java:216)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:301)
at java.awt.Component.dispatchEventImpl(Component.java:4486)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

:(

below is d code....ne help wud b highly appreciated ....plz reply soon...


import java.awt.*;
import java.applet.*;

public class s1 extends Applet implements Runnable
{
Image[] img;
Image img2;
static int cnt;

Thread th = null;


public void init()
{
int i=1;
img=new Image[12];
img2= getImage(getDocumentBase(),"image1.gif");

for ( i = 1; i < 13; i++)
{
String str="pic"+i+".gif";
img[i-1]= getImage(getDocumentBase(),str);
}

}
public void start()
{


if ( th == null )
{
th = new Thread( this );
th.start();
}
}

public void stop()
{
showStatus("Stopping....");
if ( th != null && th.isAlive() )
th.stop();

th = null;

}

public void run() {

while (th != null) {

repaint();

try {

Thread.sleep( 20 );


} catch ( InterruptedException e ) {

// do nothing
}
}
}

public void paint(Graphics g)
{
update(g);

}

public void update(Graphics g)
{
showStatus("Welcome To AstroFun.com.....");

g.drawImage(img2,0,0,this);
g.drawImage(img[cnt],0,0,this);
cnt=cnt+1;
if(cnt>12)
{
cnt=0;
th=null;
}
}
}
 
Joined
Jul 29, 2004
Messages
6,650
Hi rana2103,

Welcome to TSG ! :)

The mistake is located in the update method.
Indeed, in the statement g.drawImage(img[cnt],0,0,this);,
the cnt variable may have reached the value 12.
Regarding the declaration img=new Image[12];, the values of cnt may only vary from 0 to 11.
Therefore, the test if(cnt>12) must be replaced by if(cnt==12).
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top