The Artima Developer Community
Sponsored Link

Java Answers Forum
objects placed into arrays

3 replies on 1 page. Most recent reply: Dec 6, 2005 10:46 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
paul watson

Posts: 3
Nickname: zoidberg
Registered: Dec, 2005

objects placed into arrays Posted: Dec 6, 2005 6:06 AM
Reply to this message Reply
Advertisement
hello!

I have created a 10x10 array of buttons in a grid formation, however i wondered if anyone can help me to place objects of a set length randomly into the array??

I know this will involve math.RANDOM, but it is items that have 4 different set lengths and i dont know how to keep these objects of one set length together and then store them into the array??

here's my code for the array:


JButton[][] enemyButtons = new JButton[10][10];
for (int i = 0; i < enemyButtons.length; i++)
{
for (int j = 0; j < enemyButtons.length; j++)
{

enemyButtons[j] = new JButton();
enemyButtons[j].setPreferredSize(gridButtonSize);
//enemyButtons[j].addActionListener(new myEvent());
enemyButtons[j].setBackground(Color.yellow);

grid2.add(enemyButtons[j]);
}
}



any help would be amazing, thank you!!!


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: objects placed into arrays Posted: Dec 6, 2005 6:36 AM
Reply to this message Reply
You mean:
Pick a random number in the Array, then fill l fields including the selected field.
Right?

What I would do: Create a list of all fields that are capable of storing n Objects in a row.

A good clas for storing the Positions would be java.awt.Point.

At the end you should have a List containing this Points:

p([0 .. 9][0 .. (10-n+1)])

The list has a certain length s;

**Loop Start
generate a random number from 0 to s-1.
Read the points coordinates xRandom and yRandom.

Fill the Array from thisposition with n Objects.

Remove all Points from the list which have y = yRandom and
Math.abs(x-xRandom) < (n-1)

**Loop End
Exit Loop when the list of Points is empty.

If n changes after an insertions, the loop must be regenerated.

paul watson

Posts: 3
Nickname: zoidberg
Registered: Dec, 2005

Re: objects placed into arrays Posted: Dec 6, 2005 8:58 AM
Reply to this message Reply
umm ok cool, i got really confused!!

so first: create a class called ship that contains ints submarine, cruiser, destroy and battleship and then give each of these ints a set length!

second: i didn't understand what the list did really, sorry!? I want to be able to store these ints within the array so that when someone clicks on one of them i can call a eventAction.

sorry for being thick about this!!

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: objects placed into arrays Posted: Dec 6, 2005 10:46 PM
Reply to this message Reply
The list just stores every valid startpoint to insert objects of a certain length.

But don't worry, it wouldn't have worked anyway since
a) it only stores points to insert objects horizontally and
b) now I know that you want to play battle ships and need to insert objects of a variable length.

It would have been a good solution for storing many objects fast and without a endless loop



So forget all about the list.

1) Create a random point (x,y) and create a random direction (values from 0 to 3).
2) check if the random point and the n fields in the given direction are not occpied. If they are, return to 1).
3) occupy the fields with your ship.

Since you have only a few ships, you won't end in a endless loop.

Flat View: This topic has 3 replies on 1 page
Topic: Help with Cut Text Method Previous Topic   Next Topic Topic: program that create objects of the class, & use the methods of the class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use