From a659fc5cfe73938731cc0b431f1c42a2fa0ed02d Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 19 Nov 2018 10:58:47 +0100 Subject: Update GameWindow and remove parent frame dependency on Scene --- src/BattleScene.java | 19 ++-- src/GameWindow.java | 87 +++++++++--------- src/MapEditor.java | 235 ------------------------------------------------ src/MapEditorScene.java | 235 ++++++++++++++++++++++++++++++++++++++++++++++++ src/MapScene.java | 9 +- src/Scene.java | 30 +++---- 6 files changed, 306 insertions(+), 309 deletions(-) delete mode 100644 src/MapEditor.java create mode 100644 src/MapEditorScene.java diff --git a/src/BattleScene.java b/src/BattleScene.java index 1e4b633..df691ea 100644 --- a/src/BattleScene.java +++ b/src/BattleScene.java @@ -43,8 +43,8 @@ public class BattleScene extends MapScene { private int realX = 0; private int realY = 0; - public BattleScene(JFrame frame) { - super(frame); + public BattleScene() { + super(); // TODO: this should be handled in MapScene MapLoader mapLoader = new MapLoader("../testmap.json"); @@ -91,26 +91,27 @@ public class BattleScene extends MapScene { //draw panels g.setColor(Palette.WHITE_T); - g.fillRect(this.WIDTH-100*this.guiSize, this.HEIGHT-100*this.guiSize, + g.fillRect(this.canvasSize.width-100*this.guiSize, this.canvasSize.height-100*this.guiSize, 100*this.guiSize, 100*this.guiSize); - g.fillRect(0, this.HEIGHT-100*this.guiSize, 150*this.guiSize, 100*this.guiSize); + g.fillRect(0, this.canvasSize.height-100*this.guiSize, 150*this.guiSize, 100*this.guiSize); g.setColor(Palette.BLACK); g.setFont(g.getFont().deriveFont(12.0F*this.guiSize)); Tile tile = null; + try{ tile = this.map.getTile(this.previousX, this.previousY); switch (tile.getType()) { case CLEAR: break; case GRASS: - g.drawString("Grass", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); + g.drawString("Grass", this.canvasSize.width-90*this.guiSize, this.canvasSize.height-80*this.guiSize); break; case WATER: - g.drawString("Water", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); + g.drawString("Water", this.canvasSize.width-90*this.guiSize, this.canvasSize.height-80*this.guiSize); break; case MOUNTAIN: - g.drawString("Mountain", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); + g.drawString("Mountain", this.canvasSize.width-90*this.guiSize, this.canvasSize.height-80*this.guiSize); break; } @@ -118,7 +119,7 @@ public class BattleScene extends MapScene { if (this.actorClicked && this.lastActor != null) { g.setColor(Palette.WHITE_T); g.fillRect(0, 0, 100*this.guiSize, 150*this.guiSize); - g.fillRect(this.WIDTH-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); + g.fillRect(this.canvasSize.width-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); g.setColor(Palette.BLACK); g.drawString(this.lastActor.getName(), 5*this.guiSize, 15*this.guiSize); @@ -135,7 +136,7 @@ public class BattleScene extends MapScene { } else if (this.actorClicked) { g.setColor(Palette.WHITE_T); g.fillRect(0, 0, 100*this.guiSize, 150*this.guiSize); - g.fillRect(this.WIDTH-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); + g.fillRect(this.canvasSize.width-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); g.setColor(Palette.BLACK); g.drawString("None left", 5*this.guiSize, 15*this.guiSize); diff --git a/src/GameWindow.java b/src/GameWindow.java index 540d75b..d1afad4 100644 --- a/src/GameWindow.java +++ b/src/GameWindow.java @@ -10,29 +10,28 @@ import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class GameWindow implements ActionListener { +public class GameWindow extends JFrame implements ActionListener { public static final Dimension WINDOW_SIZE = new Dimension(600, 400); - private JFrame frame; private JPanel menu; // TODO: remove map editor, start directly on Battle mode public GameWindow() { - this.frame = new JFrame("Subconscious"); + super("Subconscious"); - this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.frame.setSize(WINDOW_SIZE); - this.frame.setPreferredSize(WINDOW_SIZE); - this.frame.setLocationRelativeTo(null); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setSize(WINDOW_SIZE); + this.setPreferredSize(WINDOW_SIZE); + this.setLocationRelativeTo(null); JPanel menu = new JPanel(); menu.setLayout(new GridLayout(3, 1)); JButton editor = new JButton("Editor"); - editor.setActionCommand("editor"); + editor.setActionCommand("btn-editor"); JButton battle = new JButton("Battle"); - battle.setActionCommand("battle"); + battle.setActionCommand("btn-battle"); JButton exit = new JButton("Exit"); - exit.setActionCommand("exit"); + exit.setActionCommand("btn-exit"); editor.addActionListener(this); battle.addActionListener(this); @@ -44,46 +43,48 @@ public class GameWindow implements ActionListener { this.menu = menu; - this.frame.add(this.menu); - this.frame.pack(); - - this.frame.setVisible(true); + this.add(this.menu); + this.pack(); + this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { - if ("editor".equals(e.getActionCommand())) { - MapEditor mapEditor = new MapEditor(frame); - - this.frame.getContentPane().removeAll(); - this.frame.getContentPane().invalidate(); - this.frame.getContentPane().add(mapEditor); - this.frame.getContentPane().revalidate(); - - Thread mapEditorThread = new Thread(mapEditor); - mapEditorThread.start(); - - } else if ("battle".equals(e.getActionCommand())) { - BattleScene battleScene = new BattleScene(frame); - - this.frame.getContentPane().removeAll(); - this.frame.getContentPane().invalidate(); - this.frame.getContentPane().add(battleScene); - this.frame.getContentPane().revalidate(); - - Thread battleThread = new Thread(battleScene); - battleThread.start(); - - } else if ("exit".equals(e.getActionCommand())) { - this.frame.setVisible(false); - this.frame.dispose(); + 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) { + return; + } + + this.getContentPane().removeAll(); + this.getContentPane().invalidate(); + this.getContentPane().add(scene); + this.getContentPane().revalidate(); + + scene.updateCanvasSize(); + + Thread sceneThread = new Thread(scene); + sceneThread.start(); } } public void backToMenu() { - this.frame.getContentPane().removeAll(); - this.frame.getContentPane().invalidate(); - this.frame.getContentPane().add(this.menu); - this.frame.getContentPane().revalidate(); + this.getContentPane().removeAll(); + this.getContentPane().invalidate(); + this.getContentPane().add(this.menu); + this.getContentPane().revalidate(); } } diff --git a/src/MapEditor.java b/src/MapEditor.java deleted file mode 100644 index 4ca5530..0000000 --- a/src/MapEditor.java +++ /dev/null @@ -1,235 +0,0 @@ -import java.util.ArrayList; - -import java.awt.Canvas; -import java.awt.Graphics2D; -import java.awt.Dimension; -import java.awt.BorderLayout; -import java.awt.GridLayout; -import java.awt.event.KeyEvent; -import java.awt.event.MouseEvent; -import java.awt.event.MouseWheelEvent; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.image.BufferStrategy; -import java.awt.geom.AffineTransform; -import java.awt.geom.Point2D; -import java.awt.geom.NoninvertibleTransformException; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JButton; -import javax.swing.JComponent; -import javax.swing.JOptionPane; -import javax.swing.JLabel; -import javax.swing.JTextField; -import javax.swing.JFileChooser; -import javax.swing.JCheckBox; - -import java.lang.Integer; - -public class MapEditor extends MapScene { - private Tile.Type placingTile = Tile.Type.CLEAR; - private Actor placingActor = new Actor("", 0, false, 0); - private ArrayList actorFields = new ArrayList<>(); - private JFrame actorFrame; - private boolean nukeActor = false; - - public MapEditor(JFrame frame) { - super(frame); - this.setLayout(new BorderLayout()); - - JPanel bottomPanel = new JPanel(); - bottomPanel.setLayout(new GridLayout(1,5)); - - JButton newButton = new JButton("New"); - newButton.setActionCommand("new"); - JButton tileButton = new JButton("Placing selector"); - tileButton.setActionCommand("tile"); - JButton exportButton = new JButton("Export"); - exportButton.setActionCommand("export"); - JButton importButton = new JButton("Import"); - importButton.setActionCommand("import"); - JButton actorButton = new JButton("Actors"); - actorButton.setActionCommand("actors"); - - newButton.addActionListener(this); - tileButton.addActionListener(this); - actorButton.addActionListener(this); - exportButton.addActionListener(this); - importButton.addActionListener(this); - - bottomPanel.add(newButton); - bottomPanel.add(tileButton); - bottomPanel.add(actorButton); - bottomPanel.add(exportButton); - bottomPanel.add(importButton); - - this.add(bottomPanel, BorderLayout.PAGE_END); - this.add(this.canvas, BorderLayout.CENTER); - } - - @Override - protected void update(int deltaTime) {} - - @Override - protected void absoluteRender(Graphics2D g2d) {} - - @Override - public void mouseClicked(MouseEvent e) { - int tileSize = this.maxSize/10; - Point2D p = new Point2D.Double(e.getX(), e.getY()); - try { - p = this.tx.inverseTransform(p, null); - } catch (NoninvertibleTransformException ex) {} - try { - int x = (int) p.getX()/tileSize; - int y = (int) p.getY()/tileSize; - this.map.getTile(x, y); - if (this.placingTile != Tile.Type.NONE) { - this.map.getTile(x, y).setType(this.placingTile); - } else { - if (!this.nukeActor) { - Actor actor = this.placingActor; - actor.place(x, y); - this.map.addActor(actor); - } else { - Actor toNuke = this.placingActor; - for (Actor actor : this.map.getActors()) { - if (x == actor.getX() && y == actor.getY()) { - toNuke = actor; - } - } - this.map.removeActor(toNuke); - } - } - } catch (ArrayIndexOutOfBoundsException ex) { - System.out.println("no tile clicked"); - } catch (NullPointerException ext) { - System.out.println("map non existent"); - } - } - - @Override - public void actionPerformed(ActionEvent e) { - if ("new".equals(e.getActionCommand())) { - JTextField size = new JTextField(); - - JComponent[] inputs = new JComponent[] { - new JLabel("Size"), - size, - }; - - int result = JOptionPane.showConfirmDialog(null, inputs, "New map", JOptionPane.DEFAULT_OPTION); - if (result == JOptionPane.OK_OPTION) { - int sizeN = Integer.parseInt(size.getText()); - this.map = new Map(new Dimension(sizeN, sizeN)); - } - } else if ("tile".equals(e.getActionCommand())) { - String[] values = {"Clear", "Grass", "Water", "Mountain", "Actor", "Remove Actor"}; - - Object result = JOptionPane.showInputDialog(null, "Tile Type", "Tile Type Selector", JOptionPane.DEFAULT_OPTION, null, values, "0"); - if (result != null) { - String selected = result.toString(); - switch (selected) { - case "Clear": - this.placingTile = Tile.Type.CLEAR; - break; - case "Grass": - this.placingTile = Tile.Type.GRASS; - break; - case "Water": - this.placingTile = Tile.Type.WATER; - break; - case "Mountain": - this.placingTile = Tile.Type.MOUNTAIN; - break; - case "Actor": - this.placingTile = Tile.Type.NONE; - this.nukeActor = false; - break; - case "Remove Actor": - this.placingTile = Tile.Type.NONE; - this.nukeActor = true; - break; - - } - } - } else if ("export".equals(e.getActionCommand())) { - JFileChooser fc = new JFileChooser(); - fc.showSaveDialog(null); - - MapLoader mapLoader = new MapLoader(fc.getSelectedFile()); - mapLoader.saveMap(this.map); - } else if ("import".equals(e.getActionCommand())) { - JFileChooser fc = new JFileChooser(); - fc.showOpenDialog(null); - - MapLoader mapLoader = new MapLoader(fc.getSelectedFile()); - this.map = mapLoader.getMap(); - } else if ("actors".equals(e.getActionCommand())) { - JFrame actorFrame = new JFrame("Actors"); - actorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - actorFrame.setSize(new Dimension(300, 400)); - actorFrame.setLocationRelativeTo(null); - - JPanel panel = new JPanel(); - panel.setLayout(new GridLayout(5,2)); - - JLabel lName = new JLabel("Name"); - JTextField name = new JTextField(); - JLabel lAgility = new JLabel("Agility"); - JTextField agility = new JTextField(); - JLabel lHp = new JLabel("max HP"); - JTextField hp = new JTextField(); - JCheckBox enemy = new JCheckBox("Enemy"); - JLabel empty = new JLabel(); - JButton ok = new JButton("Confirm"); - ok.setActionCommand("actOk"); - JButton cancel = new JButton("Cancel"); - cancel.setActionCommand("actCancel"); - - ArrayList actorFields = new ArrayList<>(); - actorFields.add(name); - actorFields.add(hp); - actorFields.add(agility); - actorFields.add(enemy); - this.actorFields = actorFields; - - ok.addActionListener(this); - cancel.addActionListener(this); - - panel.add(lName); - panel.add(name); - panel.add(lAgility); - panel.add(agility); - panel.add(lHp); - panel.add(hp); - panel.add(enemy); - panel.add(empty); - panel.add(ok); - panel.add(cancel); - - actorFrame.add(panel); - actorFrame.pack(); - actorFrame.setVisible(true); - - this.actorFrame = actorFrame; - } else if ("actOk".equals(e.getActionCommand())) { - JTextField nameField = (JTextField) this.actorFields.get(0); - String name = nameField.getText(); - JTextField hpField = (JTextField) this.actorFields.get(1); - int hp = Integer.parseInt(hpField.getText()); - JTextField agilityField = (JTextField) this.actorFields.get(2); - int agility = Integer.parseInt(agilityField.getText()); - JCheckBox enemyBox = (JCheckBox) this.actorFields.get(3); - Actor actor = new Actor(name, hp, enemyBox.isSelected(), agility); - this.placingActor = actor; - this.actorFrame.setVisible(false); - this.actorFrame.dispose(); - } else if ("actCancel".equals(e.getActionCommand())) { - this.actorFrame.setVisible(false); - this.actorFrame.dispose(); - } - - } -} diff --git a/src/MapEditorScene.java b/src/MapEditorScene.java new file mode 100644 index 0000000..4a42951 --- /dev/null +++ b/src/MapEditorScene.java @@ -0,0 +1,235 @@ +import java.util.ArrayList; + +import java.awt.Canvas; +import java.awt.Graphics2D; +import java.awt.Dimension; +import java.awt.BorderLayout; +import java.awt.GridLayout; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.event.MouseWheelEvent; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferStrategy; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.NoninvertibleTransformException; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JOptionPane; +import javax.swing.JLabel; +import javax.swing.JTextField; +import javax.swing.JFileChooser; +import javax.swing.JCheckBox; + +import java.lang.Integer; + +public class MapEditorScene extends MapScene { + private Tile.Type placingTile = Tile.Type.CLEAR; + private Actor placingActor = new Actor("", 0, false, 0); + private ArrayList actorFields = new ArrayList<>(); + private JFrame actorFrame; + private boolean nukeActor = false; + + public MapEditorScene() { + super(); + this.setLayout(new BorderLayout()); + + JPanel bottomPanel = new JPanel(); + bottomPanel.setLayout(new GridLayout(1,5)); + + JButton newButton = new JButton("New"); + newButton.setActionCommand("new"); + JButton tileButton = new JButton("Placing selector"); + tileButton.setActionCommand("tile"); + JButton exportButton = new JButton("Export"); + exportButton.setActionCommand("export"); + JButton importButton = new JButton("Import"); + importButton.setActionCommand("import"); + JButton actorButton = new JButton("Actors"); + actorButton.setActionCommand("actors"); + + newButton.addActionListener(this); + tileButton.addActionListener(this); + actorButton.addActionListener(this); + exportButton.addActionListener(this); + importButton.addActionListener(this); + + bottomPanel.add(newButton); + bottomPanel.add(tileButton); + bottomPanel.add(actorButton); + bottomPanel.add(exportButton); + bottomPanel.add(importButton); + + this.add(bottomPanel, BorderLayout.PAGE_END); + this.add(this.canvas, BorderLayout.CENTER); + } + + @Override + protected void update(int deltaTime) {} + + @Override + protected void absoluteRender(Graphics2D g2d) {} + + @Override + public void mouseClicked(MouseEvent e) { + int tileSize = this.maxSize/10; + Point2D p = new Point2D.Double(e.getX(), e.getY()); + try { + p = this.tx.inverseTransform(p, null); + } catch (NoninvertibleTransformException ex) {} + try { + int x = (int) p.getX()/tileSize; + int y = (int) p.getY()/tileSize; + this.map.getTile(x, y); + if (this.placingTile != Tile.Type.NONE) { + this.map.getTile(x, y).setType(this.placingTile); + } else { + if (!this.nukeActor) { + Actor actor = this.placingActor; + actor.place(x, y); + this.map.addActor(actor); + } else { + Actor toNuke = this.placingActor; + for (Actor actor : this.map.getActors()) { + if (x == actor.getX() && y == actor.getY()) { + toNuke = actor; + } + } + this.map.removeActor(toNuke); + } + } + } catch (ArrayIndexOutOfBoundsException ex) { + System.out.println("no tile clicked"); + } catch (NullPointerException ext) { + System.out.println("map non existent"); + } + } + + @Override + public void actionPerformed(ActionEvent e) { + if ("new".equals(e.getActionCommand())) { + JTextField size = new JTextField(); + + JComponent[] inputs = new JComponent[] { + new JLabel("Size"), + size, + }; + + int result = JOptionPane.showConfirmDialog(null, inputs, "New map", JOptionPane.DEFAULT_OPTION); + if (result == JOptionPane.OK_OPTION) { + int sizeN = Integer.parseInt(size.getText()); + this.map = new Map(new Dimension(sizeN, sizeN)); + } + } else if ("tile".equals(e.getActionCommand())) { + String[] values = {"Clear", "Grass", "Water", "Mountain", "Actor", "Remove Actor"}; + + Object result = JOptionPane.showInputDialog(null, "Tile Type", "Tile Type Selector", JOptionPane.DEFAULT_OPTION, null, values, "0"); + if (result != null) { + String selected = result.toString(); + switch (selected) { + case "Clear": + this.placingTile = Tile.Type.CLEAR; + break; + case "Grass": + this.placingTile = Tile.Type.GRASS; + break; + case "Water": + this.placingTile = Tile.Type.WATER; + break; + case "Mountain": + this.placingTile = Tile.Type.MOUNTAIN; + break; + case "Actor": + this.placingTile = Tile.Type.NONE; + this.nukeActor = false; + break; + case "Remove Actor": + this.placingTile = Tile.Type.NONE; + this.nukeActor = true; + break; + + } + } + } else if ("export".equals(e.getActionCommand())) { + JFileChooser fc = new JFileChooser(); + fc.showSaveDialog(null); + + MapLoader mapLoader = new MapLoader(fc.getSelectedFile()); + mapLoader.saveMap(this.map); + } else if ("import".equals(e.getActionCommand())) { + JFileChooser fc = new JFileChooser(); + fc.showOpenDialog(null); + + MapLoader mapLoader = new MapLoader(fc.getSelectedFile()); + this.map = mapLoader.getMap(); + } else if ("actors".equals(e.getActionCommand())) { + JFrame actorFrame = new JFrame("Actors"); + actorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + actorFrame.setSize(new Dimension(300, 400)); + actorFrame.setLocationRelativeTo(null); + + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(5,2)); + + JLabel lName = new JLabel("Name"); + JTextField name = new JTextField(); + JLabel lAgility = new JLabel("Agility"); + JTextField agility = new JTextField(); + JLabel lHp = new JLabel("max HP"); + JTextField hp = new JTextField(); + JCheckBox enemy = new JCheckBox("Enemy"); + JLabel empty = new JLabel(); + JButton ok = new JButton("Confirm"); + ok.setActionCommand("actOk"); + JButton cancel = new JButton("Cancel"); + cancel.setActionCommand("actCancel"); + + ArrayList actorFields = new ArrayList<>(); + actorFields.add(name); + actorFields.add(hp); + actorFields.add(agility); + actorFields.add(enemy); + this.actorFields = actorFields; + + ok.addActionListener(this); + cancel.addActionListener(this); + + panel.add(lName); + panel.add(name); + panel.add(lAgility); + panel.add(agility); + panel.add(lHp); + panel.add(hp); + panel.add(enemy); + panel.add(empty); + panel.add(ok); + panel.add(cancel); + + actorFrame.add(panel); + actorFrame.pack(); + actorFrame.setVisible(true); + + this.actorFrame = actorFrame; + } else if ("actOk".equals(e.getActionCommand())) { + JTextField nameField = (JTextField) this.actorFields.get(0); + String name = nameField.getText(); + JTextField hpField = (JTextField) this.actorFields.get(1); + int hp = Integer.parseInt(hpField.getText()); + JTextField agilityField = (JTextField) this.actorFields.get(2); + int agility = Integer.parseInt(agilityField.getText()); + JCheckBox enemyBox = (JCheckBox) this.actorFields.get(3); + Actor actor = new Actor(name, hp, enemyBox.isSelected(), agility); + this.placingActor = actor; + this.actorFrame.setVisible(false); + this.actorFrame.dispose(); + } else if ("actCancel".equals(e.getActionCommand())) { + this.actorFrame.setVisible(false); + this.actorFrame.dispose(); + } + + } +} diff --git a/src/MapScene.java b/src/MapScene.java index af86431..7ed8e3b 100644 --- a/src/MapScene.java +++ b/src/MapScene.java @@ -46,15 +46,14 @@ public abstract class MapScene extends Scene implements ActionListener { protected int maxSize; protected AffineTransform tx = new AffineTransform(); - public MapScene(JFrame frame) { - super(frame); - + public MapScene() { + super(); //this.map = new Map(new Dimension(10, 10)); } @Override protected void render() { - if (this.WIDTH < this.HEIGHT) { + if (this.canvasSize.width < this.canvasSize.height) { this.maxSize = WIDTH; } else { this.maxSize = HEIGHT; @@ -68,7 +67,7 @@ public abstract class MapScene extends Scene implements ActionListener { Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics(); //clear g.setColor(Palette.BLACK); - g.fillRect(0, 0, this.WIDTH, this.HEIGHT); + g.fillRect(0, 0, this.canvasSize.width, this.canvasSize.height); //zoom and pan if (this.zooming) { diff --git a/src/Scene.java b/src/Scene.java index 529a2f6..d8c826e 100644 --- a/src/Scene.java +++ b/src/Scene.java @@ -23,28 +23,20 @@ public abstract class Scene extends JPanel protected final long DESIRED_FPS = 60; protected final long DESIRED_DELTA_LOOP = (1000*1000*1000)/DESIRED_FPS; - // TODO: lowercase not constant values - protected int WIDTH; - protected int HEIGHT; protected BufferStrategy buffer; - // TODO: remove frame reference - protected JFrame frame; + protected Dimension canvasSize; protected boolean running; protected Canvas canvas; // TODO: make accessible from user settings protected int guiSize = 2; - protected Scene(JFrame frame) { - this.frame = frame; - // getting window size - this.WIDTH = (int) this.frame.getSize().getWidth(); - this.HEIGHT = (int) this.frame.getSize().getHeight(); - + protected Scene() { + this.canvasSize = GameWindow.WINDOW_SIZE; this.canvas = new Canvas(); - this.canvas.setBounds(0, 0, this.WIDTH, this.HEIGHT); + this.canvas.setBounds(0, 0, this.canvasSize.width, this.canvasSize.height); this.canvas.setIgnoreRepaint(true); this.canvas.addKeyListener(this); @@ -74,11 +66,6 @@ public abstract class Scene extends JPanel while (running) { beginLoopTime = System.nanoTime(); - // TODO: use WindowEventListener to wait for changes - //getting window size - this.WIDTH = (int) this.frame.getSize().getWidth(); - this.HEIGHT = (int) this.frame.getSize().getHeight(); - this.render(); lastUpdateTime = currentUpdateTime; @@ -100,6 +87,15 @@ public abstract class Scene extends JPanel } } + // automagically set the canvas size to the parent's size + public void updateCanvasSize() { + this.canvasSize = this.getParent().getSize(); + } + + public void setCanvasSize(Dimension newSize) { + this.canvasSize = newSize; + } + protected abstract void render(); protected abstract void update(int deltaNanoTime); -- cgit v1.2.1