Files
Tetris/app/src/main/java/org/Views/VueBandeauControle.java
2025-05-20 15:13:37 +02:00

172 lines
5.7 KiB
Java

package org.Views;
import org.Models.GridLayoutCarre;
import org.Models.Jeu;
import org.Models.PieceCourante;
import javax.swing.*;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;
@SuppressWarnings("deprecation")
public class VueBandeauControle extends JPanel implements Observer {
private JLabel scoreLabel;
private JPanel nextPiecePanel;
private JButton pauseButton;
private JButton aideButton;
private JPanel[][] caseNextPiece = new JPanel[4][4];
private JLabel nbLigneLabel;
private JButton quitterButton;
private Jeu jeu;
private JButton relancerButton;
public VueBandeauControle(Jeu jeu) {
this.jeu = jeu;
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(Color.gray);
// setPreferredSize();
// SCORE
scoreLabel = new JLabel("SCORE : " + jeu.getGrille().getScore());
scoreLabel.setForeground(Color.white);
scoreLabel.setFont(new Font("Arial", Font.PLAIN, 16));
scoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// NB LIGNE
nbLigneLabel = new JLabel("LIGNES : "+ jeu.getGrille().getNbLignesSupprimees());
nbLigneLabel.setForeground(Color.white);
nbLigneLabel.setFont(new Font("Arial", Font.PLAIN, 16));
nbLigneLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// NEXT PIECE
nextPiecePanel = new JPanel();
nextPiecePanel.setBackground(Color.LIGHT_GRAY);
nextPiecePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
initierNextPiecePanel();
// PAUSE BUTTON & QUIT BUTTON
pauseButton = new JButton("PAUSE");
quitterButton = new JButton("QUITTER");
Dimension buttonSize = new Dimension(85, 30);
Insets margeBoutton = new Insets(2, 2, 2, 2);
pauseButton.setPreferredSize(buttonSize);
pauseButton.setMargin(margeBoutton);
quitterButton.setPreferredSize(buttonSize);
quitterButton.setMargin(margeBoutton);
JPanel hautBoutonsPanel = new JPanel();
//boutonsPanel.setLayout(new BoxLayout(boutonsPanel, BoxLayout.X_AXIS));
hautBoutonsPanel.setOpaque(false);
hautBoutonsPanel.add(pauseButton);
hautBoutonsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
hautBoutonsPanel.add(quitterButton);
pauseButton.setFocusable(false);
quitterButton.setFocusable(false);
//Relancer button
relancerButton = new JButton("RESTART");
relancerButton.setPreferredSize(buttonSize);
relancerButton.setMargin(margeBoutton);
relancerButton.setFocusable(false);
// AIDE BUTTON
aideButton = new JButton("?");
aideButton.setToolTipText("AFFICHER L'AIDE");
aideButton.setPreferredSize(new Dimension(45, 20));
aideButton.setFocusable(false);
JPanel footerPanel = new JPanel(new BorderLayout());
footerPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
footerPanel.setOpaque(false);
footerPanel.add(relancerButton, BorderLayout.WEST);
footerPanel.add(aideButton, BorderLayout.EAST);
add(Box.createVerticalStrut(20));
add(scoreLabel);
add(Box.createVerticalStrut(20));
add(nbLigneLabel);
add(Box.createVerticalStrut(20));
add(nextPiecePanel);
add(Box.createVerticalStrut(20));
add(hautBoutonsPanel);
add(Box.createVerticalGlue()); //force le JPanel à prendre toute la hauteur
add(footerPanel);
// setVisible(true);
jeu.addObserver(this);
}
public void setScore(int score) {
scoreLabel.setText("Score: " + score);
}
public JButton getPauseButton() {
return pauseButton;
}
public JButton getQuitterButton() {
return quitterButton;
}
public JPanel getNextPiecePanel() {
return nextPiecePanel;
}
private void initierNextPiecePanel() {
nextPiecePanel.removeAll();
nextPiecePanel.setLayout(new GridLayoutCarre(4, 4));
Dimension tailleCarre = new Dimension(120, 120);
nextPiecePanel.setPreferredSize(tailleCarre);
nextPiecePanel.setMaximumSize(tailleCarre);
nextPiecePanel.setMinimumSize(tailleCarre);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
JPanel caseG = new JPanel();
caseG.setBorder(BorderFactory.createLineBorder(Color.BLACK));
caseG.setBackground(Color.WHITE);
caseNextPiece[i][j] = caseG;
nextPiecePanel.add(caseG);
}
}
nextPiecePanel.revalidate();
nextPiecePanel.repaint();
}
public void afficherPieceSuivante(PieceCourante piece) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
caseNextPiece[i][j].setBackground(Color.WHITE);
}
}
boolean[][] motif = piece.getMotif();
for (int i = 0; i < motif.length; i++) {
for (int j = 0; j < motif[i].length; j++) {
if (motif[i][j]) {
caseNextPiece[i][j].setBackground(Color.RED);
}
}
}
nextPiecePanel.revalidate();
nextPiecePanel.repaint();
}
public JButton getAideButton() {
return aideButton;
}
public JButton getRelancerButton() {
return relancerButton;
}
@Override
public void update(Observable o, Object arg) {
if (o instanceof Jeu) {
afficherPieceSuivante(jeu.getPieceSuivante());
setScore(jeu.getGrille().getScore());
nbLigneLabel.setText("LIGNES : " + jeu.getGrille().getNbLignesSupprimees());
scoreLabel.setText("SCORE : " + jeu.getGrille().getScore());
}
}
}