C. Chapman
C. Chapman

Reputation: 3

JFrame won't load the images I want it to

Please be patient with me.. I'm very new to Java.

I have two separate JFrames and the first loads the background I want but when I dispose the first JFrame and load the second one it loads with the background from the first.

j1.java

    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;

    public class j1 extends JFrame implements KeyListener {
        public bg1 img;
        public bg2 img2;

        public j1() { 
            lvl1();
        }

        private JFrame lvl1() { 
            this.img=new bg1();
            addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
            setTitle("lvl1");
            setResizable(false);
            setSize(600, 600);
            setMinimumSize(new Dimension(600, 600));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            getContentPane().add(img);
            pack();
            setVisible(true);
            return(this);
        }

        private JFrame lvl2() {
            this.img2=new bg2();
            addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
            setTitle("lvl2");
            setResizable(false);
            setSize(600, 600);
            setMinimumSize(new Dimension(600, 600));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            getContentPane().add(img2);
            pack();
            setVisible(true);
            return(this);
        }

        public void keyPressed(KeyEvent e) { }
        public void keyReleased(KeyEvent e) { 
            if(e.getKeyCode()== KeyEvent.VK_RIGHT) {
                lvl1().dispose();
                lvl2();
                }
        }
        public void keyTyped(KeyEvent e) { }

        public static void main(String[] args) {
            new j1();
        }
    }

bg1.java

    import java.awt.Graphics;
    import javax.swing.JComponent;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;

    public class bg1 extends JComponent {
        public BufferedImage person;
        public BufferedImage background;
        public bg1() { 
            loadImages2();
    }

        public void loadImages2() {
            try {
                String personn = "Images/person.gif";
                person = ImageIO.read(new File(personn));
                String backgroundd = "Images/background2.jpg";
                background = ImageIO.read(new File(backgroundd));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(background, 0, 0, this);
            g.drawImage(person, 100, 100, this);
        }

        public static void main(String[] args) {
            new bg1();
        }
    }

bg2.java is very similar to bg1.java but it has different names for the images and voids.

Upvotes: 0

Views: 49

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

So you kind of have a series of problems.

First, this is one of the dangers of re-using a frame this way, basically, you never actually remove bg1 from the frame, you just keep adding new instances of the bg2. This means that bg1 is still visible and valid on the frame...

Second, you're calling lvl1() AGAIN before you call lvl2, which is making a new instance of bg1 and adding that to the window and then disposing of it (which does NOT dispose of the components) and then you add a new instance of lvl2 to the frame and the whole thing is just one big mess.

Instead, you should simply be using a CardLayout which will allow you to switch between the individual views more elegantly. See How to Use CardLayout for moer details.

You should also have a look at How to Use Key Bindings instead of using KeyListener

As general rule of thumb, you should avoid overriding JFrame, this has a nasty habit of just confusing the whole thing. Simple create a new instance of a JFrame when you need it and add you components directly to it. Before anyone takes that the wrong way, you'll also want to have a look at The Use of Multiple JFrames, Good/Bad Practice?

Upvotes: 2

Related Questions