By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

Battleships game

P: 13
Hello there,

I have currently started a project in java, a battleships game. I am having trouble deciding how to deploy the grid and have the pictures of ships hidden under each picture of blue square which represents the sea. Can anybody out there give me some helpful pointers? Much appreciated,
thanks,
Ragaman
Nov 12 '06 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Hello there,

I have currently started a project in java, a battleships game. I am having trouble deciding how to deploy the grid and have the pictures of ships hidden under each picture of blue square which represents the sea. Can anybody out there give me some helpful pointers? Much appreciated,
thanks,
Ragaman
Do you know how to animations in applets?
Nov 13 '06 #2

P: 26
One method could be to have a grid of JButton objects. You could have say a two dimensional array of JButtons and then when a user clicks on the JButton, the appropriate image/icon of either the ship or nothing eg a hole in the water appears.

To make these images appear you will need to know something about ActionListeners and also Image creation/manipulation

http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html

is a good starting place.

To create a two dimensional array of JButtons you could do something like this
Expand|Select|Wrap|Line Numbers
  1.         JButton[][] battleshipgrid = new JButton[10][10];
  2.         for(int i=0; i < battleshipgrid.length; i++) {
  3.             for(int j=0; j < battleshipgrid.length; j++) {
  4.                 // create new button with a label. Can use an Icon here too.
  5.                 battleshipgrid[i][j] = new JButton("My Button"));
  6.             }
  7.         }
  8.  
Nov 13 '06 #3

P: 13
Thanks a million man, I appreciate it, i will give it a go.

Regards,

Ragaman
Nov 13 '06 #4

P: 13
Hello everyone,

I am still working on my battleships project and have been doing some research into how i am going to implement it. I have been working with the idea of having a 2d grid of JButtons and have been looking into the graphics tutorials on the sun wesite but i have been advised that i would not need any of this (such as AlphaComposite) to display and change the display of the sea, ships etc. If so, does anyone have any ideas how to display the images, as in, is there a way of having the images appear in the JButtons themselves?And then change them?

Thanks a million,

Ragaman
Nov 19 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.