Skip to content

Commit

Permalink
Merge pull request #18 from nscherer30/nick/toggle-music
Browse files Browse the repository at this point in the history
Nick/Adjust music volume.  Resolves AB#683
  • Loading branch information
nickordoodle authored Feb 16, 2021
2 parents aa9720f + 4d227b6 commit 1b03f50
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/org/lizard/MapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class MapView extends JPanel {
public void paint(Graphics g) {


g.fillRect(size, size, size * numOfCols, size * numOfRows);
for (int column = 0; column < numOfCols; column++) {

for (int row = 0; row < numOfRows; row++) {
Expand Down
35 changes: 32 additions & 3 deletions src/org/lizard/MyJFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
package org.lizard;

import org.lizard.constants.GameInformation;
import org.lizard.constants.Settings;
import org.lizard.util.Music;
import org.lizard.util.Screen;

import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand Down Expand Up @@ -37,8 +40,9 @@ public class MyJFrame extends JFrame implements ActionListener {
JLabel imageLabel;
JTextField textField = new JTextField();
JButton enterGame, quitGame, helpBtn, musicBtn;
JSlider volumeSlider;
JFrame frame;
JPanel titlePanel;
JPanel titlePanel, volumePanel;
JTextArea mainStoryText, instructionsTxt;
JTextField desicionField = new JTextField();
List<GameDictionary.Noun> nounList = null;
Expand Down Expand Up @@ -101,6 +105,31 @@ public class MyJFrame extends JFrame implements ActionListener {
musicBtn.setBounds(1250, 415, 120, 40);
musicBtn.addActionListener(this);

FloatControl gainControl =
(FloatControl) Music.clip.getControl(FloatControl.Type.MASTER_GAIN);

//Create the slider to adjust volume
volumeSlider = new JSlider(JSlider.VERTICAL,
(int) gainControl.getMinimum(),
(int) gainControl.getMaximum(),
(int) gainControl.getValue());
volumeSlider.setMajorTickSpacing(25);
volumeSlider.setPaintTicks(true);
volumeSlider.setForeground(Color.BLUE);
volumeSlider.addChangeListener(event -> {
// Update global volume adjustment whenever it changes
JSlider volumeSlider = (JSlider) event.getSource();
if (!volumeSlider.getValueIsAdjusting()) {
Settings.VOLUME_SETTING = volumeSlider.getValue();
Music.adjustVolume((float) Settings.VOLUME_SETTING, gainControl);
}
});

// Create a volume panel "container" to hold the slider
volumePanel = new JPanel();
volumePanel.setBounds(1200, 550, 80, 250);
volumePanel.add(volumeSlider);

//panel with the game title
titlePanel = new JPanel();
titlePanel.setBackground(Color.black);
Expand All @@ -113,7 +142,6 @@ public class MyJFrame extends JFrame implements ActionListener {
frame.add(titlePanel);
frame.add(enterGame);
frame.add(imageLabel);

// Make the frame visible to the player
frame.setVisible(true);

Expand Down Expand Up @@ -244,6 +272,7 @@ public void createGameView() {
frame.add(helpBtn);
frame.add(quitGame);
frame.add(musicBtn);
frame.add(volumePanel);
titlePanel.setBounds(50, 50, 450, 80);
frame.add(titlePanel);
gameScreen(board.introduction());
Expand All @@ -267,7 +296,7 @@ public void actionPerformed(ActionEvent e) {
Music.stop();
} else {
// Otherwise, play music form the specified track
Music.play("princeofdarkness.wav");
Music.play();
}

}
Expand Down
6 changes: 6 additions & 0 deletions src/org/lizard/constants/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.lizard.constants;

public class Settings {
// Set default volume to 70
public static int VOLUME_SETTING = 70;
}
33 changes: 29 additions & 4 deletions src/org/lizard/util/Music.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class Music {


private static Clip clip = null;
public static Clip clip;

private static Music musicHandler = new Music();

private Music(){


private Music(){
loadTrack("princeofdarkness.wav");
}

private Music(List<String> listOfTracks){
//TODO Implement a multi song library
}

public static void play(String soundFileName) {
private static void loadTrack(String fileName){
AudioInputStream audioInputStream = null;

try {
audioInputStream = AudioSystem.getAudioInputStream(new File(soundFileName).getAbsoluteFile());
audioInputStream = AudioSystem.getAudioInputStream(new File(fileName).getAbsoluteFile());

} catch (UnsupportedAudioFileException | IOException unsupportedAudioFileException) {
unsupportedAudioFileException.printStackTrace();
}
Expand All @@ -34,13 +43,29 @@ public static void play(String soundFileName) {
} catch (LineUnavailableException | IOException lineUnavailableException) {
lineUnavailableException.printStackTrace();
}
}

public static void play() {
clip.start();
}

public static void stop(){
clip.stop();
}

// Amount is in decibels. Enter negative amount to reduce volume.
// Enter positive amount to raise volume.
public static void adjustVolume(float amount, FloatControl gainControl){

try {

gainControl.setValue(amount); //Change volume by the amount

} catch (NullPointerException | IllegalArgumentException e) {
e.printStackTrace();
}
}

public static boolean isRunning(){
try {
return clip.isRunning();
Expand Down

0 comments on commit 1b03f50

Please sign in to comment.