Skip to content
Snippets Groups Projects
Commit 10cb2254 authored by MEDEDJI Setondji's avatar MEDEDJI Setondji :speech_balloon:
Browse files

ajout du bouton pour couper le son

parent 239ce354
No related branches found
No related tags found
No related merge requests found
Pipeline #25206 failed
......@@ -49,6 +49,8 @@ public class Kernel {
this.update();
this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane());
soundEngine = new SoundEngine();
String music = "src/main/resources/Sound/son.wav";
SoundEngine.playMusic(music);
start();
}
......@@ -194,4 +196,8 @@ public class Kernel {
button.setSize(50, 30);
graphicEngine.addButton(button);
}
public void addLabel(JLabel label){
graphicEngine.addLabel(label);
}
}
......@@ -3,12 +3,14 @@ package engine.graphic;
import engine.Engine;
import engine.Kernel;
import engine.physic.Coordinates2D;
import engine.sound.SoundEngine;
import pong.PongApp;
import pong.PongObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class GraphicEngine extends JFrame implements Engine {
......
......@@ -7,8 +7,10 @@ import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
public class SoundEngine implements Engine {
private static Clip clip; // Ajout de cette variable pour stocker le clip en cours de lecture
public SoundEngine(){}
public static void playMusic(String location){
......@@ -16,7 +18,7 @@ public class SoundEngine implements Engine {
File musicPath = new File(location);
if (musicPath.exists()) {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
}
......@@ -29,6 +31,12 @@ public class SoundEngine implements Engine {
}
}
public static void stopMusic() {
if (clip != null && clip.isRunning()) {
clip.stop();
}
}
@Override
public void update() {
if (Kernel.isCollision){
......@@ -36,8 +44,11 @@ public class SoundEngine implements Engine {
SoundEngine.playMusic(collisionPath);
Kernel.isCollision = false;
}
}
@Override
public void restart() {
......
......@@ -2,9 +2,12 @@ package pong;
import engine.physic.Coordinates2D;
import engine.Kernel;
import engine.sound.SoundEngine;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
......@@ -46,6 +49,18 @@ public class PongApp {
Kernel kernel = new Kernel("Pong", width, height, components);
JButton button = new JButton("Restart");
JButton button2 = new JButton("Mute");
/*JLabel muteLabel = new JLabel(new ImageIcon("src/main/resources/Sound/mute.png"));
// définir la taille de l'image
muteLabel.setBounds(0, 0, 10, 50);
muteLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Mettez ici le code pour gérer le clic sur l'image (par exemple, pour activer/désactiver le son)
SoundEngine.stopMusic(); // Exemple, vous devrez implémenter cette méthode
}
});*/
button.addActionListener(e -> {
score1 = 0;
score2 = 0;
......@@ -59,7 +74,10 @@ public class PongApp {
}
});
button2.addActionListener(e -> SoundEngine.stopMusic());
kernel.addButton(button); // Add a button to restart the game
kernel.addButton(button2); // Add a button to mute the game
//kernel.addLabel(muteLabel); // Add a button to mute the game
final long MIN_INTERVAL_TIME = 10; // Minimum time between two iterations
......@@ -91,6 +109,7 @@ public class PongApp {
}
}
/**
......@@ -100,4 +119,6 @@ public class PongApp {
isGoal = false;
components.get(2).setPosition(new Coordinates2D(width/2, height/2));
}
}
src/main/resources/Sound/mute.png

91.1 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment