2026-02-03 13:50:27 +01:00
|
|
|
package linea;
|
|
|
|
|
|
2026-02-03 15:45:52 +01:00
|
|
|
import java.awt.*;
|
2026-02-03 13:50:27 +01:00
|
|
|
import java.util.ArrayList;
|
2026-02-10 16:02:29 +01:00
|
|
|
import javax.swing.*;
|
2026-02-03 13:50:27 +01:00
|
|
|
|
2026-02-10 16:02:29 +01:00
|
|
|
public class ZoneDessin extends JPanel {
|
2026-02-03 13:50:27 +01:00
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
protected ArrayList<ObjetGraphique> listeObjets = new ArrayList<ObjetGraphique>();
|
2026-02-03 15:45:52 +01:00
|
|
|
|
2026-02-10 16:02:29 +01:00
|
|
|
// Etats du jeu
|
|
|
|
|
protected boolean estArrete = false;
|
2026-02-03 15:45:52 +01:00
|
|
|
protected boolean isGameOver = false;
|
2026-02-10 16:02:29 +01:00
|
|
|
|
|
|
|
|
// LE BOUTON DOIT ÊTRE PUBLIC POUR ÊTRE ACCESSIBLE DEPUIS JEU.JAVA
|
|
|
|
|
public JButton btnRetour;
|
|
|
|
|
|
2026-02-03 13:50:27 +01:00
|
|
|
public ZoneDessin(){
|
2026-02-10 16:12:27 +01:00
|
|
|
// Layout null pour positionner le bouton pixel par pixel
|
2026-02-10 16:02:29 +01:00
|
|
|
setLayout(null);
|
2026-02-03 13:50:27 +01:00
|
|
|
setPreferredSize(new Dimension(800, 600));
|
2026-02-10 15:44:59 +01:00
|
|
|
setBackground(new Color(100,100,100));
|
2026-02-03 13:50:27 +01:00
|
|
|
|
2026-02-10 16:02:29 +01:00
|
|
|
// 2. Création et configuration du bouton
|
2026-02-10 16:03:23 +01:00
|
|
|
btnRetour = new JButton("Back to Menu");
|
2026-02-10 16:02:29 +01:00
|
|
|
// 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);
|
2026-02-03 13:50:27 +01:00
|
|
|
}
|
2026-02-10 16:02:29 +01:00
|
|
|
|
|
|
|
|
// ... (méthodes ajouterObjet, viderObjets, arreter, demarrer restent pareilles) ...
|
|
|
|
|
public void ajouterObjet(ObjetGraphique unObjet) { this.listeObjets.add(unObjet); }
|
|
|
|
|
public void viderObjets() { this.listeObjets.clear(); }
|
|
|
|
|
|
2026-02-03 13:50:27 +01:00
|
|
|
public void traiterBoucleAnimation(){
|
2026-02-10 16:02:29 +01:00
|
|
|
if (estArrete || isGameOver) return;
|
2026-02-03 13:50:27 +01:00
|
|
|
|
|
|
|
|
for (ObjetGraphique obj : listeObjets){
|
|
|
|
|
obj.Animer();
|
|
|
|
|
}
|
2026-02-10 16:02:29 +01:00
|
|
|
repaint();
|
2026-02-03 13:50:27 +01:00
|
|
|
}
|
2026-02-10 16:02:29 +01:00
|
|
|
|
2026-02-03 13:50:27 +01:00
|
|
|
public void paintComponent(Graphics g) {
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
|
Graphics2D g2D = (Graphics2D) g;
|
|
|
|
|
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
|
2026-02-10 16:02:29 +01:00
|
|
|
|
2026-02-03 13:50:27 +01:00
|
|
|
for (ObjetGraphique obj : listeObjets){
|
|
|
|
|
obj.Afficher(g);
|
|
|
|
|
}
|
2026-02-10 16:02:29 +01:00
|
|
|
|
|
|
|
|
// Affichage de l'écran Game Over
|
2026-02-03 15:45:52 +01:00
|
|
|
if (isGameOver) {
|
2026-02-10 16:02:29 +01:00
|
|
|
g2D.setColor(new Color(0, 0, 0, 150)); // Fond sombre
|
2026-02-03 15:45:52 +01:00
|
|
|
g2D.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
|
|
|
|
|
|
g2D.setColor(Color.WHITE);
|
|
|
|
|
g2D.setFont(new Font("Arial", Font.BOLD, 50));
|
|
|
|
|
String msg = "GAME OVER";
|
2026-02-10 16:02:29 +01:00
|
|
|
int largeur = g2D.getFontMetrics().stringWidth(msg);
|
|
|
|
|
g2D.drawString(msg, (getWidth() - largeur) / 2, getHeight() / 2 - 50);
|
2026-02-03 15:45:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setGameOver(boolean state) {
|
|
|
|
|
this.isGameOver = state;
|
2026-02-10 16:02:29 +01:00
|
|
|
// 3. C'est ici qu'on affiche ou cache le bouton
|
|
|
|
|
btnRetour.setVisible(state);
|
2026-02-03 15:45:52 +01:00
|
|
|
repaint();
|
2026-02-03 13:50:27 +01:00
|
|
|
}
|
2026-02-10 16:02:29 +01:00
|
|
|
}
|