I am creating a battleship game with 4 classes using sockets. A computer, player message and a menu class. To start the game I run the computer class which is the server then I run the menu class which bring up the menu. Through the menu I click start to create a new player object which looks like this
public static void runPlayer()
{
Player client = new Player(); //creates player
client.createBoard();
client.run();
}
this runs perfectly runs without the menu class if i run the computer class then the player class, the game runs successfully. But when i call the run player method in the menu a window pops up with nothing in it.Here is my Menu class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import sun.audio.*;
public class Menu {
private javax.swing.JLabel image;
private static final int EXIT_ON_CLOSE = 0;
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JLabel statusbar = new JLabel(" Battleship");
JLabel Battleship = new JLabel(" Battleship ");
static AudioPlayer MGP = AudioPlayer.player;
static AudioStream BGM;
AudioData MD;
static ContinuousAudioDataStream loop = null;
public static void waiting (int n)
{
long t0, t1;
t0 = System.currentTimeMillis();
do{
t1 = System.currentTimeMillis();
}
while (t1 - t0 < n);
}
public Menu()
{
frame.setTitle("Battleship");
statusbar.setBorder(BorderFactory.createEtchedBorder(
EtchedBorder.RAISED));
Battleship.setBorder(BorderFactory.createEtchedBorder(
EtchedBorder.RAISED));
panel.setLayout(null);
JButton start = new JButton("Start");
start.setBounds(100, 660, 80, 25);
JButton exit = new JButton("Exit");
exit.setBounds(190, 660, 80, 25);
JButton StopMusic = new JButton("Stop Music");
StopMusic.setBounds(300, 660, 160, 25);
JButton StartMusic = new JButton("Start Music");
StartMusic.setBounds(470, 660, 160, 25);
Battleship.setFont(new Font("Courier New", Font.ITALIC, 36));
Battleship.setForeground(Color.BLACK);
image = new javax.swing.JLabel();
image.setIcon(new javax.swing.ImageIcon("./battleship2.jpg"));
frame.add(image, BorderLayout.EAST);
frame.pack();
frame.add(start);
frame.add(exit);
frame.add(StopMusic);
frame.add(StartMusic);
frame.add(panel);
frame.add(statusbar, BorderLayout.SOUTH);
frame.add(Battleship, BorderLayout.NORTH );
frame.setSize(700, 800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
music();
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
{
frame.dispose(); //closes frame
stopMusic(); //stops music
//waiting(500);
runPlayer();
}}
});
StopMusic.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
{
stopMusic();
}
}
});
StartMusic.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
{
startMusic();
}
}
});
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println( "Ending Game" );
System.exit(0);
}
});}
public static void music()
{
try
{
InputStream test = new FileInputStream("./battle.wav");
BGM = new AudioStream(test);
AudioPlayer.player.start(BGM);
}
catch(FileNotFoundException e){
System.out.print(e.toString());
}
catch(IOException error)
{
System.out.print(error.toString());
}
MGP.start(loop);
}
public void stopMusic()
{
if (BGM != null)
AudioPlayer.player.stop(BGM);
if (loop != null)
AudioPlayer.player.stop(loop);
}
public void startMusic() {
try
{
InputStream test = new FileInputStream("./battle.wav");
BGM = new AudioStream(test);
AudioPlayer.player.start(BGM);
}
catch(FileNotFoundException e){
System.out.print(e.toString());
}
catch(IOException error)
{
System.out.print(error.toString());
}
MGP.start(loop);
}
public static void runPlayer()
{
Player client = new Player(); //creates player
client.createBoard();
client.run();
}
public static void main(String[] args)
{
new Menu();
}
}
I think my problem is somewhere in this listener method
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
{
frame.dispose(); //closes frame
stopMusic(); //stops music
//waiting(500);
runPlayer();
}}
});