Skip to content
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/bin/


ConnectFourGame/src/connect/four/gui/JnarioTest\.feature

connectfour\.jar

*._trace

Binary file not shown.
Binary file added ConnectFourGame/src/button.wav
Binary file not shown.
22 changes: 21 additions & 1 deletion ConnectFourGame/src/connect/four/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
}
25 changes: 24 additions & 1 deletion ConnectFourGame/src/connect/four/gui/GameOverPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,20 +101,36 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}// </editor-fold>//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();
gui.repaint();
}//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;
Expand Down
37 changes: 37 additions & 0 deletions ConnectFourGame/src/connect/four/gui/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -427,6 +434,7 @@ else if(game.getCurrentPlayer() == players[1]) {
player.getBoard().play(getColumnNum(), player);
}
}
pieceSound(); //Plays piece sound
turnUp();
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
80 changes: 80 additions & 0 deletions ConnectFourGame/src/connect/four/gui/MainMenuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +50,8 @@ private void initComponents() {
butPlay = new javax.swing.JButton();
jtComputerToggle = new javax.swing.JToggleButton();
compDifficulty = new JComboBox<String>(difficulties);
musicAttribute = new javax.swing.JLabel();
musicDL = new javax.swing.JButton();

compDifficulty.setSelectedIndex(1); // sets "Normal" as default choice
compDifficulty.setVisible(false);
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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))

);
}// </editor-fold>//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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -207,8 +285,10 @@ public int getDiff() {
private javax.swing.JLabel jLabel2;
private javax.swing.JComboBox<String> 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
}
Binary file added ConnectFourGame/src/gameOver.wav
Binary file not shown.
Binary file added ConnectFourGame/src/pieceNoise1.wav
Binary file not shown.
Binary file added ConnectFourGame/src/pieceNoise2.wav
Binary file not shown.