图像只会加载有时

时间:2011-11-26 17:56:03

标签: java

我正在创建一个带有图像的应用程序,它工作得很好。但是,出于某种原因,图像已停止加载。该图像位于项目目录的根文件夹中。这是我的代码:

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);
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

图像不能一致加载的原因是因为加载是在线程中完成的,这种线程具有不可预测的行为,更糟糕的是,该线程的启动取决于调用addNotify的时间(我是猜测也是不可预测的)。您可以通过在构造函数中放置loadImages();然后repaint();来解决此问题。摆脱addNotify覆盖,run方法和Runnable修饰符。这只是一个临时解决方案(可能适合您的需求),因为不建议在EDT(事件调度线程)上进行密集工作(加载许多图像),因为它可能导致无响应的GUI。图像加载应该在SwingWorker的实例中或在构造GUI之前完成。

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 );
    }
}