我对 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:堆栈跟踪。
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)
主类
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);
}
}