views:

79

answers:

4

I'm having a little trouble building the grids for a Battleship game for my Java class. So far, I can easily make a for loop to add JPanel or JButton objects to the JFrame. However, my issue is that I'll need to use those Panels or Buttons again when playing the game (such as clicking on a button to see if your opponent put a ship on that square, et cetera). Is there a simple way in Java to initialize reference variables for a LOT of objects? Or will I have to declare all of them individually?

+2  A: 

You can always extend JButton to keep track of the info you need. A simple example:

class MyButton extends JButton{
    private MyGameInfo mygameInfo;
    private int buttonId;
    //More fields....
    //Getters/Setters
}

Then instead of creating and adding JButton objects to your layout, create MyButton objects(which is also a JButton so your layout will not be effected) and use its extra functionality for your game logic.

Erkan Haspulat
This is definitely useful for what I need, thanks a ton! I'll need to use something like this in order to get the grid location of the button, so I can tell if I hit a ship or not. Again, thank you!
1n73rn37_j3d1
+3  A: 

You could try a multi dimensional array of JPanels (or any other object). Create an array with the same size as your grid. The line below initializes an array with 5 rows and 5 columns.

JPanel[][] battleField = new JPanel[5][5];

Use nested for loops to create the panels in the array.

for (int rowIndex = 0; rowIndex < battleField.length; rowIndex++)
{
    for (int cellIndex = 0; cellIndex < battleField[rowIndex]; cellIndex++)
    {
         battleField[rowIndex][cellIndex] = new JPanel();
    }
}

If you want to reference the battleField array later on you would just make it into a instance variable.

willcodejavaforfood
Is the layout for a 2D array (x, y), like battleField[1][2] would be position (1, 2)?
1n73rn37_j3d1
Nevermind, I answered my own question. Thank you so much for the help, this is exactly what I was looking for!!
1n73rn37_j3d1
+2  A: 

For a battleship game, you most likely want to retrieve the location of a button after is has been clicked. You can create a hashtable using your buttons as keys and the point it is located at as a value.

HashMap<JButton, Point> buttonMap = new HashMap<JButton, Point>();

for (int x = 0; x < COLUMNS; x++)
{
  for (int y = 0; y < ROWS; y++)
  {
     JButton btn = new JButton();
     btn.addActionListener(this);
     buttonMap.put(btn, new Point(x, y));
     //then add the button to your container
  }
}

The in your actionPerformed method you can convert the button to the point it is located at like this.

JButton btn = (JButton)actionEvent.getSource();
Point p = buttonMake.get(btn);

Of course you will need to properly handle error conditions such as source not being a button or the button not being in the map...

jlewis42
Oooo... I didn't know that I could do that! Thank you so much, I'll definitely be putting this into good use!
1n73rn37_j3d1
+1  A: 

For reference, here is a related matching game that uses a grid of buttons. This related answer demonstrates the application of the Model–View–Controller pattern to a simple game.

trashgod
Thanks! That's pretty neat, and I'll have to look into possibly implementing MVC into my project!
1n73rn37_j3d1