SWING - skip from file to file and repaint everything?
Hi.
I have little problem. I want to erase everything from my screen and paint everything what is in another file.
I have something like that:
Menu:
Code:
package jumperSwing;
// DRAW MENU CONTENT IN PANEL
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Menu extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton start;
private JButton exit;
private BufferedImage MenuBG;
public Menu() {
super();
setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
File imageFile = new File("images/sky.jpg");
try {
MenuBG = ImageIO.read(imageFile);
} catch (IOException e) {
System.err.println("Image Error");
e.printStackTrace();
}
start = new JButton("Start");
exit = new JButton("Exit");
start.addActionListener(this);
exit.addActionListener(this);
add(start);
add(exit);
}
@Override
public void actionPerformed(ActionEvent e) {
Object click = e.getSource();
if(click == start){
//skip to game
}
else if(click == exit)
System.exit(0);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(MenuBG, 0, 0, this);
}
}
Game:
Code:
package jumperSwing;
// DRAW GAME CONTENT IN PANEL
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton jump;
private BufferedImage GameBG;
public Game(){
super();
setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
File imageFile = new File("images/sky2.jpg");
try {
GameBG = ImageIO.read(imageFile);
} catch (IOException e) {
System.err.println("Image Error");
e.printStackTrace();
}
jump = new JButton("Jump");
jump.addActionListener(this);
add(jump);
}
@Override
public void actionPerformed(ActionEvent e) {
Object click = e.getSource();
if(click == jump){
// jump + points
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(GameBG, 0, 0, this);
}
}
There is comment "//skip to game" in Menu, where I want put jump to Game file, but I have no idea how.
I tried to use this:
Code:
JPanel game = new Game();
add(game);
But It can't do anything.
Also tried:
Code:
validate();
repaint();
JPanel menu = new Menu();
add(menu);
But it just draw "jump" button in first free place(just like new button cause of flowlayout).
So. How can I stop and erase menu and paint game instead?
// Yea yea, I know maybe it doesnt make sense now to create 2 files with same things, but I just want to know how to do this and then I will change almost everything in Game ofc :P