tags:

views:

62

answers:

2

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();





            }}
         });
+2  A: 

It's a bit hard to answer this question with the information provided. But I'll have to guess what the runPlayer() method is doing. I'm assuming there's some type of while loop in there that prevents the Event Dispatch Thread from refreshing the new JFrame you created.

Try to place the stopMusic() and runPlayer() in a new thread.

Basically something like this

start.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    frame.dispose(); // closes frame

    new Thread(){
      public void run(){
        stopMusic(); // stops music
        runPlayer();
      }
    }.start();
  }
}); 
David Young
absolute genius David, works perfectlyThanks
+1  A: 

I think the problem lies outside the code you've shown; it could be anything from a missing setVisible() to a synchronization problem. FWIW, I have a few observation about the code:

  1. Build your GUI on the EDT, @David Young just suggested.

  2. Use static constants to avoid repeating yourself.

  3. Don't use spaces to format labels; use the JLabel alignment constants.

  4. Instead of a null layout, use nested layouts to get the result you want.

  5. To avoid calling a public method in the constructor, you've duplicated the code of startMusic(). Instead, invoke it after the constructor completes.

Here's an example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
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.ImageIcon;
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 static final String TITLE = "Battleship";
    private static final String SOUND_FILE = "./battle.wav";
    private javax.swing.JLabel image;
    JFrame frame = new JFrame(TITLE);
    JPanel center = new JPanel(new BorderLayout());
    JLabel statusbar = new JLabel(TITLE);
    JLabel battleship = new JLabel(TITLE, JLabel.CENTER);
    static AudioPlayer MGP = AudioPlayer.player;
    static AudioStream BGM;
    static ContinuousAudioDataStream loop = null;
    AudioData MD;

    public Menu() {

        frame.setTitle("Battleship");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        statusbar.setBorder(BorderFactory.createEtchedBorder(
            EtchedBorder.RAISED));
        battleship.setBorder(BorderFactory.createEtchedBorder(
            EtchedBorder.RAISED));

        JButton start = new JButton("Start");
        JButton exit = new JButton("Exit");
        JButton StopMusic = new JButton("Stop Music");
        JButton StartMusic = new JButton("Start Music");

        battleship.setFont(new Font("Courier New", Font.ITALIC, 36));
        battleship.setForeground(Color.BLACK);

        image = new JLabel();
        image.setHorizontalAlignment(JLabel.CENTER);
        image.setIcon(new ImageIcon("./battleship2.jpg"));
        center.add(image, BorderLayout.CENTER);

        JPanel panel = new JPanel();
        panel.add(start);
        panel.add(exit);
        panel.add(StopMusic);
        panel.add(StartMusic);
        center.add(panel, BorderLayout.SOUTH);

        frame.add(battleship, BorderLayout.NORTH);
        frame.add(center, BorderLayout.CENTER);
        frame.add(statusbar, BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        start.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //frame.dispose();
                stopMusic();
                runPlayer();
            }
        });
        StopMusic.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                stopMusic();
            }
        });
        StartMusic.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                startMusic();
            }
        });

        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("Ending Game");
                System.exit(0);
            }
        });
    }

    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(SOUND_FILE);
            BGM = new AudioStream(test);
            AudioPlayer.player.start(BGM);
            MGP.start(loop);
        } catch (FileNotFoundException e) {
            System.out.print(e.toString());
        } catch (IOException error) {
            System.out.print(error.toString());
        }
    }

    public static void runPlayer() {
        Player client = new Player();
        client.createBoard();
        client.run();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Menu menu = new Menu();
                menu.startMusic();
            }
        });
    }

    // stub for missing class
    private static class Player {
        void createBoard() {}
        void run() {}
    }
}
trashgod