Commit bf0c51bf authored by Hugo DACLON's avatar Hugo DACLON
Browse files

Modification de presque toutes les classes

pour améliorer la lisibilité et avancement des
vue menu, Creation et Resultat ainsi que
de leurs controlleurs
parent e5c7a35c
package info1.Vue;
import info1.application.appli;
import javax.swing.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import info1.application.appli;
public class ChoixModePartie extends JDialog {
......@@ -66,7 +72,6 @@ public class ChoixModePartie extends JDialog {
this.pack();
this.setResizable(false);
this.setAlwaysOnTop(true);
}
public void fixeListenerEtreHost(ActionListener action) { etreHost.addActionListener(action); }//OptPartie.afficherheberger
......
package info1.Vue;
import javax.swing.*;
import info1.ships.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
public class CreationFlotte extends JPanel {//JPanel {
private JButton notreGrille;
private JButton listeBateaux;
private JButton pret;
private JButton reset;
private JButton annuler;
private NavyFleet flotte;
private boolean version;
public NavyFleet getVersion() {
return flotte;
}
public void setVersion(boolean version) {
this.flotte = flotte;
}
public NavyFleet getFlotte() {
return flotte;
}
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import info1.application.appli;
import info1.ships.AircraftCarrier;
import info1.ships.BadCoordException;
import info1.ships.Battleship;
import info1.ships.Coord;
import info1.ships.CoordsBadShipException;
import info1.ships.Cruiser;
import info1.ships.Destroyer;
import info1.ships.ICoord;
import info1.ships.IShip;
import info1.ships.NavyFleet;
import info1.ships.Ship;
import info1.ships.Submarine;
public void setFlotte(NavyFleet flotte) {
this.flotte = flotte;
}
public class CreationFlotte extends JPanel {// JPanel {
private static final JLabel[] LISTE_BATEAUX_V_BE = new JLabel[] { new JLabel("Cuirrassé"), new JLabel("Croiseur"),
new JLabel("Croiseur"), new JLabel("Destroyer"), new JLabel("Destroyer"), new JLabel("Destroyer"),
new JLabel("Sous-Marin"), new JLabel("Sous-Marin"), new JLabel("Sous-Marin"), new JLabel("Sous-Marin") };
private static final JLabel[] LISTE_BATEAUX_V_FR = new JLabel[] { new JLabel("Porte-Avion"),
new JLabel("Cuirrassé"), new JLabel("Croiseur"), new JLabel("Croiseur"), new JLabel("Destroyer"),
new JLabel("Destroyer"), new JLabel("Sous-Marin") };
private static final Dimension DIMENSION_MAP = new Dimension(500, 500);
private static final Dimension DIMENSION_LISTE = new Dimension(250, 500);
// ! TODO:changer les dimension en fonction de la taille des images
private static final int VGAP = 50;
private static final int WIDTH = (int) (DIMENSION_MAP.getWidth() + DIMENSION_LISTE.getWidth());
private static final Dimension DIMENSION_READY = new Dimension((WIDTH - 2 * VGAP) / 3, 100);
private static final Dimension DIMENSION_RESET = new Dimension((WIDTH - 2 * VGAP) / 3, 100);
private static final Dimension DIMENSION_RETOUR = new Dimension((WIDTH - 2 * VGAP) / 3, 100);
private static final Dimension DIMENSION_REFRESH = new Dimension(32, 32);
private Map map;
private LinkedList<JLabel> bateauxRestant;
private JPanel liste;
private JButton ready;
private JButton reset;
private JButton retour;
private JButton refresh;
private Coord selectedCell;
public CreationFlotte(String titre) {
super(new BorderLayout());
this.setName(titre);
notreGrille = new JButton("Insérer grille ici");
listeBateaux = new JButton("Liste des bateaux à placer");
pret = new JButton("Prêt pour la bataille !");
for (JLabel label : LISTE_BATEAUX_V_BE) {
setIconLabelBateau(label);
}
for (JLabel label : LISTE_BATEAUX_V_FR) {
setIconLabelBateau(label);
}
ready = new JButton("Prêt pour la bataille !");
ready.setPreferredSize(DIMENSION_READY);
reset = new JButton("Recommencer le placement");
annuler = new JButton("Déserter");
ready.setPreferredSize(DIMENSION_RESET);
retour = new JButton("Déserter");
ready.setPreferredSize(DIMENSION_RETOUR);
refresh = new JButton(new ImageIcon("img/interface/bouton/BoutonRafraichirMap.png"));
refresh.setText("Raffraichir la carte");
refresh.setPreferredSize(DIMENSION_REFRESH);
JPanel grille = new JPanel();
JPanel liste = new JPanel();
JPanel boutons = new JPanel();
liste = new JPanel();
liste.setLayout(new BoxLayout(liste, BoxLayout.Y_AXIS));
JPanel boutons = new JPanel(new GridLayout(1, 3, 50, 50));
TitledBorder borderListe = new TitledBorder("Liste de bateaux à placer");
borderListe.setTitleColor(Color.WHITE);
map = new Map();
map.setPreferredSize(DIMENSION_MAP);
setVersion(appli.getVersion());
grille.add(map);
for (JLabel label : bateauxRestant) {
liste.add(label);
}
liste.setBorder(borderListe);
liste.setPreferredSize(DIMENSION_LISTE);
Insets insets = liste.getInsets();
refresh.setBounds(insets.right + (int) DIMENSION_REFRESH.getWidth(),
insets.bottom + (int) DIMENSION_REFRESH.getHeight(), (int) DIMENSION_REFRESH.getWidth(),
(int) DIMENSION_REFRESH.getHeight());
liste.add(refresh);
boutons.add(retour);
boutons.add(reset);
boutons.add(ready);
this.add(grille, BorderLayout.EAST);
this.add(liste, BorderLayout.WEST);
this.add(boutons, BorderLayout.SOUTH);
}
private JLabel setIconLabelBateau(JLabel label) {
try {
switch (label.getText()) {
case "Porte-Avion":
label.setIcon(new ImageIcon(
(ImageIO.read(new File("img/interface/bateaux/horizontal/AirCraftCarrier.png")))
.getScaledInstance(150, 30, Image.SCALE_SMOOTH)));
break;
case "Cuirrassé":
label.setIcon(
new ImageIcon((ImageIO.read(new File("img/interface/bateaux/horizontal/BattleShip.png")))
.getScaledInstance(120, 30, Image.SCALE_SMOOTH)));
break;
case "Croiseur":
label.setIcon(new ImageIcon((ImageIO.read(new File("img/interface/bateaux/horizontal/Cruiser.png")))
.getScaledInstance(90, 30, Image.SCALE_SMOOTH)));
break;
case "Destroyer":
label.setIcon(
new ImageIcon((ImageIO.read(new File("img/interface/bateaux/horizontal/Destroyer.png")))
.getScaledInstance(60, 30, Image.SCALE_SMOOTH)));
break;
case "Sous-Marin":
label.setIcon(
new ImageIcon((ImageIO.read(new File("img/interface/bateaux/horizontal/Submarine.png")))
.getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
break;
default:
throw new IOException();
}
} catch (IOException e) {
System.out.println("Erreur lors du chargement d'une image.");
e.printStackTrace();
}
label.setForeground(Color.WHITE);
return label;
}
GridLayout lay = new GridLayout(1,2);
lay.setHgap(50);
lay.setVgap(50);
grille.setLayout(lay);
public Object getSelectedCell() {
return selectedCell;
}
grille.setPreferredSize(new Dimension(675,675));
grille.add(notreGrille);
public void selectionCell(Coord cell) {
map.ajouterViseur(cell);
selectedCell = cell;
}
public void creerBateau(Coord cell) throws BadCoordException, CoordsBadShipException {
Ship ship;
map.resetCell(selectedCell);
switch (getDistanceBateau(cell)) {
case 1:
ship = new Submarine("Sous-Marin", selectedCell.toString());
break;
case 2:
ship = new Destroyer("Destroyer", selectedCell.toString(), cell.toString());
break;
case 3:
ship = new Cruiser("Sous-Marin", selectedCell.toString(), cell.toString());
break;
case 4:
ship = new Battleship("Sous-Marin", selectedCell.toString(), cell.toString());
break;
case 5:
ship = new AircraftCarrier("Sous-Marin", selectedCell.toString(), cell.toString());
break;
default:
selectedCell = null;
throw new CoordsBadShipException();
}
selectedCell = null;
appli.getFlotte().add(ship);
map.ajouterBateau(ship);
}
lay = new GridLayout(1,1);
lay.setHgap(50);
lay.setVgap(50);
liste.setLayout(lay);
private boolean isVertical(Coord cell) {
return (selectedCell.getX() == cell.getX());
}
liste.setPreferredSize(new Dimension(250,250));
liste.add(listeBateaux);
private boolean isHorizontal(Coord cell) {
return (selectedCell.getY() == cell.getY());
}
private int getDistanceBateau(Coord cell) throws CoordsBadShipException {
if (isVertical(cell)) {
return Math.abs(cell.getY() - selectedCell.getY()) + 1;
} else if (isHorizontal(cell)) {
return Math.abs(cell.getX() - selectedCell.getX()) + 1;
} else {
selectedCell = null;
throw new CoordsBadShipException();
}
}
lay = new GridLayout(1,3);
lay.setHgap(50);
lay.setVgap(50);
boutons.setLayout(lay);
public void effacerBateau(Coord cell) {
for (IShip ship : appli.getFlotte().getShips()) {
if (ship.getCoords().contains(cell)) {
for (ICoord c : ship.getCoords()) {
map.resetCell(c);
}
NavyFleet newFlotte = new NavyFleet();
for (IShip newFlotteShip : appli.getFlotte().getShips()) {
if (!newFlotteShip.equals(ship))
newFlotte.add(newFlotteShip);
}
appli.setFlotte(newFlotte);
return;
}
}
}
boutons.setPreferredSize(new Dimension(100,100));
boutons.add(annuler);
boutons.add(reset);
boutons.add(pret);
public LinkedList<JLabel> getBateauxRestants() {
return bateauxRestant;
}
public void setVersion(boolean version) {
appli.setVersion(version);
if (appli.getFlotte().isFrenchConfiguration() != appli.getVersion() && appli.getFlotte().isComplete()) {
appli.setFlotte(new NavyFleet());
}
reset.setText(version ? "passer en Belge" : "Passer en Français");
map.resetGrille();
if (!appli.getFlotte().getShips().isEmpty()) {
map.ajouterFlotte(appli.getFlotte());
}
bateauxRestant = new LinkedList<JLabel>(
version ? Arrays.asList(LISTE_BATEAUX_V_FR) : Arrays.asList(LISTE_BATEAUX_V_BE));
}
this.add(grille, BorderLayout.EAST);
this.add(liste, BorderLayout.WEST);
this.add(boutons, BorderLayout.SOUTH);
public void fixeListenerOuvrirChoixPartie(ActionListener l) {
ready.addActionListener(l);
}
this.setPreferredSize(new Dimension(1000, 600)); //taille de la fenêtre
public void fixeListenerResetFlotte(ActionListener l) {
reset.addActionListener(l);
}
public void fixeListenerOuvrirOptPartie(ActionListener action) {
pret.addActionListener(action);
public void fixeListenerFermerCreation(ActionListener l) {
retour.addActionListener(l);
}
//public void fixeListenerOuvrirChoixPartie(ActionListener action) {
// pret.addActionListener(action);
//}
public void fixeListenerFermerCreation(ActionListener action) {
annuler.addActionListener(action);
public void fixeListenerAjouterBateau(MouseListener l) {
map.addMouseListener(l);
}
}
package info1.Vue;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import info1.application.appli;
import java.awt.*;
import java.awt.event.*;
public class Credits extends JDialog {
......@@ -41,7 +53,7 @@ public class Credits extends JDialog {
credits.setMargin(new Insets(5, 5, 5, 5));
credits.setBackground(appli.BACKGROUND_COLOR);
credits.setCaretPosition(0);
JScrollPane principal = new JScrollPane(credits);
panelPrincipal.setPreferredSize(new Dimension(WIDTH, HEIGHT));
......@@ -67,6 +79,4 @@ public class Credits extends JDialog {
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
}
public JTextPane getCredits()
{return credits;}
}
package info1.Vue;
import info1.ships.*;
import info1.ships.ICoord;
import info1.ships.INavyFleet;
import info1.ships.IShip;
public abstract interface IMap{
......
package info1.Vue;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.imageio.*;
import info1.ships.*;
import info1.ships.ICoord;
import info1.ships.INavyFleet;
import info1.ships.IShip;
public class Map extends JPanel implements IMap{
public class Map extends JPanel implements IMap {
/**
* représentation graphique de la grille de jeu, permets d'ajouter les images sur la grille de jeu
* représentation graphique de la grille de jeu, permets d'ajouter les images
* sur la grille de jeu
*/
private BufferedImage image;
public Map() {
super();
try {
......@@ -26,135 +29,122 @@ public class Map extends JPanel implements IMap{
}
}
/**
* ajoute l'image d'un plouf (tir manqué) aux coordonnées indiquées de la grille
*
* @param Coordonnee
*/
public void ajouterPlouf(ICoord Coordonnee) {
BufferedImage Plouf=null;
BufferedImage Plouf = null;
try {
int rand = (int) (Math.random()*5+1);
int rand = (int) (Math.random() * 5 + 1);
switch (rand) {
case 1:
Plouf = ImageIO.read(new File("img/interface/plouf/plouf1.png"));
break;
case 2 :
case 2:
Plouf = ImageIO.read(new File("img/interface/plouf/plouf2.png"));
break;
case 3 :
case 3:
Plouf = ImageIO.read(new File("img/interface/plouf/plouf3.png"));
break;
case 4 :
case 4:
Plouf = ImageIO.read(new File("img/interface/plouf/plouf4.png"));
break;
case 5 :
case 5:
Plouf = ImageIO.read(new File("img/interface/plouf/plouf5.png"));
break;
default:
Plouf = ImageIO.read(new File("img/interface/plouf/tiplouf.png"));
break;
}
}
catch(IOException e)
{
} catch (IOException e) {
System.out.println(e);
}
Graphics g=image.getGraphics();
g.drawImage(Plouf, Coordonnee.getX()*10-10, Coordonnee.getY()*10-10, null);
Graphics g = image.getGraphics();
g.drawImage(Plouf, Coordonnee.getX() * 10 - 10, Coordonnee.getY() * 10 - 10, null);
this.update(this.getGraphics());
}
/**
* ajoute l'image d'un boom (tir touché) aux coordonnées indiquées de la grille
*
* @param Coordonnee
*/
public void ajouterBoom(ICoord Coordonnee) {
BufferedImage Boom=null;
BufferedImage Boom = null;
try {
Boom = ImageIO.read(new File("img/interface/boom/Boom1.png"));
}
catch(IOException e)
{
} catch (IOException e) {
System.out.println(e);
}
Graphics g=image.getGraphics();
g.drawImage(Boom, Coordonnee.getX()*10-10, Coordonnee.getY()*10-10, null);
Graphics g = image.getGraphics();
g.drawImage(Boom, Coordonnee.getX() * 10 - 10, Coordonnee.getY() * 10 - 10, null);
this.update(this.getGraphics());
}
/**
* ajoute un bateau de la bonne catégorie et tourné dans le sens: avant vers la droite si il est a l'horizontal, avant vers le haut si il est a la vertical a ses coordonnées sur la grille
* ajoute un bateau de la bonne catégorie et tourné dans le sens: avant vers la
* droite si il est a l'horizontal, avant vers le haut si il est a la vertical a
* ses coordonnées sur la grille
*
* @param bateau
*/
public void ajouterBateau(IShip bateau) {
String direction;
direction = bateau.getFront().getX()==bateau.getBack().getX() ? "vertical" : "horizontal";
direction = bateau.getFront().getX() == bateau.getBack().getX() ? "vertical" : "horizontal";
BufferedImage i = null;
switch (bateau.getCategory().getSize()){
case 1:
try {
i = ImageIO.read(new File("img/interface/bateaux/"+direction+"/Submarine.png"));
}
catch(IOException e)
{
switch (bateau.getCategory().getSize()) {
case 1:
try {
i = ImageIO.read(new File("img/interface/bateaux/" + direction + "/Submarine.png"));
} catch (IOException e) {
e.printStackTrace();
}
break;
case 2:
try {
i = ImageIO.read(new File("img/interface/bateaux/"+direction+"/Destroyer.png"));
}
catch(IOException e)
{
break;
case 2:
try {
i = ImageIO.read(new File("img/interface/bateaux/" + direction + "/Destroyer.png"));
} catch (IOException e) {
e.printStackTrace();
}
break;
case 3:
try {
i = ImageIO.read(new File("img/interface/bateaux/"+direction+"/Cruiser.png"));
}
catch(IOException e)
{
break;
case 3:
try {
i = ImageIO.read(new File("img/interface/bateaux/" + direction + "/Cruiser.png"));
} catch (IOException e) {
e.printStackTrace();
}
break;
case 4:
try {
i = ImageIO.read(new File("img/interface/bateaux/"+direction+"/Battleship.png"));
}
catch(IOException e)
{
break;
case 4:
try {
i = ImageIO.read(new File("img/interface/bateaux/" + direction + "/Battleship.png"));
} catch (IOException e) {
e.printStackTrace();
}
break;
case 5:
try {
i = ImageIO.read(new File("img/interface/bateaux/"+direction+"/AirCraftCarrier.png"));
}
catch(IOException e)
{
break;
case 5: