display ships on window + add mouse listener
This commit is contained in:
parent
43ab7ebfa7
commit
c43b200888
@ -36,7 +36,7 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||||
Player[] players = new AbstractPlayer[2];
|
Player[] players = new Player[2];
|
||||||
ArrayList<Pair<String, Class<? extends AbstractPlayer>>> playerClass = new ArrayList<>(2);
|
ArrayList<Pair<String, Class<? extends AbstractPlayer>>> playerClass = new ArrayList<>(2);
|
||||||
playerClass.add(new Pair<>("human", Human.class));
|
playerClass.add(new Pair<>("human", Human.class));
|
||||||
playerClass.add(new Pair<>("random", Random.class));
|
playerClass.add(new Pair<>("random", Random.class));
|
||||||
|
39
src/battleship/control/WindowMouseListener.java
Normal file
39
src/battleship/control/WindowMouseListener.java
Normal file
@ -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) {}
|
||||||
|
|
||||||
|
}
|
@ -8,7 +8,7 @@ import battleship.utils.Triplet;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public abstract class AbstractView implements View{
|
public abstract class AbstractView implements View {
|
||||||
|
|
||||||
|
|
||||||
protected Game game;
|
protected Game game;
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package battleship.view;
|
package battleship.view;
|
||||||
|
|
||||||
|
import battleship.control.WindowMouseListener;
|
||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
|
import battleship.model.Ship;
|
||||||
|
import battleship.model.player.Human;
|
||||||
import battleship.model.player.Player;
|
import battleship.model.player.Player;
|
||||||
import battleship.utils.Pair;
|
import battleship.utils.Pair;
|
||||||
|
|
||||||
@ -9,24 +12,35 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class Window extends AbstractView {
|
public class Window extends AbstractView {
|
||||||
|
|
||||||
private final JFrame frame;
|
final JFrame frame;
|
||||||
|
|
||||||
private final int height = 600;
|
public final int height = 600;
|
||||||
private final int width = 1200;
|
public final int width = 1200;
|
||||||
|
String upperText = "";
|
||||||
|
|
||||||
public Window(Game game) {
|
public Window(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
this.frame = new JFrame("Battleship");
|
this.frame = new JFrame("Battleship");
|
||||||
frame.setSize(width + width / 13, height + height / 4);
|
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.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
|
frame.addMouseListener(new WindowMouseListener(this));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setShips(Player player) {
|
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
|
@Override
|
||||||
@ -46,14 +60,20 @@ public class Window extends AbstractView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Draw extends JPanel {
|
class Draw extends JPanel {
|
||||||
|
|
||||||
|
private final Window window;
|
||||||
|
|
||||||
|
public Draw(Window window) {
|
||||||
|
this.window = window;
|
||||||
|
}
|
||||||
|
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
/*JTextArea area = new JTextArea();
|
g.drawString(upperText, (int) (window.width /2 - (upperText.length() * 2.5)), 50);
|
||||||
area.setBounds(20,10,400,20);
|
int width = window.width;
|
||||||
//area.append("A B C D E F G H I J");
|
int height = window.height;
|
||||||
frame.add(area);*/
|
|
||||||
for (int abscisse = width / 24; abscisse< width +1; abscisse+= width / 24) {
|
for (int abscisse = width / 24; abscisse< width +1; abscisse+= width / 24) {
|
||||||
g.drawLine(abscisse, height /6, abscisse, height);
|
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 ;
|
abscisse += width / 24 ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,9 +81,50 @@ public class Window extends AbstractView {
|
|||||||
g.drawLine(width / 24, ordonnee, (int) (width /2.18), ordonnee);
|
g.drawLine(width / 24, ordonnee, (int) (width /2.18), ordonnee);
|
||||||
g.drawLine((int) (width / 1.845), ordonnee, width, ordonnee);
|
g.drawLine((int) (width / 1.845), ordonnee, width, ordonnee);
|
||||||
}
|
}
|
||||||
TextArea a = new TextArea("Aouiuxdytftgykhulijhguhghf");
|
// TODO: 12/04/2021 Dessiner les navires
|
||||||
setLayout(new GridLayout(width, height));
|
int initialHeight = height / 12;
|
||||||
a.replaceRange("dqsdqsfdqsd", 0, 1);
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user