From 0d322b3c77da68f4b773ad77815fa62213afa77e Mon Sep 17 00:00:00 2001 From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr> Date: Tue, 10 May 2016 10:57:55 +0200 Subject: [PATCH] Modified sound in GUI --- .../{pingu_generique.wav => pingu_theme.wav} | Bin gui/src/controller/Controller.java | 38 +++++++++++------- gui/src/main/Main.java | 8 ---- 3 files changed, 23 insertions(+), 23 deletions(-) rename gui/resource/{pingu_generique.wav => pingu_theme.wav} (100%) diff --git a/gui/resource/pingu_generique.wav b/gui/resource/pingu_theme.wav similarity index 100% rename from gui/resource/pingu_generique.wav rename to gui/resource/pingu_theme.wav diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java index 9387e8b..1dbc42e 100644 --- a/gui/src/controller/Controller.java +++ b/gui/src/controller/Controller.java @@ -26,6 +26,7 @@ import java.util.ResourceBundle; public class Controller implements Initializable { + private static final String introMusicFile = "./resource/pingu_theme.wav"; private GameState gameState; private Process gameProcess; @@ -103,6 +104,13 @@ public class Controller implements Initializable UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, humanPlayer, scoreRed, scoreBlue, turnLabel); upT.setDaemon(true); upT.start(); + try + { + AudioPlayer.player.start(new AudioStream(new FileInputStream(introMusicFile))); + } catch (IOException e) + { + e.printStackTrace(); + } } /** @@ -114,6 +122,7 @@ public class Controller implements Initializable private int tileNumber; + private final String clickSoundFile = "./resource/clic.wav"; public TileClickHandler(int tileNumber) { @@ -130,30 +139,26 @@ public class Controller implements Initializable } } - @Override - public void handle(MouseEvent event) + private void playClickSound() { - // open the sound file as a Java input stream - String intro = "./resource/clic.wav"; - InputStream in = null; - try { - in = new FileInputStream(intro); - // create an audiostream from the inputstream - AudioStream audioStream = new AudioStream(in); - // play the audio clip with the audioplayer class - AudioPlayer.player.start(audioStream); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { + try + { + AudioPlayer.player.start(new AudioStream(new FileInputStream(clickSoundFile))); + } catch (IOException e) + { e.printStackTrace(); } + } - + @Override + public void handle(MouseEvent event) + { if (!gameState.getCurrent_player().equals(humanPlayer)) return; if (selectedTile == tileNumber) //If we clicked again on the selected tile { + playClickSound(); //UnSelect and un-highlight previously selected and highlighted tiles boardView[selectedTile].setSelected(false); boardView[selectedTile].update(); @@ -163,9 +168,11 @@ public class Controller implements Initializable } //We clicked on a tile that wasn't previously selected + //Selecting the tile int penguinNb = gameState.getPenguinOnTile(humanPlayer, tileNumber); if (penguinNb != -1) //There is a penguin on this tile: we want to select it { + playClickSound(); //Unselect previously selected tiles if (selectedTile != -1) { @@ -217,6 +224,7 @@ public class Controller implements Initializable } else if (boardView[tileNumber].isHighlighted()) //There is no penguin but this is a possible move for the penguin on the selected tile { + playClickSound(); Move move = boardView[tileNumber].getHighlightMove(); int moveNb = gameState.getPlayerMoveNumber(humanPlayer, gameState.getPenguinOnTile(humanPlayer, selectedTile), move.getDirection(), move.getSteps()); //Before playing the move: deselect and de-highlight tiles diff --git a/gui/src/main/Main.java b/gui/src/main/Main.java index 45a4370..0a6f66f 100644 --- a/gui/src/main/Main.java +++ b/gui/src/main/Main.java @@ -32,14 +32,6 @@ public class Main extends Application primaryStage.setScene(scene); primaryStage.setTitle("Penguin game"); primaryStage.show(); - - // open the sound file as a Java input stream - String intro = "./resource/pingu_generique.wav"; - InputStream in = new FileInputStream(intro); - // create an audiostream from the inputstream - AudioStream audioStream = new AudioStream(in); - // play the audio clip with the audioplayer class - AudioPlayer.player.start(audioStream); } catch (Exception e) { -- GitLab