From c8d2d5ae25daa62d3e71a29932c836676f55584a Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 27 Apr 2021 14:24:57 +0200 Subject: [PATCH] Improve WindowMouseListener.java --- .../control/WindowMouseListener.java | 21 +++++++++++++++++-- src/battleship/view/Window.java | 5 ++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/battleship/control/WindowMouseListener.java b/src/battleship/control/WindowMouseListener.java index 667d28b..dfe9c78 100644 --- a/src/battleship/control/WindowMouseListener.java +++ b/src/battleship/control/WindowMouseListener.java @@ -1,5 +1,6 @@ package battleship.control; +import battleship.utils.Pair; import battleship.view.Window; import java.awt.event.MouseEvent; @@ -15,8 +16,24 @@ public class WindowMouseListener implements MouseListener { @Override public void mouseClicked(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); + int x = e.getX() - 7; + int y = e.getY() - 30; + int initialHeight = window.height / 12; + int initialWidth = window.width / 23; + if(y >= initialHeight * 2 && y <= window.height) { + y -= initialHeight * 2; + if(x >= initialWidth && x <= initialWidth * 11) { + x -= initialWidth; + System.out.println("Player 1"); + Pair location = new Pair<>(y / initialHeight, x / initialWidth); + System.out.println(location); + } else if(x >= initialHeight * 13 && x <= window.width) { + x -= initialWidth * 13; + System.out.println("Player 2"); + Pair location = new Pair<>(y / initialHeight, x / initialWidth); + System.out.println("location: " + location); + } + } System.out.println("(" + x + ", " + y + ")"); } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 5849ee7..9efb945 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -32,10 +32,10 @@ public class Window extends AbstractView { @Override public void setShips(Player player) { - upperText = "Placez votre navire joueur " + player.getId(); if(player instanceof Human) { for(int i : shipsSize) { - upperText += "Placez votre premier navire de taille " + i + " à l'aide de la souris"; + upperText = "joueur " + player.getId() + ", Placez votre premier navire de taille " + i + " à l'aide de la souris"; + } } else { @@ -82,7 +82,6 @@ public class Window extends AbstractView { g.drawLine(initialWidth, ordonnee, initialWidth * 11, ordonnee); g.drawLine(initialWidth * 13, ordonnee, width - 4, ordonnee); } - // TODO: 12/04/2021 Dessiner les navires for(int i = 1; i < 3; ++i) { Player player = game.players[i-1];