Changing backgrounds and starting a thread, help!
I am rather new to java, just a 10th grader in my schools Programming II class. I recently took up the challenge of creating the old school Pong game. I have the game all working and stuff, to were it runs and you can play. However, I have been trying to build upon that. I want there to be a main "Title screen" with a button that changes the image of the background to the main game background and also starting my game thread. I have all these in seperate classes, and I can't seem to figure out how when the area of the button is clicked, to change the image and start the game. I've been looking around for hours and still I am clueless. If someone could help, I'd much appreciate it.
Re: Changing backgrounds and starting a thread, help!
Welcome to Java Programming Forums zachbray. Without seeing your code we can only assume. So, first assumption, incoming is to create a frame and call the game starting thread frame by clicking on the button of the created frame and pass the image to the next where you are setting the image as background in your game.
Re: Changing backgrounds and starting a thread, help!
Excuse me for not putting any of my code. Here is my main class, which handles the frames.
import javax.swing.*;
public class Windows extends JPanel{
public static void main(String[] args){
Graphic g = new Graphic();
RunGame rungame = new RunGame();
TitleScreen title = new TitleScreen();
JFrame mainWindow = new JFrame("Main Window");
mainWindow.setSize(700,500);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);
mainWindow.setVisible(true);
mainWindow.setResizable(false);
mainWindow.setLocationRelativeTo(null);
mainWindow.add(title);
}
}
The thing I've been trying to figure out is how to (on response to clicking) remove title, then add g and rungame to mainWindow. I've tried a ton of ways but they always just keep the component title up. If you know of anyways I could do this, help, or if there is another way to do it other than what I've been trying, please tell me.
Re: Changing backgrounds and starting a thread, help!
Quote:
Originally Posted by
zachbray
Excuse me for not putting any of my code. Here is my main class, which handles the frames.
import javax.swing.*;
public class Windows extends JPanel{
public static void main(String[] args){
Graphic g = new Graphic();
RunGame rungame = new RunGame();
TitleScreen title = new TitleScreen();
JFrame mainWindow = new JFrame("Main Window");
mainWindow.setSize(700,500);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);
mainWindow.setVisible(true);
mainWindow.setResizable(false);
mainWindow.setLocationRelativeTo(null);
mainWindow.add(title);
}
}
The thing I've been trying to figure out is how to (on response to clicking) remove title, then add g and rungame to mainWindow. I've tried a ton of ways but they always just keep the component title up. If you know of anyways I could do this, help, or if there is another way to do it other than what I've been trying, please tell me.
Your game will be surely in some other frame, so you just need to read about action listeners and then apply them for invoking the new frame that is in this case your game class frame.