From abc02f94e7e94b34c955fc6b61bd16d0d8c178f3 Mon Sep 17 00:00:00 2001 From: Eideen Date: Thu, 29 Apr 2021 11:41:43 +0200 Subject: [PATCH] Use arrow for ship placement --- .../control/WindowKeyboardListener.java | 4 ++++ src/battleship/model/Direction.java | 20 +++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/battleship/control/WindowKeyboardListener.java b/src/battleship/control/WindowKeyboardListener.java index 20c00b3..6409616 100644 --- a/src/battleship/control/WindowKeyboardListener.java +++ b/src/battleship/control/WindowKeyboardListener.java @@ -10,6 +10,7 @@ public class WindowKeyboardListener implements KeyListener { private final Window window; public boolean requestInput = false; public char keyTyped = KeyEvent.CHAR_UNDEFINED; + public int keyTypedArrow = KeyEvent.VK_UNDEFINED; public WindowKeyboardListener(Window window) { this.window = window; @@ -20,6 +21,9 @@ public class WindowKeyboardListener implements KeyListener { if(requestInput) { if(e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) keyTyped = e.getKeyChar(); + if(e.getKeyCode() != KeyEvent.VK_UNDEFINED) + System.out.println(e.getKeyCode()); + keyTypedArrow = e.getKeyCode(); } } diff --git a/src/battleship/model/Direction.java b/src/battleship/model/Direction.java index d79420f..c41f770 100644 --- a/src/battleship/model/Direction.java +++ b/src/battleship/model/Direction.java @@ -1,21 +1,25 @@ package battleship.model; import battleship.utils.Pair; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; public enum Direction { - RIGHT(new Pair<>(0, 1), "D"), - LEFT(new Pair<>(0,-1), "G"), - UP(new Pair<>(-1,0), "H"), - DOWN(new Pair<>(1,0), "B"), - DEFAULT(new Pair<>(-1,-1), null); + RIGHT(new Pair<>(0, 1), "D", KeyEvent.VK_RIGHT), + LEFT(new Pair<>(0,-1), "G", KeyEvent.VK_LEFT), + UP(new Pair<>(-1,0), "H", KeyEvent.VK_UP), + DOWN(new Pair<>(1,0), "B", KeyEvent.VK_DOWN), + DEFAULT(new Pair<>(-1,-1), null, KeyEvent.VK_UNDEFINED); private final Pair direction; private final String keyword; + private final int arrow; - Direction(Pair ukPair, String keyword) { + Direction(Pair ukPair, String keyword, int arrow) { this.direction = ukPair; this.keyword = keyword; + this.arrow = arrow; } public Pair getDirection() { @@ -25,4 +29,8 @@ public enum Direction { public String getKeyword() { return keyword; } + + public int getArrow() { + return arrow; + } }