1

I am making a Java game and I need some advice on how to do something.

Currently I am using 2 different .java files to make the game. I have a main program that extends JFrame and I call the other .java file which extends JPanel.

The game currently works but I want to make another mode for the game and I think it would be better if I made another file for it.

Here is all of the code for the main file. I removed all the useless unneeded things:

import java.awt.*;
import javax.swing.*;

public class ProgramMain extends JFrame  implements KeyListener
{
    private Program mode1;
    private Program2 mode2;

    public ProgramMain ()
    {
        setResizable (false);
        mode1 = new Program ();
        getContentPane ().add (mode1, BorderLayout.CENTER);
        if (mode1.change == true)
        {
            this.remove (mode1);
            board = new Program2 ();
            this.getContentPane ().add (mode2);            
        }
    }

  public void actionPerformed (ActionEvent event)
  {
  }

    public static void main (String[] args)
    {
        GameMain frame = new GameMain ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.pack ();
        frame.setVisible (true);
    }
}

Is there a way where I can relaunch the ProgramMain () method so it does another check, so if this time, the if statement becomes true, it changes the screen and opens the Program2.java file instead.

That is my main problem here. I am not sure how I can switch between .java programs. What do I need to do if I would like my Main program (ProgramMain) to read another program/.java file.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ashimashi
  • 463
  • 1
  • 5
  • 14
  • what u mean by have another file? do you want to open some frame n want to be able switch between the frame? – koponk Jan 13 '13 at 00:09
  • Sorry I should have been a little more specific of what I meant. I will edit my first question to explain better because it is hard to explain in a comment – ashimashi Jan 13 '13 at 04:33
  • `this.remove(screen); this.add(otherPanel);` Use a `CardLayout` as shown in [this answer](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005)! – Andrew Thompson Jan 13 '13 at 04:56
  • Thanks a lot for the link. But it is not really what I am looking for. But it is a good second option to consider for me if what I am trying to do does not work. – ashimashi Jan 13 '13 at 06:45

1 Answers1

0

For switching you can have a action in your frame where you need switch.

ActionListener al = new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
        if (board1) {
            getContentPane().add(gameBoard, BorderLayout.CENTER);
        } else {
            getContentPane().add(gameBoard2, BorderLayout.CENTER);
        }
    }
};
buttonSwitch.addActionListener (al);
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • I actually just found a very nice way of doing this, it is close to what you have posted here. But the one problem is, this check is only preformed once at the start. Is there a way to make this check happen every time paintComponent is repainted in the gameBoard java program? – ashimashi Jan 13 '13 at 06:36
  • neither your question nor your comments are clear what you are trying to do – vels4j Jan 13 '13 at 07:58
  • Sorry Vels4j, I seemed to have even confused myself with my explanations. I changed the whole question and made it more straight forward to understand what I am trying to accomplish. Thanks! – ashimashi Jan 13 '13 at 17:00
  • do you need to switch second program in run time or when startup ? – vels4j Jan 13 '13 at 17:23
  • I would like the switch to happen in run time. Basically in the program, if the person clicks on a certain area of the screen, it will switch to the other program. – ashimashi Jan 13 '13 at 17:33
  • Yes I see what you mean, but my mouse/click action is not in the main program (ProgramMain), but in the programs that I want to switch between. All my mouse and keyboard methods/commands are within the program1 and program2 that the main program reads/opens. – ashimashi Jan 13 '13 at 17:44
  • have that switch in ProgramMain itself. – vels4j Jan 13 '13 at 17:47
  • Sorry but where must I place this code? I already have actionPerformed in the ProgramMain which is used for the menu options. I can't just replace the current actionPerformed with the one you provided, I also have implemented ActionListener already for ProgramMain. If it is hard to understand what I mean, I could post the full code, I have eliminated all the menu and menu items from the code above to keep it neat. – ashimashi Jan 13 '13 at 18:05