From c43b2008881711ca6fcdb4a9295e34bd1cacd2c8 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 22 Apr 2021 12:26:17 +0200 Subject: [PATCH] display ships on window + add mouse listener --- src/battleship/Main.java | 2 +- .../control/WindowMouseListener.java | 39 +++++++++ src/battleship/view/AbstractView.java | 2 +- src/battleship/view/Window.java | 85 ++++++++++++++++--- 4 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 src/battleship/control/WindowMouseListener.java diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 4443464..be3ae37 100644 --- a/src/battleship/Main.java +++ b/src/battleship/Main.java @@ -36,7 +36,7 @@ public class Main { } private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Player[] players = new AbstractPlayer[2]; + Player[] players = new Player[2]; ArrayList>> playerClass = new ArrayList<>(2); playerClass.add(new Pair<>("human", Human.class)); playerClass.add(new Pair<>("random", Random.class)); diff --git a/src/battleship/control/WindowMouseListener.java b/src/battleship/control/WindowMouseListener.java new file mode 100644 index 0000000..667d28b --- /dev/null +++ b/src/battleship/control/WindowMouseListener.java @@ -0,0 +1,39 @@ +package battleship.control; + +import battleship.view.Window; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +public class WindowMouseListener implements MouseListener { + + private final Window window; + + public WindowMouseListener(Window view) { + this.window = view; + } + + @Override + public void mouseClicked(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + System.out.println("(" + x + ", " + y + ")"); + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent ignored) {} + + @Override + public void mouseExited(MouseEvent ignored) {} + +} diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index c71f46f..e64e048 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -8,7 +8,7 @@ import battleship.utils.Triplet; import java.util.ArrayList; -public abstract class AbstractView implements View{ +public abstract class AbstractView implements View { protected Game game; diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 5441758..3425ddb 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -1,6 +1,9 @@ package battleship.view; +import battleship.control.WindowMouseListener; import battleship.model.Game; +import battleship.model.Ship; +import battleship.model.player.Human; import battleship.model.player.Player; import battleship.utils.Pair; @@ -9,24 +12,35 @@ import java.awt.*; public class Window extends AbstractView { - private final JFrame frame; + final JFrame frame; - private final int height = 600; - private final int width = 1200; + public final int height = 600; + public final int width = 1200; + String upperText = ""; public Window(Game game) { super(game); this.frame = new JFrame("Battleship"); frame.setSize(width + width / 13, height + height / 4); - frame.setContentPane(new Draw()); + frame.setResizable(false); + frame.setContentPane(new Draw(this)); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); + frame.addMouseListener(new WindowMouseListener(this)); } @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"; + } + } else { + super.setShips(player); + } } @Override @@ -46,14 +60,20 @@ public class Window extends AbstractView { } class Draw extends JPanel { + + private final Window window; + + public Draw(Window window) { + this.window = window; + } + public void paintComponent(Graphics g) { - /*JTextArea area = new JTextArea(); - area.setBounds(20,10,400,20); - //area.append("A B C D E F G H I J"); - frame.add(area);*/ + g.drawString(upperText, (int) (window.width /2 - (upperText.length() * 2.5)), 50); + int width = window.width; + int height = window.height; for (int abscisse = width / 24; abscisse< width +1; abscisse+= width / 24) { g.drawLine(abscisse, height /6, abscisse, height); - if ( width * 0.44167 < abscisse && abscisse < (width >> 1)) { + if ( width * 0.44167 < abscisse && abscisse < width / 2) { abscisse += width / 24 ; } } @@ -61,9 +81,50 @@ public class Window extends AbstractView { g.drawLine(width / 24, ordonnee, (int) (width /2.18), ordonnee); g.drawLine((int) (width / 1.845), ordonnee, width, ordonnee); } - TextArea a = new TextArea("Aouiuxdytftgykhulijhguhghf"); - setLayout(new GridLayout(width, height)); - a.replaceRange("dqsdqsfdqsd", 0, 1); + // TODO: 12/04/2021 Dessiner les navires + int initialHeight = height / 12; + for(int i = 1; i < 3; ++i) { + int initialWidth = width / 24; + Player player = game.players[i-1]; + System.out.println(i); + for(Ship ship : player.getShips()) { + int x1 = 0; + int y1 = 0; + int shipWidth = 0; + int shipHeight = 0; + switch(ship.getDirection()) { + case DOWN: + x1 = initialWidth * ship.getCoords().getRight(); + y1 = initialHeight * ship.getCoords().getLeft(); + shipWidth = initialWidth; + shipHeight = initialHeight * ship.getSize(); + break; + case UP: + shipWidth = initialWidth; + shipHeight = initialHeight * ship.getSize(); + x1 = initialWidth * ship.getCoords().getRight(); + y1 = initialHeight * ship.getCoords().getLeft() - shipHeight; + break; + case RIGHT: + x1 = initialWidth * ship.getCoords().getRight(); + y1 = initialHeight * ship.getCoords().getLeft(); + shipWidth = initialWidth * ship.getSize(); + shipHeight = initialHeight; + break; + case LEFT: + shipWidth = initialWidth * ship.getSize(); + shipHeight = initialHeight; + x1 = initialWidth * ship.getCoords().getRight() - shipWidth; + y1 = initialHeight * ship.getCoords().getLeft(); + break; + } + x1 += i == 1 ? initialWidth : initialWidth + width / 2; + y1 += height / 6; + g.setColor(new Color(255, 0, 0)); + g.fillRect(x1, y1, shipWidth, shipHeight); + } + } + System.out.println(window.toString()); } } }