Battleships game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ragaman123
    New Member
    • Nov 2006
    • 13

    #1

    Battleships game

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ragaman123
    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?

    Comment

    • mharrison
      New Member
      • Nov 2006
      • 26

      #3
      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
      Code:
              JButton[][] battleshipgrid = new JButton[10][10];
              for(int i=0; i < battleshipgrid.length; i++) {
                  for(int j=0; j < battleshipgrid.length; j++) {
                      // create new button with a label. Can use an Icon here too.
                      battleshipgrid[i][j] = new JButton("My Button"));
                  }
              }

      Comment

      • ragaman123
        New Member
        • Nov 2006
        • 13

        #4
        Thanks a million man, I appreciate it, i will give it a go.

        Regards,

        Ragaman

        Comment

        • ragaman123
          New Member
          • Nov 2006
          • 13

          #5
          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

          Comment

          Working...