From e85032012f1d5ded1b6a49a88d345cc60a6ac88b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 22:52:56 +0200 Subject: Properly handle keyboard events --- test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'test/ch/epfl') diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 8009d9f..9329cc7 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java @@ -11,6 +11,7 @@ import ch.epfl.xblast.server.Level; import ch.epfl.xblast.server.painter.BoardPainter; import javax.swing.*; +import java.awt.*; import java.awt.event.KeyEvent; import java.util.Collections; import java.util.HashMap; @@ -48,27 +49,28 @@ public class GraphicalSimulation extends Simulation { return GameStateDeserializer.deserialize(serializedGameState); } - private static JFrame buildFrame(XBlastComponent gui) { + private static JFrame buildFrame(Container content) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); - frame.setContentPane(gui); + frame.setContentPane(content); frame.pack(); return frame; } - private void attachKeyboardHandler(JFrame frame) { - frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); - frame.requestFocusInWindow(); + private void attachKeyboardHandler(Component comp) { + comp.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); + comp.requestFocusInWindow(); } private final XBlastComponent gui; private GraphicalSimulation() { this.gui = new XBlastComponent(); - attachKeyboardHandler(buildFrame(this.gui)); + buildFrame(this.gui); + attachKeyboardHandler(this.gui); } @Override -- cgit v1.2.3