Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像只会在某些时候加载_Java - Fatal编程技术网

Java 图像只会在某些时候加载

Java 图像只会在某些时候加载,java,Java,我正在创建一个应用程序,其中绘制了一个图像,它工作得非常好。但是,由于某些原因,图像已停止加载。该映像位于我的项目目录的根文件夹中。这是我的密码: JFrame: package com.cgp.tetris; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class TetrisFrame extends JFrame { private sta

我正在创建一个应用程序,其中绘制了一个图像,它工作得非常好。但是,由于某些原因,图像已停止加载。该映像位于我的项目目录的根文件夹中。这是我的密码:

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

提前谢谢

图像加载不一致的原因是加载是在线程中完成的,该线程具有不可预测的行为,更糟糕的是,该线程的启动取决于何时调用
addNotify
(我猜这也是不可预测的)。您可以通过放置
loadImages()来修复此问题然后
重新绘制()。去掉
addNotify
覆盖、
run
方法和
Runnable
修饰符。这只是一个暂时的解决方案(可能适合您的需要),因为不建议在EDT(事件调度线程)上进行密集工作(加载许多图像),因为它可能会导致非响应GUI。图像加载应该在GUI的实例中或在构建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 );
    }
}

请将您的代码张贴在此处,而不是场外。