Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  runtime error accessing ArrayList  (Read 1464 times)
0 Members and 1 Guest are viewing this topic.
Offline 106498

Senior Newbie





« Posted 2007-12-12 04:29:59 »

These 2 classes are basically the classes from the book developing games in java, without the fullscreen stuff. They are supposed to create an animation. They both compile fine but when I run I get an error message, an no animation, paint() is only called once. Here's the error message:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.RangeCheck(ArrayList.java:546)
at java.util.ArrayList.get(ArrayList.java:321)
at Animation.getFrame(Animation.java:60)
at Animation.update(Animation.java:43)
at AnimationTest1.animationLoop(AnimationTest1.java:58)
at AnimationTest1.run(AnimationTest1.java:44)
at AnimationTest1.main(AnimationTest1.java:10)

I think this means the program is trying to access frame 4 when there is none. Right? I just can't get my brain around this. Any help would be great!
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
import java.awt.Image;
import java.util.ArrayList;
 
/*   this class manages a serie of images (frames) and the amount of time to
   display each frame */

   
public class Animation {
   
   private ArrayList frames;
   private int currFrameIndex;
   private long animTime;
   private long totalDuration;
   
   //creates a new, empty animation, calls start().
  public Animation() {
      frames = new ArrayList();
      totalDuration = 0;
      start();
   }
   
   //adds an image to the animation, + how long to display the image.
  public synchronized void addFrame(Image image, long duration) {
      totalDuration += duration;
      frames.add(new AnimFrame(image, totalDuration));
   }
   
   //starts this animation over from the beginning .
  public synchronized void start() {
      animTime = 0;
      currFrameIndex = 0;
   }
   
   //updates this animations current frame if necessary
  public synchronized void update(long elapsedTime) {
      if (frames.size() >1) {
         animTime += elapsedTime;
         
         if (animTime >= totalDuration) {
            animTime = animTime % totalDuration;
            currFrameIndex = 0;
         }
         
         while (animTime > getFrame(currFrameIndex).endTime) {
            currFrameIndex ++;
         }
      }
   }
   
   //gets the animations current frame. null if none availible.
  public synchronized Image getImage() {
      if (frames.size() == 0) {
         return null;
      }
      else {
         return getFrame(currFrameIndex).image;
      }
   }
   
   private AnimFrame getFrame(int i) {
      return (AnimFrame)frames.get(i);
   }
   
   private class AnimFrame {
     
      Image image;
      long endTime;
     
      public AnimFrame(Image image, long endtime) {
         this.image = image;
         this.endTime = endTime;
      }
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
 
public class AnimationTest1 {
 
   public static void main (String args[]) {      
     
      AnimationTest1 test = new AnimationTest1();
      test.run();
   }
   
   private static final long DEMO_TIME = 5000;
   
   private JFrame frame;
   private Animation anim;
   
   public void loadImages() {
      //load images
     Image player1 = loadImage("gif1.png");
      Image player2 = loadImage("gif2.png");
      Image player3 = loadImage("gif3.png");
      //create animation
     anim = new Animation();
      anim.addFrame(player1, 200);
      anim.addFrame(player2, 200);
      anim.addFrame(player3, 200);
      anim.addFrame(player2, 200);
   }
   
   //loads image with ImageIcon
  private Image loadImage(String filename) {
      return new ImageIcon(filename).getImage();
   }
   
   public void run() {
      try {
         frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(300, 300);
         frame.setVisible(true);
         
         loadImages();
         animationLoop();
      }
      finally {
      }
   }
   
   public void animationLoop() {
      long startTime = System.currentTimeMillis();
      long currTime = startTime;
     
      while (currTime - startTime < DEMO_TIME) {
         long elapsedTime = System.currentTimeMillis() - currTime;
         currTime += elapsedTime;
         
         anim.update(elapsedTime);
         //getContentpane.getgraphics??!!
        Graphics g = frame.getGraphics();
         draw(g);
         g.dispose();
         
         try {
            Thread.sleep(20);
         }
         catch (InterruptedException ex) {}
      }
   }
   
   //draw the animation
  public void draw(Graphics g) {
      g.setColor(Color.white);
      g.fillRect(0, 0, 300, 300);      
      g.drawImage(anim.getImage(), 0, 0, null);
   }
}
Offline Juriy

Junior Member





« Reply #1 - Posted 2007-12-12 09:07:29 »

Basically, yes. You're trying access fifth frame (it's index is four - 'cause indexes start from 0).

After a brief look at the code I've found that the loop in update() is a bit weired. Try this hotfix it might help.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
//updates this animations current frame if necessary
  public synchronized void update(long elapsedTime) {
      if (frames.size() >1) {

         ...

         while (animTime > getFrame(currFrameIndex).endTime) {
            currFrameIndex ++;
            if (currFrameIndex == frames.size) break; // Hotfix
        }
      }
   }



http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline 106498

Senior Newbie





« Reply #2 - Posted 2007-12-12 20:01:04 »

I thought that was never supposed to happen because of the
1  
2  
 
   animTime = animTime % totalDuration;


I left the Animation class untouched so that would mean the code in the book is wrong. Or I made a mistake copying. I think the fault is somewhere in my test class.

oh by the way your hotfix gave a compile error. I added something similar, It got rid of the error message, but paint is still only called once, before the images are loaded.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Juriy

Junior Member





« Reply #3 - Posted 2007-12-13 08:13:45 »

1  
oh by the way your hotfix gave a compile error


Oh, sure: it's frames.size()

Look at your stack trace. It clearly shows that runtime exception happens in Animation.getFrame(Animation.java:60).

http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline 106498

Senior Newbie





« Reply #4 - Posted 2007-12-13 23:41:59 »

First of all, thanks for your replies so far!!

I added the (now working) hotfix but I still get the same runtime error. Which sort of surprised me. I looked at getFrame(), but all the methods depend on eachother so the fault could be anywhere, I think.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (21 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (56 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (65 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!