From a6148c3ff977495dba580786c94941f37fd5fdac Mon Sep 17 00:00:00 2001 From: Justine Yannis Date: Thu, 13 Oct 2022 11:01:22 +0200 Subject: [PATCH] =?UTF-8?q?Tests=20modele=20et=20vue=20m=C3=A9lang=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- premierTests/Grille.java | 71 +++++++++++++++++++++++++++ premierTests/GrilleMouseListener.java | 17 +++++++ premierTests/Pion.java | 23 +++++++++ premierTests/Puissance4_View.java | 33 +++++++++++++ premierTests/Utils.java | 11 +++++ 5 files changed, 155 insertions(+) create mode 100644 premierTests/Grille.java create mode 100644 premierTests/GrilleMouseListener.java create mode 100644 premierTests/Pion.java create mode 100644 premierTests/Puissance4_View.java create mode 100644 premierTests/Utils.java diff --git a/premierTests/Grille.java b/premierTests/Grille.java new file mode 100644 index 0000000..45b43ab --- /dev/null +++ b/premierTests/Grille.java @@ -0,0 +1,71 @@ +import javax.swing.*; +import java.awt.*; + +public class Grille extends JPanel { + + private Pion grille[][]; + private int playerTurn = Utils.PLAYER_ONE; + + public Grille() { + super(); + /* + * On peut remplacer GridBagLayout par un GridLayout + */ + GridBagLayout gbl = new GridBagLayout(); + this.setLayout(gbl); + GridBagConstraints gbc = new GridBagConstraints(); + 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(); + gbc.gridx = x; + gbc.gridy = y; + gbc.fill = GridBagConstraints.BOTH; + 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(pion, gbc); + + grille[x][y] = pion; + } + } + + int minimumWidth = Utils.COLUMN_COUNT * grille[0][0].getMinimumSize().width; + int minimumHeight = Utils.ROW_COUNT * grille[0][0].getMinimumSize().height; + int preferredWidth = Utils.COLUMN_COUNT * grille[0][0].getPreferredSize().width; + int preferredHeight = Utils.ROW_COUNT * grille[0][0].getPreferredSize().height; + + this.setMinimumSize(new Dimension(minimumWidth, minimumHeight)); + this.setPreferredSize(new Dimension(preferredWidth, preferredHeight)); + } + + public void drawPlayerPlay(int column, int player) { + for (int row = Utils.ROW_COUNT - 1; row >= 0; row--) { + Pion p = grille[column][row]; + if (p.getBackground() != Utils.PLAYER_ONE_COLOR && p.getBackground() != Utils.PLAYER_TWO_COLOR) { + p.setBackground(player == Utils.PLAYER_ONE ? Utils.PLAYER_ONE_COLOR : Utils.PLAYER_TWO_COLOR); + this.switchPlayer(); + break; + } + } + } + + private void switchPlayer() { + this.playerTurn = (playerTurn + 1) % 2; + } + + public int getPlayerTurn() { + return playerTurn; + } + + @Override + protected void paintComponent(Graphics g) { + Graphics g2 = g.create(); + g2.setColor(Color.BLUE); + g2.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 3 * Utils.PAWN_MARGIN, 3 * Utils.PAWN_MARGIN); + } +} \ No newline at end of file diff --git a/premierTests/GrilleMouseListener.java b/premierTests/GrilleMouseListener.java new file mode 100644 index 0000000..8b993f2 --- /dev/null +++ b/premierTests/GrilleMouseListener.java @@ -0,0 +1,17 @@ +import java.awt.event.MouseEvent; +import javax.swing.event.MouseInputAdapter; + +public class GrilleMouseListener extends MouseInputAdapter{ + + private Grille grille; + + public GrilleMouseListener(Grille g) { + this.grille = g; + } + + @Override + public void mouseClicked(MouseEvent e) { + int column = (e.getX() * Utils.COLUMN_COUNT / grille.getWidth()); + grille.drawPlayerPlay(column, grille.getPlayerTurn()); + } +} diff --git a/premierTests/Pion.java b/premierTests/Pion.java new file mode 100644 index 0000000..ce76973 --- /dev/null +++ b/premierTests/Pion.java @@ -0,0 +1,23 @@ +import java.awt.*; +import javax.swing.*; + +public class Pion extends JComponent{ + + public Pion() { + this.setMinimumSize(new Dimension(80,80)); + this.setMaximumSize(new Dimension(150,150)); + this.setPreferredSize(new Dimension(120,120)); + this.setBackground(Color.WHITE); + } + + @Override + protected void paintComponent(Graphics g) { + Graphics g2 = g.create(); + //g2.setColor(Color.BLUE); + //g2.fillRect(0, 0, this.getWidth(), this.getHeight()); + g2.setColor(Color.GRAY); + g2.fillOval((int) (Utils.PAWN_MARGIN * 1.2),(int) (Utils.PAWN_MARGIN * 1.1) , this.getWidth() - 2 * Utils.PAWN_MARGIN, this.getHeight() - 2 * Utils.PAWN_MARGIN + 2); + g2.setColor(this.getBackground()); + 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/premierTests/Puissance4_View.java b/premierTests/Puissance4_View.java new file mode 100644 index 0000000..0b6cbd9 --- /dev/null +++ b/premierTests/Puissance4_View.java @@ -0,0 +1,33 @@ +import javax.swing.*; + +import java.awt.*; + +public class Puissance4_View extends JFrame { + public Puissance4_View() { + super("Puissance 4"); + this.setLayout(new GridBagLayout()); + Grille g = new Grille(); + g.addMouseListener(new GrilleMouseListener(g)); + 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(); + } + + public static void main(String[] args) { + Puissance4_View v = new Puissance4_View(); + } +} diff --git a/premierTests/Utils.java b/premierTests/Utils.java new file mode 100644 index 0000000..1ef1449 --- /dev/null +++ b/premierTests/Utils.java @@ -0,0 +1,11 @@ +import java.awt.Color; + +public class Utils { + public final static Color PLAYER_ONE_COLOR = Color.RED; + public final static Color PLAYER_TWO_COLOR = Color.YELLOW; + public final static int PLAYER_ONE = 0; + public final static int PLAYER_TWO = 1; + public static int PAWN_MARGIN = 10; + public static int COLUMN_COUNT = 7; + public static int ROW_COUNT = 6; +}