diff options
author | Nao Pross <naopross@thearcway.org> | 2018-11-19 13:11:43 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-11-19 13:11:43 +0100 |
commit | 2c176589c24093ae93ec45e8d208dce81c27a515 (patch) | |
tree | eba17e826ed3e159c03b2b4ccc269c1bae131693 /src/subconscious/graphics/GameWindow.java | |
parent | Set up CardLayout for GameWindow and Scene pause/resume (diff) | |
download | Subconscious-java-2c176589c24093ae93ec45e8d208dce81c27a515.tar.gz Subconscious-java-2c176589c24093ae93ec45e8d208dce81c27a515.zip |
Create java package for the project
Diffstat (limited to 'src/subconscious/graphics/GameWindow.java')
-rw-r--r-- | src/subconscious/graphics/GameWindow.java | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java new file mode 100644 index 0000000..4d2f91c --- /dev/null +++ b/src/subconscious/graphics/GameWindow.java @@ -0,0 +1,132 @@ +package subconscious.graphics; + +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); + } + } + } +} |