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.