Choix des couleurs, pas de test et de commentaires

This commit is contained in:
Justine Yannis 2022-10-30 13:38:15 +01:00
parent ae56151718
commit 6e8af79dd7
7 changed files with 268 additions and 170 deletions

View File

@ -56,6 +56,12 @@ build/$(PACKAGE_PATH)/Puissance4/Controller/ResetGame.class : src/$(PACKAGE_PATH
build/$(PACKAGE_PATH)/Puissance4/Controller/GrilleMouseListener.class : src/$(PACKAGE_PATH)/Puissance4/Controller/GrilleMouseListener.java
@javac $(@JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Puissance4/Controller/ColorChooserListener.class : src/$(PACKAGE_PATH)/Puissance4/Controller/ColorChooserListener.java
@javac $(@JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Puissance4/Controller/ColorActionListener.class : src/$(PACKAGE_PATH)/Puissance4/Controller/ColorActionListener.java
@javac $(@JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Puissance4/Controller/ModelEventListener.class : src/$(PACKAGE_PATH)/Puissance4/Controller/ModelEventListener.java \
build/$(PACKAGE_PATH)/Puissance4/Event/GridChangedListener.class \
build/$(PACKAGE_PATH)/Puissance4/View/Grille.class \
@ -110,6 +116,11 @@ build/$(PACKAGE_PATH)/Puissance4/View/Puissance4Panel.class : src/$(PACKAGE_PAT
build/$(PACKAGE_PATH)/Puissance4/Utils/Constants.class
@javac $(@JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Puissance4/View/ColorChooserDialog.class : src/$(PACKAGE_PATH)/Puissance4/View/ColorChooserDialog.java \
build/$(PACKAGE_PATH)/Puissance4/Controller/ColorActionListener.class \
build/$(PACKAGE_PATH)/Puissance4/Controller/ColorChooserListener.class
@javac $(@JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Menu/View/BoutonsMenu.class : src/$(PACKAGE_PATH)/Menu/View/BoutonsMenu.java
@javac $(@JAVAC_OPT) $<
@ -129,6 +140,7 @@ build/$(PACKAGE_PATH)/Main.class : src/$(PACKAGE_PATH)/Main.java \
build/$(PACKAGE_PATH)/Puissance4/LancementPartie.class : src/$(PACKAGE_PATH)/Puissance4/LancementPartie.java \
build/$(PACKAGE_PATH)/Puissance4/Controller/Puissance4Controller.class \
build/$(PACKAGE_PATH)/Puissance4/View/ColorChooserDialog.class \
build/$(PACKAGE_PATH)/Puissance4/View/Grille.class \
build/$(PACKAGE_PATH)/Puissance4/Model/GrilleModel.class
@javac $(@JAVAC_OPT) $<

View File

@ -0,0 +1,22 @@
package fr.iutfbleau.projetAgile.Puissance4.Controller;
import fr.iutfbleau.projetAgile.Puissance4.View.ColorChooserDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ColorActionListener implements ActionListener{
private ColorChooserDialog dialog;
public ColorActionListener(ColorChooserDialog c) {
this.dialog = c;
}
@Override
public void actionPerformed(ActionEvent e) {
if(this.dialog.isColorValid() && this.dialog.isNameValid()) {
this.dialog.setConfirm(true);
this.dialog.setVisible(false);
}
}
}

View File

@ -0,0 +1,31 @@
package fr.iutfbleau.projetAgile.Puissance4.Controller;
import javax.swing.JComponent;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import fr.iutfbleau.projetAgile.Puissance4.Utils.Constants;
public class ColorChooserListener implements ChangeListener {
private JComponent composant;
private int player;
public ColorChooserListener(JComponent c, int player) {
this.composant = c;
this.player = player;
}
@Override
public void stateChanged(ChangeEvent e) {
if(e.getSource() instanceof ColorSelectionModel) {
ColorSelectionModel chooser = (ColorSelectionModel) e.getSource();
if(this.player == Constants.PLAYER_ONE)
Constants.PLAYER_ONE_COLOR = chooser.getSelectedColor();
else
Constants.PLAYER_TWO_COLOR = chooser.getSelectedColor();
composant.repaint();
}
}
}

View File

@ -2,13 +2,12 @@ package fr.iutfbleau.projetAgile.Puissance4;
import fr.iutfbleau.projetAgile.Puissance4.Controller.Puissance4Controller;
import fr.iutfbleau.projetAgile.Puissance4.View.Grille;
import fr.iutfbleau.projetAgile.Puissance4.View.ColorChooserDialog;
import fr.iutfbleau.projetAgile.Puissance4.Model.GrilleModel;
import java.awt.event.ActionListener;
import java.util.concurrent.CancellationException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* Classe qui initialise une partie de Puissance 4
@ -22,36 +21,14 @@ public class LancementPartie {
* @throws CancellationException Renvoie une exception si les utilisateurs annule le lancement de la partie
*/
public LancementPartie(JFrame fenetre) throws CancellationException{
boolean nameCorrect = false;
JTextField field1 = new JTextField("Joueur 1");
JTextField field2 = new JTextField("Joueur 2");
Object[] message = {
"Entrez le nom du joueur 1", field1,
"Entrez le nom du joueur 2:", field2,
};
String J1 = null;
String J2 = null;
while(!nameCorrect) {
int choix = JOptionPane.showConfirmDialog(fenetre, message, "Entrez les noms des joueurs", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (choix == JOptionPane.OK_OPTION) {
J1 = field1.getText();
J2 = field2.getText();
if(J1.length() > 10)
J1 = J1.substring(0, 9);
if(J2.length() > 10)
J2 = J2.substring(0, 9);
nameCorrect = true;
if(J1.isEmpty() || J2.isEmpty()) {
JOptionPane.showMessageDialog(fenetre,"Un des noms est vides","Erreur ", JOptionPane.ERROR_MESSAGE);
nameCorrect = false;
}
else if(J1.compareTo(J2) == 0) {
JOptionPane.showMessageDialog(fenetre,"Les deux noms sont identiques","Erreur ", JOptionPane.ERROR_MESSAGE);
nameCorrect = false;
}
} else {
throw new CancellationException("User cancelled");
}
ColorChooserDialog chooser = new ColorChooserDialog(fenetre, true);
if (chooser.getChoice()) {
J1 = chooser.getPlayerOneName();
J2 = chooser.getPlayerTwoName();
} else {
throw new CancellationException("User cancelled");
}
Grille g=new Grille();
GrilleModel m=new GrilleModel();

View File

@ -54,4 +54,9 @@ public class Constants {
private Constants() {
}
public static void resetColor() {
Constants.PLAYER_ONE_COLOR = new Color(252,21,21);
Constants.PLAYER_TWO_COLOR = new Color(241,205,15);
}
}

View File

@ -0,0 +1,191 @@
package fr.iutfbleau.projetAgile.Puissance4.View;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import java.awt.*;
import fr.iutfbleau.projetAgile.Puissance4.Controller.ColorActionListener;
import fr.iutfbleau.projetAgile.Puissance4.Controller.ColorChooserListener;
import fr.iutfbleau.projetAgile.Puissance4.Utils.Constants;
public class ColorChooserDialog extends JDialog{
private JTextField field1;
private JTextField field2;
private boolean confirm = false;
public ColorChooserDialog(JFrame fenetre, boolean b) {
super(fenetre, "Choisissez vos noms et couleurs",b);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
int length = 12;
JColorChooser playerOneChooser = new JColorChooser(Constants.PLAYER_ONE_COLOR);
JColorChooser playerTwoChooser = new JColorChooser(Constants.PLAYER_TWO_COLOR);
AbstractColorChooserPanel[] panelsP1 = playerOneChooser.getChooserPanels();
AbstractColorChooserPanel[] panelsP2 = playerTwoChooser.getChooserPanels();
UIManager.put("ColorChooser.swatchesSwatchSize", new Dimension(length, length));
UIManager.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(length, length));
for(int i = 1; i < panelsP1.length; i++) {
playerOneChooser.removeChooserPanel(panelsP1[i]);
playerTwoChooser.removeChooserPanel(panelsP2[i]);
}
JButton bouton = new JButton("Confirmer");
Pion pionP1 = new Pion(Constants.PLAYER_ONE);
Pion pionP2 = new Pion(Constants.PLAYER_TWO);
bouton.addActionListener(new ColorActionListener(this));
pionP1.setPreferredSize(new Dimension(50,50));
pionP2.setPreferredSize(new Dimension(50,50));
JPanel panelPionP1 = new JPanel();
JPanel panelPionP2 = new JPanel();
panelPionP1.add(pionP1);
panelPionP1.setBackground(Constants.BACKGROUND_COLOR);
panelPionP2.add(pionP2);
panelPionP2.setBackground(Constants.BACKGROUND_COLOR);
playerOneChooser.setPreviewPanel(panelPionP1);
playerTwoChooser.setPreviewPanel(panelPionP2);
playerOneChooser.getSelectionModel().addChangeListener(new ColorChooserListener(pionP1, Constants.PLAYER_ONE));
playerTwoChooser.getSelectionModel().addChangeListener(new ColorChooserListener(pionP2, Constants.PLAYER_TWO));
JPanel panelP1 = new JPanel(new GridBagLayout());
JPanel panelP2 = new JPanel(new GridBagLayout());
JLabel labelP1 = new JLabel("Nom et couleur du joueur 1");
JLabel labelP2 = new JLabel("Nom et couleur du joueur 2");
this.field1 = new JTextField("Joueur 1");
this.field2 = new JTextField("Joueur 2");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTH;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(0, 0, 0, 0);
panelP1.add(labelP1, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panelP1.add(field1, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 0.5;
panelP1.add(playerOneChooser, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 0;
panelP2.add(labelP2, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panelP2.add(field2, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 0.5;
panelP2.add(playerTwoChooser, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 5, 5);
this.add(panelP1,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(panelP2,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
this.add(bouton,gbc);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(fenetre);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
//Voir https://en.wikipedia.org/wiki/Color_difference
public boolean isColorValid() {
double distanceBetweenP1AndP2 = Math.sqrt(Math.pow(Constants.PLAYER_TWO_COLOR.getRed() - Constants.PLAYER_ONE_COLOR.getRed(), 2) + Math.pow(Constants.PLAYER_TWO_COLOR.getGreen() - Constants.PLAYER_ONE_COLOR.getGreen(), 2) + Math.pow(Constants.PLAYER_TWO_COLOR.getBlue() - Constants.PLAYER_ONE_COLOR.getBlue(), 2));
double distanceBetweenP1AndEmpty = Math.sqrt(Math.pow(Constants.EMPTY_COLOR.getRed() - Constants.PLAYER_ONE_COLOR.getRed(), 2) + Math.pow(Constants.EMPTY_COLOR.getGreen() - Constants.PLAYER_ONE_COLOR.getGreen(), 2) + Math.pow(Constants.EMPTY_COLOR.getBlue() - Constants.PLAYER_ONE_COLOR.getBlue(), 2));
double distanceBetweenP2AndEmpty = Math.sqrt(Math.pow(Constants.EMPTY_COLOR.getRed() - Constants.PLAYER_TWO_COLOR.getRed(), 2) + Math.pow(Constants.EMPTY_COLOR.getGreen() - Constants.PLAYER_TWO_COLOR.getGreen(), 2) + Math.pow(Constants.EMPTY_COLOR.getBlue() - Constants.PLAYER_TWO_COLOR.getBlue(), 2));
if(distanceBetweenP1AndP2 < 150f) {
JOptionPane.showMessageDialog(this,"Les deux couleurs sont trop proches","Erreur ", JOptionPane.ERROR_MESSAGE);
return false;
}
else if(distanceBetweenP1AndEmpty < 150f) {
JOptionPane.showMessageDialog(this,"La couleur du joueur 1 est trop sombre","Erreur ", JOptionPane.ERROR_MESSAGE);
return false;
}
else if(distanceBetweenP2AndEmpty < 150f) {
JOptionPane.showMessageDialog(this,"La couleur du joueur 2 est trop sombre","Erreur ", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
public boolean isNameValid() {
String J1 = null;
String J2 = null;
J1 = field1.getText();
J2 = field2.getText();
if(J1.length() > 10) {
J1 = J1.substring(0, 9);
this.field1.setText(J1);
}
if(J2.length() > 10) {
J2 = J2.substring(0, 9);
this.field2.setText(J2);
}
if(J1.isEmpty() || J2.isEmpty()) {
JOptionPane.showMessageDialog(this,"Un des noms est vides","Erreur ", JOptionPane.ERROR_MESSAGE);
return false;
}
else if(J1.compareTo(J2) == 0) {
JOptionPane.showMessageDialog(this,"Les deux noms sont identiques","Erreur ", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
public String getPlayerOneName() {
return this.field1.getText();
}
public String getPlayerTwoName() {
return this.field2.getText();
}
public void setConfirm(boolean b) {
this.confirm = b;
}
public boolean getChoice() {
this.setVisible(true);
return this.confirm;
}
}

View File

@ -1,140 +0,0 @@
package fr.iutfbleau.projetAgile.Puissance4.View;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import fr.iutfbleau.projetAgile.Puissance4.Utils.Constants;
public class TestColorChooserDialog extends JDialog{
private Pion pionP1;
private Pion pionP2;
public TestColorChooserDialog(JFrame fenetre, boolean b) {
super(fenetre, b);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
int length = 12;
UIManager.put("ColorChooser.swatchesSwatchSize", new Dimension(length, length));
UIManager.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(length, length));
JColorChooser playerOneChooser = new JColorChooser(Constants.PLAYER_ONE_COLOR);
JColorChooser playerTwoChooser = new JColorChooser(Constants.PLAYER_TWO_COLOR);
AbstractColorChooserPanel[] panelsP1 = playerOneChooser.getChooserPanels();
AbstractColorChooserPanel[] panelsP2 = playerTwoChooser.getChooserPanels();
for(int i = 1; i < panelsP1.length; i++) {
playerOneChooser.removeChooserPanel(panelsP1[i]);
playerTwoChooser.removeChooserPanel(panelsP2[i]);
}
this.pionP1 = new Pion(Constants.PLAYER_ONE);
this.pionP2 = new Pion(Constants.PLAYER_TWO);
this.pionP1.setPreferredSize(new Dimension(50,50));
this.pionP2.setPreferredSize(new Dimension(50,50));
JPanel panelPionP1 = new JPanel();
JPanel panelPionP2 = new JPanel();
panelPionP1.add(pionP1);
panelPionP1.setBackground(Constants.BACKGROUND_COLOR);
panelPionP2.add(pionP2);
panelPionP2.setBackground(Constants.BACKGROUND_COLOR);
playerOneChooser.setPreviewPanel(panelPionP1);
playerTwoChooser.setPreviewPanel(panelPionP2);
playerOneChooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Constants.PLAYER_ONE_COLOR = playerOneChooser.getColor();
pionP1.repaint();
}
});
playerTwoChooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Constants.PLAYER_TWO_COLOR = playerTwoChooser.getColor();
pionP2.repaint();
//System.out.println(" Distance entre joueur 1 et joueur 2 = " + Math.sqrt(Math.pow(Constants.PLAYER_TWO_COLOR.getRed() - Constants.PLAYER_ONE_COLOR.getRed(), 2) +
//Math.pow(Constants.PLAYER_TWO_COLOR.getGreen() - Constants.PLAYER_ONE_COLOR.getGreen(), 2) + Math.pow(Constants.PLAYER_TWO_COLOR.getBlue() - Constants.PLAYER_ONE_COLOR.getBlue(), 2)));
//System.out.println(" Distance entre joueur 1 et du noir = " + Math.sqrt(Math.pow(Constants.EMPTY_COLOR.getRed() - Constants.PLAYER_ONE_COLOR.getRed(), 2) +
//Math.pow(Constants.EMPTY_COLOR.getGreen() - Constants.PLAYER_ONE_COLOR.getGreen(), 2) + Math.pow(Constants.EMPTY_COLOR.getBlue() - Constants.PLAYER_ONE_COLOR.getBlue(), 2)));
}
});
JPanel panelP1 = new JPanel(new GridBagLayout());
JPanel panelP2 = new JPanel(new GridBagLayout());
JLabel labelP1 = new JLabel("Nom et couleur du joueur 1");
JLabel labelP2 = new JLabel("Nom et couleur du joueur 2");
JTextField field1 = new JTextField("Joueur 1");
JTextField field2 = new JTextField("Joueur 2");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTH;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(0, 0, 0, 0);
panelP1.add(labelP1, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panelP1.add(field1, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 0.5;
panelP1.add(playerOneChooser, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 0;
panelP2.add(labelP2, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panelP2.add(field2, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 0.5;
panelP2.add(playerTwoChooser, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 5, 5);
this.add(panelP1,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(panelP2,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JButton("Yes bébé"),gbc);
this.pack();
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestColorChooserDialog(null, false);
}
}