Compare commits

...

2 Commits

Author SHA1 Message Date
aef5c7f70c Merge branch 'Hugo' 2025-10-08 14:12:42 +02:00
dcac91d944 Affichage du pendu 2025-10-08 14:10:40 +02:00
2 changed files with 144 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
import javax.swing.*;
import java.awt.*;
class Affiche extends JComponent {
private int step = 0;
private boolean youWin = false;
public void setStep(int step) {
this.step = step;
repaint();
}
public void setYouWin(boolean value) {
this.youWin = value;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
Graphics2D g2 = (Graphics2D) g.create();
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.BLACK);
drawGallows(g2);
drawHangman(g2);
if (step >= 7) drawSkull(g2);
if (youWin) drawYouWinMessage(g2);
g2.dispose();
}
private void drawGallows(Graphics2D g2) {
// Base and vertical post
g2.drawLine(50, 350, 200, 350);
g2.drawLine(100, 350, 100, 50);
// Horizontal beam and rope
g2.drawLine(100, 50, 250, 50);
g2.drawLine(250, 50, 250, 100);
// Diagonal support
g2.drawLine(100, 100, 180, 50);
}
private void drawHangman(Graphics2D g2) {
int headX = 225, headY = 100, headDiam = 50;
int bodyX = headX + headDiam / 2, bodyYStart = headY + headDiam, bodyYEnd = bodyYStart + 100;
int armLength = 60;
int legLength = 70;
if (step >= 1) g2.drawOval(headX, headY, headDiam, headDiam); // head
if (step >= 2) g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd); // body
if (step >= 3) g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20); // left arm
if (step >= 4) g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20); // right arm
if (step >= 5) g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength); // left leg
if (step >= 6) g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength); // right leg
if (step == 6) drawAura(g2, headX, headY, headDiam, bodyX, bodyYStart, bodyYEnd, armLength, legLength);
}
private void drawAura(Graphics2D g2, int headX, int headY, int headDiam, int bodyX, int bodyYStart, int bodyYEnd, int armLength, int legLength) {
g2.setColor(new Color(0, 0, 255, 100));
g2.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.drawOval(headX, headY, headDiam, headDiam);
g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd);
g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20);
g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20);
g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength);
g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength);
}
private void drawSkull(Graphics2D g2) {
int skullX = 225, skullY = 100, skullDiam = 50;
// Skull
g2.setColor(Color.BLACK);
g2.fillOval(skullX, skullY, skullDiam, skullDiam);
// Eyes
g2.setColor(Color.WHITE);
g2.fillOval(skullX + skullDiam / 5, skullY + skullDiam / 6, skullDiam / 5, skullDiam / 5);
g2.fillOval(skullX + 3 * skullDiam / 5, skullY + skullDiam / 6, skullDiam / 5, skullDiam / 5);
// Nose
g2.fillOval(skullX + skullDiam / 2 - skullDiam / 12, skullY + skullDiam / 2 - skullDiam / 12, skullDiam / 6, skullDiam / 6);
// Mouth
g2.setStroke(new BasicStroke(2));
g2.drawLine(skullX + skullDiam / 5, skullY + 2 * skullDiam / 3,
skullX + 4 * skullDiam / 5, skullY + 2 * skullDiam / 3);
// GAME OVER message
g2.setColor(Color.RED);
g2.setFont(new Font("Arial", Font.BOLD, 36));
String message = "GAME OVER";
FontMetrics fm = g2.getFontMetrics();
int textWidth = fm.stringWidth(message);
int xText = (getWidth() - textWidth) / 2;
int yText = skullY - 20;
g2.drawString(message, xText, yText);
}
private void drawYouWinMessage(Graphics2D g2) {
g2.setColor(Color.GREEN);
g2.setFont(new Font("Arial", Font.BOLD, 36));
String message = "YOU WIN";
FontMetrics fm = g2.getFontMetrics();
int textWidth = fm.stringWidth(message);
int xText = (getWidth() - textWidth) / 2;
int yText = 50;
g2.drawString(message, xText, yText);
}
}

View File

@@ -0,0 +1,25 @@
import java.awt.event.*;
import javax.swing.SwingUtilities;
class Mouse extends MouseAdapter {
private Affiche aff;
private int step = 0; // correspond à Affiche.step
public Mouse(Affiche aff) {
this.aff = aff;
}
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
// clic droit -> gagne
aff.setYouWin(true);
} else {
// clic gauche -> incrémente étape
if (step < 7) {
step++;
aff.setStep(step); // <-- utiliser setStep
}
}
}
}