diff --git a/.gitignore b/.gitignore index 51d2c3e..cb05413 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ /bin/ + ConnectFourGame/src/connect/four/gui/JnarioTest\.feature connectfour\.jar *._trace + diff --git a/ConnectFourGame/src/ConnectFourBackgroundMusic.wav b/ConnectFourGame/src/ConnectFourBackgroundMusic.wav new file mode 100644 index 0000000..4f874b8 Binary files /dev/null and b/ConnectFourGame/src/ConnectFourBackgroundMusic.wav differ diff --git a/ConnectFourGame/src/button.wav b/ConnectFourGame/src/button.wav new file mode 100644 index 0000000..1c0ce4d Binary files /dev/null and b/ConnectFourGame/src/button.wav differ diff --git a/ConnectFourGame/src/connect/four/gui/GUI.java b/ConnectFourGame/src/connect/four/gui/GUI.java index 63799e6..98a4002 100644 --- a/ConnectFourGame/src/connect/four/gui/GUI.java +++ b/ConnectFourGame/src/connect/four/gui/GUI.java @@ -6,9 +6,14 @@ package connect.four.gui; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.FloatControl; public class GUI extends javax.swing.JFrame { private static final long serialVersionUID = -8980582623705405399L; + public static float AUDIO_GAIN = -13; MainMenuPanel mainMenu; GamePanel gamePanel; GameOverPanel gameOverPanel; @@ -18,11 +23,11 @@ public class GUI extends javax.swing.JFrame { int score1, score2; public GUI() { + backgroundMusic(); initComponents(); score1 = 0; score2 = 0; mainMenu = new MainMenuPanel(this); - gamePanel = new GamePanel(this, mainMenu.getIsEnabled(), mainMenu.getDiff()); add(mainMenu); } @@ -74,6 +79,7 @@ public static void main(String args[]) { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { + @Override public void run() { new GUI().setVisible(true); } @@ -152,6 +158,20 @@ void setScore2(int newScore){ score2 = newScore; } + public final void backgroundMusic() { + try { + AudioInputStream in = AudioSystem.getAudioInputStream(getClass().getResource("/ConnectFourBackgroundMusic.wav")); + Clip clip = AudioSystem.getClip(); + clip.open(in); + FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + volumeControl.setValue(GUI.AUDIO_GAIN); + clip.loop(Clip.LOOP_CONTINUOUSLY); + clip.start(); + } catch(Exception any) { + System.out.println("Exception: " + any); + } + } + // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables } diff --git a/ConnectFourGame/src/connect/four/gui/GameOverPanel.java b/ConnectFourGame/src/connect/four/gui/GameOverPanel.java index 41e36c4..c36d7c5 100644 --- a/ConnectFourGame/src/connect/four/gui/GameOverPanel.java +++ b/ConnectFourGame/src/connect/four/gui/GameOverPanel.java @@ -2,6 +2,13 @@ package connect.four.gui; +import java.io.InputStream; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.FloatControl; + public class GameOverPanel extends javax.swing.JPanel { private static final long serialVersionUID = 92671837883194129L; @@ -94,7 +101,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { }// //GEN-END:initComponents private void butPlayAgainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butPlayAgainActionPerformed - + buttonSound(); //Play button sound gui.remove(this); gui.addGamePanel(); gui.revalidate(); @@ -102,12 +109,28 @@ private void butPlayAgainActionPerformed(java.awt.event.ActionEvent evt) {//GEN- }//GEN-LAST:event_butPlayAgainActionPerformed private void butMainMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butMainMenuActionPerformed + buttonSound(); //Play button sound gui.remove(this); gui.addMainMenu(); gui.revalidate(); gui.repaint(); }//GEN-LAST:event_butMainMenuActionPerformed + //Plays Button Sound + public void buttonSound() { + try { + AudioInputStream in = AudioSystem.getAudioInputStream(getClass().getResource("/button.wav")); + Clip clip = AudioSystem.getClip(); + clip.open(in); + FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + volumeControl.setValue(GUI.AUDIO_GAIN); + clip.start(); + + } catch(Exception any) { + System.out.println("Exception : " + any); + } + } + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton butMainMenu; private javax.swing.JButton butPlayAgain; diff --git a/ConnectFourGame/src/connect/four/gui/GamePanel.java b/ConnectFourGame/src/connect/four/gui/GamePanel.java index 9553dfa..574ffee 100644 --- a/ConnectFourGame/src/connect/four/gui/GamePanel.java +++ b/ConnectFourGame/src/connect/four/gui/GamePanel.java @@ -9,9 +9,16 @@ import connect.four.*; import connect.four.board.*; import connect.four.player.*; + import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.InputStream; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.FloatControl; import javax.swing.Timer; @@ -427,6 +434,7 @@ else if(game.getCurrentPlayer() == players[1]) { player.getBoard().play(getColumnNum(), player); } } + pieceSound(); //Plays piece sound turnUp(); } } @@ -626,6 +634,7 @@ else if(game.getCurrentPlayer() == players[1]){ gui.setWinner(game.getCurrentPlayer().getName()); board.clear(); + gameOverSound(); //Plays game over sound initNewGame(); gui.addGameOver(); justWon = true; @@ -652,6 +661,34 @@ public void actionPerformed(ActionEvent e) { timer.start(); } + //Plays game over sound + public void gameOverSound() { + try { + AudioInputStream in = AudioSystem.getAudioInputStream(getClass().getResource("/gameOver.wav")); + Clip clip = AudioSystem.getClip(); + clip.open(in); + clip.start(); + + } catch(Exception any) { + System.out.println("Exception : " + any); + } + } + + //Play piece drop sound + public void pieceSound() { + try { + AudioInputStream in = AudioSystem.getAudioInputStream(getClass().getResource("/pieceNoise2.wav")); + Clip clip = AudioSystem.getClip(); + clip.open(in); + FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + volumeControl.setValue(GUI.AUDIO_GAIN); + clip.start(); + + } catch(Exception any) { + System.out.println("Exception : " + any); + } + } + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel bgImage; private javax.swing.JPanel col1; diff --git a/ConnectFourGame/src/connect/four/gui/MainMenuPanel.java b/ConnectFourGame/src/connect/four/gui/MainMenuPanel.java index f51a839..d039651 100644 --- a/ConnectFourGame/src/connect/four/gui/MainMenuPanel.java +++ b/ConnectFourGame/src/connect/four/gui/MainMenuPanel.java @@ -6,9 +6,17 @@ package connect.four.gui; +import connect.four.*; + +import java.awt.Desktop; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JComboBox; +import java.io.*; +import java.net.URI; + +import javax.sound.sampled.*; +import javax.swing.*; public class MainMenuPanel extends javax.swing.JPanel { private static final long serialVersionUID = -3250605153152509088L; @@ -42,6 +50,8 @@ private void initComponents() { butPlay = new javax.swing.JButton(); jtComputerToggle = new javax.swing.JToggleButton(); compDifficulty = new JComboBox(difficulties); + musicAttribute = new javax.swing.JLabel(); + musicDL = new javax.swing.JButton(); compDifficulty.setSelectedIndex(1); // sets "Normal" as default choice compDifficulty.setVisible(false); @@ -95,6 +105,18 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jtComputerToggleActionPerformed(evt); } }); + + musicAttribute.setFont(new java.awt.Font("Lucida Grande", 0, 12)); + musicAttribute.setForeground(new java.awt.Color(255, 255, 255)); + musicAttribute.setText("Music: \"Smudj\" by Foniqz licensed under CC 3.0 "); + + musicDL.setFont(new java.awt.Font("Lucida Grande", 0, 12)); + musicDL.setText("Source"); + musicDL.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + musicDLAction(evt); + } + }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); @@ -118,6 +140,10 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGap(18, 18, 18) .addComponent(compDifficulty) .addComponent(tfplayer2, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addGroup(layout.createSequentialGroup() + .addGap(500, 500, 500) + .addComponent(musicAttribute) + .addComponent(musicDL)) .addGroup(layout.createSequentialGroup() .addGap(524, 524, 524) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) @@ -143,12 +169,19 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addComponent(jtComputerToggle, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(45, 45, 45) .addComponent(butPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(280,280,280) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addGap(50, 50, 50) + .addComponent(musicAttribute) + .addComponent(musicDL)) .addContainerGap(352, Short.MAX_VALUE)) + ); }// //GEN-END:initComponents private void butPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butPlayActionPerformed + buttonSound(); if (tfplayer1.getText().length() >= MAX_CHARACTERS_IN_NAME ) // if more than max character input tfplayer1.setText(tfplayer1.getText().substring(0, MAX_CHARACTERS_IN_NAME)); // sets name to substring if (tfplayer2.getText().length() >= MAX_CHARACTERS_IN_NAME ) // if more than max character input @@ -178,6 +211,7 @@ else if(compDifficulty.getSelectedIndex() == 3) }//GEN-LAST:event_butPlayActionPerformed private void jtComputerToggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtComputerToggleActionPerformed + toggleSound(); //Plays toggle button sound if(!isEnabled){ tfplayer2.setText("Computer"); tfplayer2.setEditable(false); @@ -194,6 +228,50 @@ private void jtComputerToggleActionPerformed(java.awt.event.ActionEvent evt) {// } }//GEN-LAST:event_jtComputerToggleActionPerformed + public void musicDLAction(java.awt.event.ActionEvent evt) { + buttonSound(); + if (Desktop.isDesktopSupported()) { + try { + URI uri = new URI("http://freemusicarchive.org/music/Foniqz/D2_EP/02_Smudj"); + Desktop.getDesktop().browse(uri); + }catch (Exception any) { + System.out.println("Exception: " + any); + } + } + } + + //Sound for toggle switch + public void toggleSound() { + + try { + + AudioInputStream in = AudioSystem.getAudioInputStream(getClass().getResource("/pieceNoise1.wav")); + Clip clip = AudioSystem.getClip(); + clip.open(in); + FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + volumeControl.setValue(GUI.AUDIO_GAIN); + clip.start(); + + }catch(Exception any) { + System.out.println("Exception: " + any); + } + } + + //Sound for button press + public void buttonSound() { + try { + AudioInputStream in = AudioSystem.getAudioInputStream(getClass().getResource("/button.wav")); + Clip clip = AudioSystem.getClip(); + clip.open(in); + FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + volumeControl.setValue(GUI.AUDIO_GAIN); + clip.start(); + + } catch(Exception any) { + System.out.println("Exception : " + any); + } + } + public boolean getIsEnabled() { return isEnabled; } @@ -207,8 +285,10 @@ public int getDiff() { private javax.swing.JLabel jLabel2; private javax.swing.JComboBox compDifficulty; private javax.swing.JToggleButton jtComputerToggle; + private javax.swing.JButton musicDL; private javax.swing.JTextField tfplayer1; private javax.swing.JTextField tfplayer2; private javax.swing.JLabel title; + private javax.swing.JLabel musicAttribute; // End of variables declaration//GEN-END:variables } diff --git a/ConnectFourGame/src/gameOver.wav b/ConnectFourGame/src/gameOver.wav new file mode 100644 index 0000000..0e02715 Binary files /dev/null and b/ConnectFourGame/src/gameOver.wav differ diff --git a/ConnectFourGame/src/pieceNoise1.wav b/ConnectFourGame/src/pieceNoise1.wav new file mode 100644 index 0000000..71f48a9 Binary files /dev/null and b/ConnectFourGame/src/pieceNoise1.wav differ diff --git a/ConnectFourGame/src/pieceNoise2.wav b/ConnectFourGame/src/pieceNoise2.wav new file mode 100644 index 0000000..d2f1579 Binary files /dev/null and b/ConnectFourGame/src/pieceNoise2.wav differ