dcsimg
store objects in an array
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Paul_Watson
Posted On:   Tuesday, December 6, 2005 09:26 AM

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[i].length; j++) { enemyButtons[i][j] = new JButton(); enemyButtons[i][j].setPreferredSize(gridButtonSize); //enemyBu   More>>

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[i].length; j++)
{

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

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



any help would be amazing, thank you!!!    <<Less

Re: store objects in an array

Posted By:   Jani_Kaarela  
Posted On:   Thursday, December 8, 2005 02:49 AM


Not really answering the question, but I thought these may generally come in handy:


Re: store objects in an array

Posted By:   WarnerJan_Veldhuis  
Posted On:   Tuesday, December 6, 2005 09:34 AM

Sounds like the old Sea Battle :)


You should only set the starting point randomly, then add/substract n in the chosen direction.

up = substract n from i

down = add n to i

right = add n to j

left = substract n from j



I hope this makes sense...
About | Sitemap | Contact