3

I'm trying to make a little game in java for an assignment.

We need to have a resizeable Grid in a JFrame window with nxn (n= some sensible number like 5 or 7) fields (in this case each field is a button) and on that field these are the rules:

The middle button is a "black hole", and there are 2 players. Each player has N-1 number of ships that they have at their disposal, and they have to move them into the black hole. The first player who achieves this wins. The ships for both players are diagonally aligned towards and away from the middle (black hole).

The trick is, when you tell a ship to go in a direction, it will go in that direction until it hits either the wall or another ship.

Here is how the game looks at setup and as you move ships around:

enter image description here

My question is, I already successfuly made the field with all the ships and black hole in the right position. All of them are buttons and are clickable, so now how do I achieve the following:

After I select a single button with the mouse click, the system should set the Focus on it (I tried requestFocus() through an ActionListener but it does not seem to do anything).

Then after the focus is set, that same button (that already has an ActionListener on it) will have a KeyListener on it which waits for arrow key inputs. Then after you input the direction that button will move its coloring - simulating movement - until it either hits a ship, the wall, or goes into the black hole when it's cleared off the map.

My question is, how do I make Java know that the keyListener on a specific button should only listen to the arrow keys after I selected the button

and the other one is - how do I actually simulate the movement?

Basically, both teams spaceships are colored blue/red. The rest of the blocks are all white except black hole in the middle which is black.

How do I make it so that when a selected button gets a movement command, it shifts its setBackGroundColoring so that it shifts over block by block until it hits a wall or another ship? I don't even know where to begin so I thought I'd ask here for a bit of help.

user1966576
  • 83
  • 1
  • 10
  • have you included the ".addActionListener(this)" to your buttons? Without that, your buttons won't listen to actions. Make sure you do something like: gameboardButton[i].addActionListener(this); – Marcelo Tataje Nov 28 '14 at 17:18
  • A simple example is shown, and a related example is cited, in this [answer](http://stackoverflow.com/a/3072979/230513). – trashgod Nov 28 '14 at 17:36
  • @Marcelo Tataje yes of course I have included that. I already achieved stuff like changing buttons to red if I click them and similar simple stuff but have no idea how to "simulate" movement. – user1966576 Nov 28 '14 at 17:47
  • @trashgod that example is literally anything but simple for somebody who began studying Swing around 1 week ago to be really honest, but thanks. – user1966576 Nov 28 '14 at 18:20
  • 1
    @user1966576: Start [here](http://stackoverflow.com/a/12228640/230513). – trashgod Nov 28 '14 at 18:46

1 Answers1

1

You should also add a KeyListener to the button with a KeyAdapter as the argument, and when the event fires, you can get the key code of the event, and check it against the values:

KeyEvent.VK_UP
KeyEvent.VK_DOWN
KeyEvent.VK_LEFT
KeyEvent.VK_RIGHT

And then take the necessary action.

Regarding simulating movement, do you want the triangle to simply 'jump' from button to button, or something more fluid?

Sokratees9
  • 102
  • 9
  • Simply jump would suffice too, though perhaps it wouldn't be too difficult to make it fluid. It would take something like repainting the whole grid after every time a single block moves. – user1966576 Dec 09 '14 at 21:35
  • I assume the images you have are currently icons on the button rather than images floating over the button. If the latter, then it would be simpler, as you just need to then move the image by a pixel every few milliseconds. If the former, then you need to remove the image from the button, draw it in the same position, and then again move it by a pixel every few milliseconds. You should also look up double buffering in Swing to find how to do it and make it look smoother. – Sokratees9 Dec 11 '14 at 06:03