|
Project Help
For a project, I need to make a GUI that displays a 7x7 grid of buttons. Each of the buttons are set to the default button specifications except for one, which has a yellow background and is labeled P. This button is the button controlled by the user. The user is then suppose to be able to use the keyboard to move the user's button around the button grid. As of now, mine displays the buttons and keeps track of the player button's position in the button grid using an X and Y coordinate. When a key on the directional pad is hit, it displays which direction was hit and the new coordinates of the player. Now, here is my problem. Once I know the new player button's location, I don't know how to display the new button grid... Can anyone help me? Here's the code I have so far:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.lang.*;
import java.util.*;
public class KeyButtonMovement{
static int playerLocationX = 0;
static int playerLocationY = 0;
public static void create (){
JFrame frame = new JFrame("Button Movement");
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(7, 7));
Random generator = new Random();
playerLocationX = generator.nextInt((6));
playerLocationX++;
Random generator2 = new Random();
playerLocationY = generator.nextInt((6));
playerLocationY++;
KeyboardPanel keyboardPanel = new KeyboardPanel();
frame.add(keyboardPanel);
keyboardPanel.setFocusable(true);
playerLocationX = keyboardPanel.playerLocationX;
playerLocationY = keyboardPanel.playerLocationY;
for(int i=0; i <(7*7); i++){
int playerLocation = ((playerLocationY-1)*7)+(playerLocationX-1);
if(i==playerLocation){
JButton button = new JButton("P");
button.setBackground(Color.YELLOW);
button.setForeground(Color.BLACK);
panel1.add(button);
}else{
JButton button = new JButton();
panel1.add(button);
}
}
frame.add(panel1, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setLocation(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
create();
}
}
class KeyboardPanel extends JPanel implements KeyListener {
int playerLocationX;
int playerLocationY;
public KeyboardPanel(){
addKeyListener(this);
KeyButtonMovement k = new KeyButtonMovement();
playerLocationX = k.playerLocationX;
playerLocationY = k.playerLocationY;
}
public void keyReleased (KeyEvent e){}
public void keyTyped (KeyEvent e){}
public void keyPressed (KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_DOWN:
if ((playerLocationY +1)!=8){
playerLocationY = playerLocationY +1;
System.out.println("DOWN "+playerLocationX+" "+playerLocationY);
}
break;
case KeyEvent.VK_UP:
if ((playerLocationY -1)!=0){
playerLocationY = playerLocationY -1;
System.out.println("UP "+playerLocationX+" "+playerLocationY);
}
break;
case KeyEvent.VK_LEFT:
if ((playerLocationX -1)!=0){
playerLocationX = playerLocationX -1;
System.out.println("LEFT "+playerLocationX+" "+playerLocationY);
}
break;
case KeyEvent.VK_RIGHT:
if ((playerLocationX +1)!=8){
playerLocationX = playerLocationX +1;
System.out.println("RIGHT "+playerLocationX+" "+playerLocationY);
}
break;
//default: keyChar = e.getKeyChar();
}
}
}
|