Improve WindowMouseListener.java

This commit is contained in:
Quentin Legot 2021-04-27 14:24:57 +02:00
parent fb636c427e
commit c8d2d5ae25
2 changed files with 21 additions and 5 deletions

View File

@ -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<Integer, Integer> 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<Integer, Integer> location = new Pair<>(y / initialHeight, x / initialWidth);
System.out.println("location: " + location);
}
}
System.out.println("(" + x + ", " + y + ")");
}

View File

@ -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];