Test dialog pour la couleur des joueurs*

This commit is contained in:
Justine Yannis 2022-10-27 11:58:39 +02:00
parent 1ada884296
commit ae56151718
2 changed files with 142 additions and 2 deletions

View File

@ -9,11 +9,11 @@ public class Constants {
/**
* La couleur du joueur 1
*/
public final static Color PLAYER_ONE_COLOR = new Color(252,21,21);
public static Color PLAYER_ONE_COLOR = new Color(252,21,21);
/**
* La couleur du joueur 1
*/
public final static Color PLAYER_TWO_COLOR = new Color(241,205,15);
public static Color PLAYER_TWO_COLOR = new Color(241,205,15);
/**
* La couleur d'un pion "vide"
*/

View File

@ -0,0 +1,140 @@
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);
}
}