0

I was creating an application with an image drawn in it, and it was working perfectly fine. For some reason, however, the image has stopped loading. The image is in the root folder of my project directory. Here is my code:

JFrame:

package com.cgp.tetris; 

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class TetrisFrame extends JFrame {
        private static final long serialVersionUID = 1L;

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

        public TetrisFrame() {
                add(new TetrisMenu());
                setTitle("Tetris");
                setSize(new Dimension(640, 576));
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                setResizable(false);
                Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                setLocation((d.width / 2) - 320, (d.height / 2) - 288);
        }
    }

JPanel:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class TetrisMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private BufferedImage titletop, titlebottom;

    public TetrisMenu() {
        super();
    }

    public void run() {
        loadImages();
    }

    private void loadImages() {
        try {
            titletop = ImageIO.read(new File("tetrispic.png"));
            titlebottom = ImageIO.read(new File("titlebottom.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(titletop, 0, 0, 640, 440, null);
        g.drawImage(titlebottom, 0, 440, null);
    }
}

Thanks in advance!

Cg2916
  • 1,117
  • 6
  • 23
  • 36

1 Answers1

1

The reason your images don't load consistently is because the loading is done in a thread, which has unpredictable behavior, and even worse, the starting of that thread depends on when addNotify is called (which I'm guessing is also unpredictable). You can fix this by putting loadImages(); and then repaint(); in the constructor. Get rid of the addNotify override, the run method, and the Runnable modifier. This is only a temporarily solution (probably fine for your needs), as it's not recommended to do intensive work (loading many images) on the EDT (Event-dispatch thread), because it can result in a non-responsive GUI. Image loading should be done in an instance of SwingWorker or before the GUI is constructed.

class TetrisMenu extends JPanel {

    private static final long   serialVersionUID    = 1L;
    private Thread              thread;
    private BufferedImage       titletop, titlebottom;

    public TetrisMenu() {
        super();
        loadImages();
        repaint();
    }

    private void loadImages() {
        try {
            titletop = ImageIO.read( new File( "tetrispic.png" ) );
            titlebottom = ImageIO.read( new File( "titlebottom.png" ) );
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    public void paint( Graphics g ) {
        super.paint( g );
        if ( titletop != null )
            g.drawImage( titletop, 0, 0, 640, 440, null );
        if ( titlebottom != null )
            g.drawImage( titlebottom, 0, 440, null );
    }
}
blackcompe
  • 3,180
  • 16
  • 27