我还是Java的新手,但我相信这不应该发生。我的paintComponent方法中存在某种奇怪的逻辑错误,这会阻止正确绘制图像。通常情况下这不是问题,我可以找到解决方法。但是,问题并不总是存在。一次评论和退出一行有时会绕过它,关闭和重新打开IDE也是如此。 (Eclipse。)可能导致这种情况的原因,以及可以采取哪些措施来避免它?
这是我的方法:
public void paintComponent(Graphics g){
g.drawImage(titlebg, 0, 0, null);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(300, 250, 300, 50);
//g.fillRect(400, 400, 300, 50);
g.drawImage(ccground, 0, 0, null);
}
编辑:取消注释setColor和fillRect方法时,drawImage方法无法正常工作。有时他们不工作,即使只是他们两个。有时它们会这样做。
编辑2:对图像的唯一其他调用位于以下代码中,用于初始化它们。
Toolkit tk = Toolkit.getDefaultToolkit();
public Image retrieveImage(String x){
URL y = this.getClass().getResource(x);
Image img = tk.getImage(y);
return img;
}
参考:
public void loadImages(){
ccground = rl.retrieveImage("\\Chrysaline Caverns ground tile.png");
titlebg = rl.retrieveImage("\\Title Screen.png");
}
与paintComponent方法在同一个类中。
编辑3:Stacktrace。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at gui.Main.createGui(Main.java:32)
at gui.Main$1.run(Main.java:21)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Main.class
public class Main extends JFrame{
static Window w;
JFrame f;
Image ccground;
Main(){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createGui();
}
});
}
public static void main(String[] args){
w = new Window();
}
public void createGui(){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(w);
f.pack();
f.setVisible(true);
f.setBackground(Color.BLACK);
}
}
编辑4:Window.java
public class Window extends JPanel{
Main m;
ResourceLoader rl;
Image ccground;
Image titlebg;
public Window(){
rl = new ResourceLoader();
m = new Main();
loadImages();
}
public Dimension getPreferredSize(){
return new Dimension(800, 592);
}
public void loadImages(){
ccground = rl.retrieveImage("\\Chrysaline Caverns ground tile.png");
titlebg = rl.retrieveImage("\\Title Screen.png");
}
public void paintComponent(Graphics g){
g.drawImage(titlebg, 0, 0, null);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(300, 250, 300, 50);
//g.fillRect(400, 400, 300, 50);
g.drawImage(ccground, 0, 0, null);
}
}
答案 0 :(得分:2)
Toolkit.getDefaultToolkit()。getImage(url)是罪魁祸首。方法获取您尚未加载的图像(某种方式为空虚拟对象)。 由于您没有采取任何措施来确保加载图像,因此有时会加载图像,有时则不然。
在使用之前确保图像完全加载的许多方法中的一种是(我知道的最短的)在使用它之前为从Toolkit获得的每个图像执行此操作:
new ImageIcon(image).getImage();
编辑:
改变你像这样检索图像,它应该工作或清楚地表明找不到图像:
public Image retrieveImage(String x){
URL y = this.getClass().getResource(x);
Image img = tk.getImage(y);
if (img == null)
throw new IllegalArgumentException("image " + x + " not found");
return new ImageIcon(img).getImage();
}