From 221aec874940378f5d8b6411e8b5c141816d5a1e Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 10 Feb 2018 19:40:08 +0100 Subject: Add simple mouseclick listener to scenes --- src/main/java/Subconscious.java | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/main/java/Subconscious.java') diff --git a/src/main/java/Subconscious.java b/src/main/java/Subconscious.java index 3e66800..d462385 100644 --- a/src/main/java/Subconscious.java +++ b/src/main/java/Subconscious.java @@ -42,21 +42,23 @@ public class Subconscious public Subconscious() { createWindow(); + // setup component + addMouseListener(this); + addKeyListener(this); + addComponentListener(this); setFocusable(true); requestFocus(); // TODO remove demo currentScene = new WorldScene(new Dimension(200, 200), 50); - addMouseListener(this); - addKeyListener(this); - addComponentListener(this); } private void createWindow() { window = new JFrame(TITLE); window.setSize(new Dimension(800, 600)); window.setMinimumSize(getSize()); - window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(this); + window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.add(this); } /* game logic */ @@ -99,19 +101,15 @@ public class Subconscious while (running) { gameLoop(); gameRender(); - repaint(); - + repaint(); // could be better with NanoSeconds, but this is // probably enough for this afterTime = System.currentTimeMillis(); sleepTime = RENDER_PERIOD_MS - (beforeTime - afterTime); try { - // TODO replace with correct timing Thread.sleep(sleepTime); - } catch (InterruptedException ex) { - // - } + } catch (InterruptedException ex) {} beforeTime = System.currentTimeMillis(); } @@ -127,7 +125,7 @@ public class Subconscious g.drawImage(dbImage, 0, 0, null); } - Toolkit.getDefaultToolkit().sync(); + Toolkit.getDefaultToolkit().sync(); // needed on linux g.dispose(); } @@ -137,8 +135,7 @@ public class Subconscious @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void mouseClicked(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); + currentScene.mouseClicked(e.getX(), e.getY()); } @Override public void keyPressed(KeyEvent e) {} -- cgit v1.2.1