I’m working on a simple sokoban game. I am using a JMenu which has 3 options: Reset board, Load board, and quit. Once the board is loaded the user should be able to use the keyboard to move around the game board. However, after the game is loaded, the keys do not work until I click the “Reset board” option on the menu (which is currently unimplemented and does nothing except a print statement). I can’t figure out what’s causing this strange behavior. Portions of the code are below. The Board class, which I’ve not included, implements the character movement functions.
Just to reiterate, if I “Load board” -> “Reset board”, the keys work fine, but I shouldn’t have to “Reset board” to get the keys to work.
public class Sokoban extends JFrame implements ActionListener {
Board b;
boolean boardLoaded = false;
public class View extends JComponent {
int xVal, yVal;
ImageIcon manIcon;
ImageIcon ballIcon;
Image man, ball;
int cellHeight, cellWidth;
int initialW = 500;
int initialH = 300;
Color floorColor;
public View() {
// ImageIcon code snipped
// Set the mouse listener
addMouseListener(new MouseAdapter() {
public void mouseClicked (MouseEvent event) {
xVal = event.getX();
yVal = event.getY();
}
});
// Set the key listener
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if(boardLoaded) {
char c = e.getKeyChar();
if(c == 'h') b.moveLeft();
if(c == 'l') b.moveRight();
if(c == 'j') b.moveDown();
if(c == 'k') b.moveUp();
}
}
public void keyPressed(KeyEvent e) {
if(boardLoaded) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT) b.moveLeft();
if(code == KeyEvent.VK_RIGHT) b.moveRight();
if(code == KeyEvent.VK_UP) b.moveUp();
if(code == KeyEvent.VK_DOWN) b.moveDown();
}
}
public void keyReleased(KeyEvent e) {}
});
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if(!boardLoaded) {
//Draw Splash Screen
}
else {
// Code to draw board snipped
}
}
public Dimension getPreferredSize() {
return new Dimension(initialW, initialH);
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Sokoban s = new Sokoban();
}
});
}
////////// Constructor
public Sokoban() {
showGUI();
}
////////// Sets up JFrame
private void showGUI() {
// Set up frame
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Sokoban");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create content
Container content = frame.getContentPane();
BorderLayout layout = new BorderLayout();
content.setLayout(layout);
// Set up board
View view = new View();
view.setFocusable(true);
content.add(view, BorderLayout.CENTER);
// Set up menu
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Actions");
menuBar.add(menu);
JMenuItem reset = new JMenuItem("Reset game");
JMenuItem load = new JMenuItem("Load board");
JMenuItem quit = new JMenuItem("Quit");
load.addActionListener(this);
load.setActionCommand("load");
reset.addActionListener(this);
reset.setActionCommand("reset");
quit.addActionListener(this);
quit.setActionCommand("quit");
menu.add(reset);
menu.add(load);
menu.add(quit);
frame.setJMenuBar(menuBar);
//Display the window
frame.pack();
frame.setVisible(true);
}
//// Actions for menu selections
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("load")) {
LinkedList<String> boardList = new LinkedList<String>();
int rows = 0;
int cols = 0;
FileDialog fd = new FileDialog(this, "Choose level file");
fd.setVisible(true);
FileInputStream fin;
String line;
try {
fin = new FileInputStream(fd.getDirectory()+fd.getFile());
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while((line = br.readLine()) != null) {
//System.out.println(line);
boardList.add(line);
if(line.length() > cols) cols = line.length();
}
fin.close();
}
catch (IOException io) {
System.err.println("Unable to read from file");
}
rows = boardList.size();
b = new Board(rows, cols);
b.getBoard(boardList);
boardLoaded = true;
}
if(e.getActionCommand().equals("quit")) {
System.exit(0);
}
if(e.getActionCommand().equals("reset")) {
System.out.println("not implemented yet");
}
}
}