Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How to use an ArrayList here?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to use an ArrayList here?

    Hey guys. I wanted to see if someone could help me here. I have the following piece of code that which displays one asteroid on the screen.

    // draw asteroid
    		if(!status.isNewAsteroid())
    		{
    			// draw the asteroid until it reaches the bottom of the screen
    			if(asteroid.getY() + asteroid.getSpeed() < this.getHeight())
    			{
    				asteroid.translate(0, asteroid.getSpeed());
     
    				// Displays one asteroid on the screen:
    				graphicsMan.drawAsteroid(asteroid, g2d, this);
    			}
    			else
    			{
    				asteroid.setLocation(rand.nextInt(getWidth() - asteroid.width), 0);
    			}
    		}

    Image of game running with only one asteroid on the screen:

    http://i1226.photobucket.com/albums/...dSpaceHelp.jpg

    I want to draw more than one asteroid on the screen at the same time. How do I do this? Do I use an Array List? I think I have to use an Enhanced For loop but I am not too sure.

    P.S. The new asteroid obviously can't be just a modification of the current asteroid with modified x and y coordinates. It has to be a brand new asteroid of its own of a class Asteroid which I also have and it has to behave differently from the current asteroid. That is why I want to solve the problem with an array list.


    Thank you all very much.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to use an ArrayList here?

    If you put the items to be drawn in an arraylist, the paintComponent() method could go through the list using a loop and call each item's draw method so it could draw itself.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to use an ArrayList here?

    hi,
    i guess you need to have arraylist of asteroid object and asteroid should have some sort of mechanism (may be pseudo random generator for co-ordinates) so that asteroids have same behaviour but different states..

  4. #4
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to use an ArrayList here?

    Quote Originally Posted by Raven123 View Post
    hi,
    i guess you need to have arraylist of asteroid object and asteroid should have some sort of mechanism (may be pseudo random generator for co-ordinates) so that asteroids have same behaviour but different states..
    You are correct in the sense that the asteroids have a random generator in its class so that it always starts at a different location. This is the constructor of the Asteroid class:

    public Asteroid(GameScreen screen)
    {
    		this.setLocation(rand.nextInt(screen.getWidth() - asteroidWidth),0);
    		this.setSize(asteroidWidth, asteroidHeight);
    	}

    What I try to do is to create one asteroid with the parameter GameScreen and then put it inside an Array or ArrayList using a loop. Like this:

    this.asteroid = new Asteroid(screen);
    		for(int i = 0; i <= 2; i++)
    		{
    			Asteroid[] asteroids = new Asteroid[i];
    			asteroids[i] = this.asteroid;
    		}

    I think that this array isn't working because it seems that I am putting the same asteroid inside every element of the array. How do I create an array that fills up with different asteroids and all of them have the parameter GameScreen?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to use an ArrayList here?

    A problem is that the variable defined inside the loop: asteroids which goes away when execution leaves the loop.
    How do I create an array that fills up with different asteroids
    A list of the steps the code needs to do:
    create an array
    begin loop
    create a new Asteroid
    add that new Asteroid to the array
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to use an ArrayList here?

    Quote Originally Posted by Norm View Post
    A problem is that the variable defined inside the loop: asteroids which goes away when execution leaves the loop.

    A list of the steps the code needs to do:
    create an array
    begin loop
    create a new Asteroid
    add that new Asteroid to the array
    end loop
    Do you suggest an array or an array list?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to use an ArrayList here?

    An arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    jean28 (December 7th, 2012)

  9. #8
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to use an ArrayList here?

    Quote Originally Posted by Norm View Post
    An arraylist.
    It worked. Thank you very much. I already gave you the rep

  10. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use an ArrayList here?

    Hello, this seems a little related topic for my Q. I am a Java beginner and would like to confirm answer to this -

    Quesiton:
    CAN WE DO THIS -
    if (myClassList instanceof List<MyClass>)

    Thanks

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to use an ArrayList here?

    CAN WE DO THIS -
    Give it to a compiler and see what it says.

    You'll get an answer much faster and more correct from the compiler.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM