package org.Views; import javax.swing.JPanel; 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.GridLayout; import java.awt.Toolkit; import java.util.Observable; import java.util.Observer; @SuppressWarnings("deprecation") public class VueGrille extends JPanel implements Observer, Runnable { private JPanel grillePanel; private Grille grille; private Jeu jeu; private Ordonnanceur ordonnanceur; private boolean afficherFenetreFinPartie = false; private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); private double tailleJFrameX = screenSize.getHeight() / 2; private double tailleJFrameY = screenSize.getHeight(); public VueGrille(Grille grille, Jeu jeu) { this.grille = grille; this.jeu = jeu; setLayout(new BorderLayout()); grillePanel = new JPanel(new GridLayout(grille.getNbLignes(), grille.getNbColonnes())); setSize((int) tailleJFrameX, (int) tailleJFrameY); System.err.println("taille " + tailleJFrameX + " " + tailleJFrameY); add(this.grillePanel, BorderLayout.CENTER); initialiserVueGrille(); grille.addObserver(this); jeu.addObserver(this); this.ordonnanceur = new Ordonnanceur(this, 1000); ordonnanceur.start(); } private void initialiserVueGrille() { for (int i = 0; i < grille.getNbLignes(); i++) { for (int j = 0; j < grille.getNbColonnes(); j++) { JPanel caseG = new JPanel(); caseG.setBackground(Color.WHITE); grillePanel.add(caseG); } } } 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(); } private void afficherFinPartie() { System.err.println("FIN PARTIE"); // TODO : gerer affichage ? } /** * * 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(); } }