但我花了几个小时试图运行这段代码,但它只是不工作。首先它应该将图像打印到我的屏幕上。到目前为止,它不想打印我的图像,但它仍然会打印矩形和字符串。我已经尝试了很多东西,我什至不确定它是否正在加载图像。有谁知道我做错了什么?如果有帮助,我正在使用 NetBeans
public class Screen extends JPanel implements Runnable {
public Thread thread = new Thread(this);
private Image Cat;
public Screen(Frame frame) {
thread.start();
}
public void run(){
System.out.println("this is running");
loadPic();
repaint();
try{
Thread.sleep(1);
} catch(Exception e) {}
}
public void loadPic(){
Cat = new ImageIcon("catIcon1.png").getImage();
System.out.println("Image Loaded!");
repaint();
}
public void paint(Graphics g){
g.drawString("sup tho!", 250, 250);
g.fillRect(50,50,50,50);
System.out.println("this works?");
g.drawImage(Cat, 0, 0, null);
System.out.println("Cat should be drawn");
}
}