0

Hi i have made a game in java that extends applet. The game works perfectly fine but one of the requirements of this assignment is that there should be a menu. For example: As the program is run a screen with "Play" and "Quit" Options should appear and if user clicks "Play", this should lead on to the game, etc...

Q) Is there a way to do this specifically for applets?

I have attempted to make a a menu using the following code but it doesn't work (I think this is only for extends JPanel or JFrame not extends Applet):

MainMenu.java

public class MainMenu extends JFrame {

    int screenWidth = 200;
    int screenHeight = 150;

    int buttonWidth = 100;
    int buttonHeight = 40;

    JButton Play;
    JButton Quit;

    public MainMenu() {
        addButtons();
        addActions();

        Play.setBounds((screenWidth - buttonWidth)/2, 5 , buttonWidth, buttonHeight); // Positions the play button
        Quit.setBounds((screenWidth - buttonWidth)/2, 10 , buttonWidth, buttonHeight);

        //Adding buttons
        getContentPane().add(Play); //add the button to the Frame
        getContentPane().add(Quit);

        pack();
        setVisible(true);
        setLocationRelativeTo(null);
        setSize(screenWidth , screenHeight);
        setTitle("Drop");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

    }

    private void addButtons() {
        Play = new JButton ("Play");
        Quit = new JButton ("Quit");

    }

    private void addActions() {
        Play.addActionListener(new ActionListener() {   // takes play button, adds new actionlistener

            public void actionPerformed(ActionEvent e) { // Turn actionPerformed into variable for usage
                dispose();                               // wipes out Jframe

                Board game = new Board();

                game.run();
            }
        }); //Play Button

        Quit.addActionListener(new ActionListener() {   // takes quit button, adds new actionlistener

            public void actionPerformed(ActionEvent e) { // Turn actionPerformed into variable for usage
                System.exit(0); 
            }
        }); //Quit Button
    }
}

Launcher.java (Where menu is run from)

public class Launcher {

    public static void main (String[] args){

        new MainMenu();
    }
}

Any help is much appreciated (Ideas, tutorials...)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2348633
  • 13
  • 1
  • 4
  • Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson May 21 '13 at 22:50
  • umm tbh i was just following a tutorial i found on youtube and apparently its easier to add sounds etc.. in applet so decided to make the actual game using it. But now i have managed to convert my code to JApplet so have to figure out how to do a main menu for that. – user2348633 May 23 '13 at 10:20
  • *"its easier to add sounds etc.. in applet"* Not really, no. Not since Java Sound, `ImageIO` etc. – Andrew Thompson May 23 '13 at 10:27

2 Answers2

1

For many components in one space, use a CardLayout as see in this short example.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Cool, thanks i will try that. I just need to make sure that the game actually starts once the OK button is clicked and not when i run the program. – user2348633 May 23 '13 at 10:18
0

I just ended up making a text based menu.

Initially set a variable to true, for eg: menu = true and make the paint method paint whatever you want on the menu, like start...

    if(menu) {

      paint what's on the menu

    }

then when when user clicks on a certain option within the menu turn menu variable to false i.e. menu = false.

You need to get mouse input to get the user's input so use either the mouse pressed or mouse clicked methods that come with the mouselistener.

After turning it false, get the paint method to paint your game. i.e.

  if(!menu) {

  paint the game

  }

Pretty much a bunch of if statements.

Hope this helps someone.

user2348633
  • 13
  • 1
  • 4