diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java index a9ffe7b..64e0061 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java @@ -1,10 +1,13 @@ package fr.iutfbleau.projetAgile.View; +import javax.rmi.CORBA.Util; import javax.swing.*; -import fr.iutfbleau.projetAgile.Utils.Utils; import java.awt.*; +import java.util.Observable; +import java.util.Observer; +import fr.iutfbleau.projetAgile.Utils.Utils; -public class Grille extends JPanel { +public class Grille extends JPanel implements Observer{ private Pion grille[][]; @@ -14,17 +17,21 @@ public class Grille extends JPanel { } protected void init() { + this.setBackground(new Color(31,31,31)); + this.reset(); + } + + public void reset() { /* * On peut remplacer GridBagLayout par un GridLayout */ - this.setBackground(new Color(31,31,31)); GridBagLayout gbl = new GridBagLayout(); this.setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); this.grille = new Pion[Utils.COLUMN_COUNT][Utils.ROW_COUNT]; for (int x = 0; x < Utils.COLUMN_COUNT; x++) { for (int y = 0; y < Utils.ROW_COUNT; y++) { - Pion pion = new Pion(); + Pion pion = new Pion(-1); gbc.gridx = x; gbc.gridy = y; gbc.fill = GridBagConstraints.BOTH; @@ -50,4 +57,16 @@ public class Grille extends JPanel { this.setPreferredSize(new Dimension(preferredWidth, preferredHeight)); } + @Override + public void update(Observable o, Object arg) { + int[] param = (int[]) arg; + this.addPlayerPawn(param[0], param[1], param[2]); + } + + + protected void addPlayerPawn(int column, int row, int player) { + Color c = player == Utils.PLAYER_ONE ? Utils.PLAYER_ONE_COLOR : Utils.PLAYER_TWO_COLOR; + this.grille[column][row] = new Pion(player); + } + } \ No newline at end of file diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java index 24ec7ab..283028d 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java @@ -6,6 +6,8 @@ import fr.iutfbleau.projetAgile.Utils.Utils; public class Pion extends JComponent{ + private int player; + public static Dimension getPionMinimumSize() { return new Dimension(80,80); } @@ -18,17 +20,29 @@ public class Pion extends JComponent{ return new Dimension(120,120); } - public Pion() { + public Pion(int player) { this.setMinimumSize(new Dimension(80,80)); this.setMaximumSize(new Dimension(150,150)); this.setPreferredSize(new Dimension(120,120)); - this.setBackground(Utils.EMPTY_COLOR); + this.player = player; } @Override protected void paintComponent(Graphics g) { Graphics g2 = g.create(); - g2.setColor(this.getBackground()); + Color c; + switch(this.player) { + case Utils.PLAYER_ONE : + c = Utils.PLAYER_ONE_COLOR; + break; + case Utils.PLAYER_TWO : + c = Utils.PLAYER_TWO_COLOR; + break; + default : + c = Utils.EMPTY_COLOR; + break; + } + g2.setColor(c); g2.fillOval(Utils.PAWN_MARGIN, Utils.PAWN_MARGIN, this.getWidth() - 2 * Utils.PAWN_MARGIN, this.getHeight() - 2 * Utils.PAWN_MARGIN); } } \ No newline at end of file diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java new file mode 100644 index 0000000..22c9d55 --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java @@ -0,0 +1,81 @@ +package fr.iutfbleau.projetAgile.View; + +import java.util.Observable; +import javax.swing.*; +import java.awt.*; + +public class Puissance4Panel extends JPanel{ + + private JButton reset; + private JButton menu; + private JLabel label; + private Grille grille; + + public Puissance4Panel() { + super(); + this.reset = new JButton("recommencer"); + this.menu = new JButton("Accueil"); + this.label = new JLabel("??"); + this.grille = new Grille(); + this.init(); + this.grille.requestFocusInWindow(); + } + + public void init() { + GridBagLayout gbl = new GridBagLayout(); + this.setLayout(gbl); + GridBagConstraints gbc = new GridBagConstraints(); + + gbc.gridx = 0; + gbc.gridy = 0; + gbc.fill = GridBagConstraints.NONE; + gbc.anchor = GridBagConstraints.CENTER; + gbc.gridwidth = 1; + gbc.gridheight = 1; + gbc.weightx = 0; + gbc.weighty = 0; + gbc.insets = new Insets(0, 0, 0, 0); + + this.add(this.menu, gbc); + + gbc.gridx = 1; + gbc.gridy = 0; + gbc.fill = GridBagConstraints.NONE; + gbc.anchor = GridBagConstraints.CENTER; + gbc.gridwidth = 1; + gbc.gridheight = 1; + gbc.weightx = 0; + gbc.weighty = 0; + gbc.insets = new Insets(0, 0, 0, 0); + + this.add(new JLabel("Puissance 4"), gbc); + + gbc.gridx = 1; + gbc.gridy = 1; + gbc.fill = GridBagConstraints.NONE; + gbc.anchor = GridBagConstraints.CENTER; + gbc.gridwidth = 1; + gbc.gridheight = 1; + gbc.weightx = 0; + gbc.weighty = 0; + gbc.insets = new Insets(0, 0, 0, 0); + + this.add(this.grille, gbc); + + JPanel panneauBas = new JPanel(new FlowLayout()); + panneauBas.add(this.label); + panneauBas.add(this.reset); + gbc.gridx = 1; + gbc.gridy = 2; + gbc.fill = GridBagConstraints.NONE; + gbc.anchor = GridBagConstraints.CENTER; + gbc.gridwidth = 1; + gbc.gridheight = 1; + gbc.weightx = 0; + gbc.weighty = 0; + gbc.insets = new Insets(0, 0, 0, 0); + + this.add(panneauBas, gbc); + } + +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java index 4bd45a9..32ab727 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java @@ -6,25 +6,9 @@ import java.awt.*; public class TestGrille extends JFrame{ public TestGrille() { super("Puissance 4"); - this.setLayout(new GridBagLayout()); - Grille g = new Grille(); this.setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - GridBagConstraints gbc = new GridBagConstraints(); - gbc.gridx = 1; - gbc.gridy = 1; - gbc.fill = GridBagConstraints.NONE; - gbc.anchor = GridBagConstraints.CENTER; - gbc.gridwidth = 1; - gbc.gridheight = 1; - gbc.weightx = 0; - gbc.weighty = 0; - gbc.insets = new Insets(0, 0, 0, 0); - this.add(g, gbc); - this.setMinimumSize(g.getMinimumSize()); - this.pack(); - this.setVisible(true); - g.requestFocusInWindow(); + this.add(new Puissance4Panel()); } public static void main(String[] args) {