FIprojetAgile2022Groupe3B/premierTests/Grille.java

71 lines
2.4 KiB
Java

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);
}
}