Results 1 to 5 of 5
  1. #1
    iowndd is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Post Need help with error: java.lang.ArrayIndexOutOfBoundsException: 1

    Hey im fairly new to java only been programming for about a month now, and i was trying to make this program which would draw particles to the screen and then make them fall to the ground, unfortunately i got this error. Heres my code : -
    Java Code:
    import java.awt.*;
    import hsa.Console;
    import java.util.*;
    
    public class Particles
    {
        static Console c;           // The output console
    
        public static void main (String[] args) throws Exception
        {
            c = new Console ();
            int maxx = c.getWidth ();
            int maxy = c.getHeight ();
            int Max_Particles = 500;
            int[] [] coordinates = new int [199] [1];
            int x = 0;
            int y = 0;
    
    
            for (int i = 0 ; i < 198 ; i++)
    
                {
                    Random rand = new Random ();
                    int random = 1 + rand.nextInt (500);
                    int random2 = 1 + rand.nextInt (500);
                    coordinates [i] [0] = random;
                    random = x;
                    coordinates [i] [1] = random2;
                    random2 = y;
                }
            for (int particles = 0 ; particles < Max_Particles ; particles++)
    
                {
                    c.drawOval (x, y + particles, 5, 10);
                    Thread.sleep (100);
                    if (y >= 500)
                        ;
                    {
                        y = 0;
                    }
    
    
                }
            // Place your program here.  'c' is the output console
        } // main method
    } // Particles class
    Any kind of help would me much appreciated!
    Last edited by sunde887; 10-02-2011 at 07:49 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with error: java.lang.ArrayIndexOutOfBoundsException: 1

    Quote Originally Posted by iowndd View Post
    int[] [] coordinates = new int [199] [1];
    This is basically a one dimensional array because the second index can only be zero.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    iowndd is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help with error: java.lang.ArrayIndexOutOfBoundsException: 1

    Oh so what do you suggest i should do to make it work and make multiple particles appear at once falling down on the screen?
    Last edited by iowndd; 10-02-2011 at 07:57 PM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Need help with error: java.lang.ArrayIndexOutOfBoundsException: 1

    I feel like you should make Particle an object, the class should represent one single particle (which contains the location). Then have an array of particles, which all have there own location. Perhaps each particle can have some deltaX/Y variables which control how it moves.

    Also, a very nice tutorial which may give you a lot of ideas on how to implement this can be found here: http://www.dreamincode.net/forums/to...-game-of-pong/

    Some suggestions if you actually try to make the pong as the tutorial goes through: draw instead with a bufferedImage, place it in a jpanel (standalone app rather than an applet). Generalize the paddle class so there is only one class. Fix the problem of using all the setters inside of constructors and setters. Instead simply assign the variables directly. Even if you don't try it, reading it should give you a good idea of how to put particles on the screen and animate them.
    Last edited by sunde887; 10-02-2011 at 08:12 PM.

  5. #5
    iowndd is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help with error: java.lang.ArrayIndexOutOfBoundsException: 1

    Thankyou very much everybody

Similar Threads

  1. Replies: 1
    Last Post: 09-27-2011, 06:47 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  4. Replies: 1
    Last Post: 07-31-2007, 11:47 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •