0
我正在創建一個應用程序,並在其中繪製圖像,並且它工作得很好。但出於某種原因,圖像已停止加載。該圖像位於我的項目目錄的根文件夾中。這裏是我的代碼:圖像只會有時加載
的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);
}
}
謝謝!
請在此處發佈您的代碼,而不是脫機。 –
好吧,我會這樣做的。 – Cg2916