feat: add of an MVC base architecture for Tetris

This commit is contained in:
Morph01
2025-05-15 16:17:50 +02:00
parent 275d761f24
commit eb7a013543
11 changed files with 553 additions and 40 deletions

View File

@@ -1,34 +1,58 @@
package org.Views;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import org.Models.Grille;
import org.Models.Jeu;
import org.Models.Ordonnanceur;
import org.Models.PieceCourante;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;
public class VueGrille extends JFrame {
@SuppressWarnings("deprecation")
public class VueGrille extends JFrame implements Observer, Runnable {
private JPanel grillePanel;
private int tailleJPanel = 1000;
private Grille grille;
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private double tailleJPanelX = screenSize.getHeight() / 4;
private double tailleJPanelY = screenSize.getHeight() / 2;
public VueGrille(Grille grille) {
private Grille grille;
private Jeu jeu;
private Ordonnanceur ordonnanceur;
private boolean afficherFenetreFinPartie = false;
public VueGrille(Grille grille, Jeu jeu) {
this.grille = grille;
this.jeu = jeu;
grillePanel = new JPanel(new GridLayout(grille.getNbLignes(), grille.getNbColonnes()));
setSize(tailleJPanel, tailleJPanel);
setSize((int) tailleJPanelX, (int) tailleJPanelY);
setContentPane(grillePanel);
initialiserVueGrille();
grille.addObserver(this);
jeu.addObserver(this);
this.ordonnanceur = new Ordonnanceur(this, 1000);
ordonnanceur.start();
}
private void initialiserVueGrille() {
Border border = BorderFactory.createLineBorder(Color.BLACK);
for (int i = 0; i < grille.getNbLignes(); i++) {
for (int j = 0; j < grille.getNbColonnes(); j++) {
JPanel caseG = new JPanel();
caseG.setBorder(border);
caseG.setBackground(Color.WHITE);
grillePanel.add(caseG);
}
@@ -38,4 +62,98 @@ public class VueGrille extends JFrame {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("TETRIS");
}
public synchronized void updateGrille() {
for (int i = 0; i < grille.getNbLignes(); i++) {
for (int j = 0; j < grille.getNbColonnes(); j++) {
JPanel caseG = (JPanel) grillePanel.getComponent(i * grille.getNbColonnes() + j);
if (grille.getGrille()[i][j]) {
caseG.setBackground(Color.BLUE);
} else {
caseG.setBackground(Color.WHITE);
}
}
}
// dessiner la pièce courante
if (jeu != null) {
PieceCourante piece = this.grille.getPieceCourante();
int posX = this.grille.getPieceCouranteX();
int posY = this.grille.getPieceCouranteY();
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]) {
int grilleX = posX + j;
int grilleY = posY + i;
if (grilleY >= 0 && grilleY < grille.getNbLignes() &&
grilleX >= 0 && grilleX < grille.getNbColonnes()) {
JPanel caseG = (JPanel) grillePanel
.getComponent(grilleY * grille.getNbColonnes() + grilleX);
caseG.setBackground(Color.RED);
}
}
}
}
}
repaint();
}
@SuppressWarnings("unused")
private void afficherFinPartie() {
JDialog gameOverDialog = new JDialog(this, "Partie terminée", true);
gameOverDialog.setLayout(new BorderLayout());
gameOverDialog.setUndecorated(true);
gameOverDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
JLabel gameOverLabel = new JLabel("GAME OVER", JLabel.CENTER);
gameOverLabel.setFont(new Font("Arial", Font.BOLD, 50));
gameOverLabel.setForeground(Color.RED);
gameOverLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
gameOverDialog.add(gameOverLabel, BorderLayout.CENTER);
JButton quitButton = new JButton("Quitter");
quitButton.addActionListener(e -> System.exit(0));
JPanel buttonPanel = new JPanel();
buttonPanel.add(quitButton);
gameOverDialog.add(buttonPanel, BorderLayout.SOUTH);
gameOverDialog.pack();
gameOverDialog.setLocationRelativeTo(this);
gameOverDialog.setVisible(true);
}
/**
* * Met à jour la vue de la grille lorsque l'état de la grille change.
*/
@Override
public void update(Observable o, Object arg) {
if (o instanceof Grille) {
updateGrille();
}
if (o instanceof Jeu && !afficherFenetreFinPartie) {
if (!this.jeu.jeuEnCours) {
afficherFenetreFinPartie = true;
afficherFinPartie();
}
}
}
/**
* * Met à jour la vue de la grille périodiquement selon l'ordonnaceur.
*/
@Override
public void run() {
updateGrille();
}
}