1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

applet multithreading

Discussion in 'Software Development' started by rana2103, Apr 5, 2008.

Thread Status:
Not open for further replies.
  1. rana2103

    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;
    }
    }
    }
     
  2. Chicon

    Chicon

    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).
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/700541

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice