bouton back to menu
This commit is contained in:
@@ -2,113 +2,76 @@ package linea;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.*;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class ZoneDessin extends JPanel { // hérite d'une classe du frameWork standard
|
||||
public class ZoneDessin extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected ArrayList<ObjetGraphique> listeObjets = new ArrayList<ObjetGraphique>();
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// PROPRIETES
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// un booleen qui permet d'arreter l'animation (suspendre)
|
||||
protected boolean estArrete = false;
|
||||
|
||||
// Etats du jeu
|
||||
protected boolean estArrete = false;
|
||||
protected boolean isGameOver = false;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// METHODES
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Constructeur de la classe
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// LE BOUTON DOIT ÊTRE PUBLIC POUR ÊTRE ACCESSIBLE DEPUIS JEU.JAVA
|
||||
public JButton btnRetour;
|
||||
|
||||
public ZoneDessin(){
|
||||
// on prépare la zone d'affichage
|
||||
setLayout(new BorderLayout());
|
||||
// 1. IMPORTANT : Layout null pour positionner le bouton pixel par pixel
|
||||
setLayout(null);
|
||||
setPreferredSize(new Dimension(800, 600));
|
||||
setBackground(new Color(100,100,100));
|
||||
|
||||
// 2. Création et configuration du bouton
|
||||
btnRetour = new JButton("Retour au Menu");
|
||||
// x, y, largeur, hauteur (centré horizontalement pour une fenêtre de 800px)
|
||||
btnRetour.setBounds(300, 350, 200, 50);
|
||||
btnRetour.setFont(new Font("Arial", Font.BOLD, 16));
|
||||
btnRetour.setFocusable(false); // Important pour ne pas gêner le clavier
|
||||
btnRetour.setVisible(false); // Caché au début
|
||||
|
||||
this.add(btnRetour);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Ajout d'un objet graphique à la zonde de dessin
|
||||
//-------------------------------------------------------------------------
|
||||
public void ajouterObjet(ObjetGraphique unObjet) {
|
||||
this.listeObjets.add(unObjet);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Mettre en pause et redémarrer
|
||||
//-------------------------------------------------------------------------
|
||||
public void arreter(){
|
||||
estArrete = true;
|
||||
}
|
||||
|
||||
public void demarrer(){
|
||||
estArrete = false;
|
||||
}
|
||||
|
||||
// ... (méthodes ajouterObjet, viderObjets, arreter, demarrer restent pareilles) ...
|
||||
public void ajouterObjet(ObjetGraphique unObjet) { this.listeObjets.add(unObjet); }
|
||||
public void viderObjets() { this.listeObjets.clear(); }
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Boucle d'animation :
|
||||
// - appelée par exemple 25 fois par seconde
|
||||
// - à chaque appel :
|
||||
// - on anime (déplace) chaque objet
|
||||
// - on redessine tout : appel à paintComponent, déclenché par repaint
|
||||
//-------------------------------------------------------------------------
|
||||
public void traiterBoucleAnimation(){
|
||||
if (estArrete==true) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. on déplace chaque objet graphique
|
||||
// A FAIRE : décommenter lorsque cela devienda exécutable, et compléter
|
||||
if (estArrete || isGameOver) return;
|
||||
|
||||
for (ObjetGraphique obj : listeObjets){
|
||||
obj.Animer();
|
||||
}
|
||||
// 2. on demande à redessiner, ce qui déclenchera automatiquement
|
||||
// l'appel à la méthode paintComponent, qui est ci-dessous
|
||||
repaint();
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Dessin, déclenché par le repaint() de la méthode ci-dessus
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
// On demande à la super-classe (JPanel) de se redessiner
|
||||
super.paintComponent(g);
|
||||
// Puis on ajoute ce qui est spécifique à la classe
|
||||
// on indique qu'il faut de l'antialiasing
|
||||
Graphics2D g2D = (Graphics2D) g;
|
||||
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
for (ObjetGraphique obj : listeObjets){
|
||||
obj.Afficher(g);
|
||||
}
|
||||
|
||||
// Affichage de l'écran Game Over
|
||||
if (isGameOver) {
|
||||
g2D.setColor(new Color(0, 0, 0, 150)); // Fond noir transparent
|
||||
g2D.setColor(new Color(0, 0, 0, 150)); // Fond sombre
|
||||
g2D.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
g2D.setColor(Color.WHITE);
|
||||
g2D.setFont(new Font("Arial", Font.BOLD, 50));
|
||||
String msg = "GAME OVER";
|
||||
int largeurTexte = g2D.getFontMetrics().stringWidth(msg);
|
||||
g2D.drawString(msg, (getWidth() - largeurTexte) / 2, getHeight() / 2);
|
||||
int largeur = g2D.getFontMetrics().stringWidth(msg);
|
||||
g2D.drawString(msg, (getWidth() - largeur) / 2, getHeight() / 2 - 50);
|
||||
}
|
||||
}
|
||||
|
||||
public void setGameOver(boolean state) {
|
||||
this.isGameOver = state;
|
||||
// 3. C'est ici qu'on affiche ou cache le bouton
|
||||
btnRetour.setVisible(state);
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user