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:
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.