// TODO: package import java.awt.Dimension; import java.awt.GridLayout; import java.awt.CardLayout; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class GameWindow extends JFrame implements ActionListener { public static final Dimension WINDOW_SIZE = new Dimension(600, 400); public final String MENU_CARD = "menu"; public final String SCENE_CARD = "scene"; private JPanel root; private Scene scene = null; private Thread sceneThread = null; // TODO: remove map editor, start directly on Battle mode public GameWindow() { super("Subconscious"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(WINDOW_SIZE); this.setPreferredSize(WINDOW_SIZE); this.setLocationRelativeTo(null); this.initComponents(); this.add(this.root, BorderLayout.CENTER); this.pack(); this.setVisible(true); } private void initComponents() { // set up a cardlayout this.root = new JPanel(new CardLayout()); // build Menu card JPanel menu = new JPanel(); menu.setLayout(new GridLayout(3, 1)); JButton editor = new JButton("Editor"); editor.setActionCommand("btn-editor"); editor.addActionListener(this); JButton battle = new JButton("Battle"); battle.setActionCommand("btn-battle"); battle.addActionListener(this); JButton exit = new JButton("Exit"); exit.setActionCommand("btn-exit"); exit.addActionListener(this); menu.add(editor); menu.add(battle); menu.add(exit); // add cards this.root.add(menu, MENU_CARD); } public void showScene(Scene scene) { // the current scene is already open if (scene == this.scene) { ((CardLayout)this.root.getLayout()).show(this.root, SCENE_CARD); scene.updateCanvasSize(); scene.resume(); return; } // if there is an old scene if (this.scene != null) { // close old scene this.scene.stop(); try { this.sceneThread.join(); } catch (InterruptedException ex) { ex.printStackTrace(); } } // build new thread this.scene = scene; this.sceneThread = new Thread(this.scene); // add to layout this.root.add(scene, SCENE_CARD); ((CardLayout)this.root.getLayout()).show(this.root, SCENE_CARD); this.sceneThread.start(); scene.updateCanvasSize(); scene.resume(); } public void showMenu() { this.scene.pause(); ((CardLayout)this.root.getLayout()).show(this, MENU_CARD); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().startsWith("btn")) { if (e.getActionCommand().equals("btn-exit")) { this.setVisible(false); this.dispose(); return; } Scene scene = null; if (e.getActionCommand().equals("btn-editor")) { scene = new MapEditorScene(); } else if (e.getActionCommand().equals("btn-battle")) { scene = new BattleScene(); } if (scene != null) { showScene(scene); } } } }