Results 1 to 1 of 1
- 04-08-2014, 04:19 PM #1
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
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:
Java 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); } }
Java 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); } }
I tried to use this:
Java Code:JPanel game = new Game(); add(game);
Also tried:
Java Code:validate(); repaint(); JPanel menu = new Menu(); add(menu);
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
Similar Threads
-
Open a jar file with button swing and load this file.jar in a desktop pane
By lordhark in forum AWT / SwingReplies: 15Last Post: 02-16-2012, 04:43 PM -
Help with using Swing's 'paintComponent' and 'repaint' methods
By dpdwilson in forum AWT / SwingReplies: 0Last Post: 10-24-2011, 10:26 PM -
Java Swing repaint
By sz20b in forum Advanced JavaReplies: 1Last Post: 10-04-2010, 03:59 PM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:29 AM -
play vedio file and flash file in swing
By rajasekharreddym in forum AWT / SwingReplies: 1Last Post: 07-30-2008, 09:22 PM
Bookmarks