我试图根据Boolean
是否为true
来绘制多个图像。但是,当 boolean
变为 true
或不变为 true
时,我会收到涉及我的方法的任何内容的 NullPointerException
。 (代码中的注释专门指出了位置),我 99% 确定它是因为图形而它是 null
。
基本上我也在问如何解决这个问题以及如何在一个类中使用我自己的方法正确绘制图像。我知道如何对一个类中的每个图像执行此操作,但我的图像不仅仅是 2 个图像,我想我几乎有 100 个图像,所以我不想创建 100 个类 (:|)。这是我的代码:
图像类列表:
public class Images{
public static Toolkit tk = Toolkit.getDefaultToolkit();
public static final Image Image1 = tk.getImage("src/images/image1.png"), Image2 = tk.getImage("src/images/image2.png");
public ImageObserver observer = null; //i just did this for no reason
public static Graphics g = Main.graphics;
public void paintImage1(Graphics g){
Images.g = g;
g.drawImage(Image1, 10, 10, observer); //NullPointerException points here, even if I replace 'pbserver' with null
}
public void paintImage2(Graphics g){
Images.g = g;
g.drawImage(Image2, 10, 10, observer); //strangely, it doesn't point here
}
}
然后,我在我的类中引用并使用它,用我的 boolean 值绘制图像,如下所示:
public class PaintHandler{
public static Graphics graphics = Images.g;
public void PaintImages(boolean upheld){
if (upheld){
Images.paintImage1(graphics);//NullPionterException points here
}
else if (!upheld){
Images.paintImage2(graphics);//doesn't point here for some reason
}
}
}
该异常还指向我用来使 upheld
true
与否的键绑定(bind)方法,这是另一件奇怪的事情。
像以前一样,我不想为每张图像创建一个类,我更希望它们都在一个类中。此外,当我尝试在我正在使用的 JFrame 上使用 getGraphics() 时,我会在使事物静态化和无法应用静态术语之间经历这个巨大的循环到非静态上下文,例如将变量设为静态,但随后它说不能在静态上下文中使用,但是当您不将其设为静态时,另一个变量会说将其更改回静态,您不会使其静态,然后您会经历使其静态和更改为非静态之间的巨大循环(抱歉解释太长,我试图具体化)。
最佳答案
“当我尝试在 JFrame
上使用 getGraphics()
时,我正在使用” - 不要 ,这不是 Swing 中自定义绘画的方式。 getGraphics
最多会返回上一个绘制周期的快照,并且绘制到其中的任何内容都将在下一个绘制周期上绘制,最坏的情况下,将返回 null
.
看看Painting in AWT and Swing和 Performing Custom Painting有关 Swing 中绘画如何工作以及应如何使用它的详细信息...
相反,您可以为自己创建一个自定义组件,从 JPanel
等扩展而来,并使用它的 paintComponent
来绘制图像。
通常,存储在 src
中的内容将作为嵌入式资源提供给您的应用程序(这取决于您的 IDE 和构建系统),但一般来说,您不应该使用包含 src
的路径,这应该是潜在问题的立即读取标志。
在这种情况下,您应该使用 Class#getResource
加载资源...
public class Images{
public static final Image Image1 = tk.getImage(Images.class.getResource("/images/image1.png"));
例如。
我不鼓励您使用Toolkit.getImage
,因为它将使用后台线程来加载图像,并且如果图像未加载则不会抛出任何类型的异常
而不是考虑使用ImageIO.read
,请参阅 Reading/Loading an Image了解更多详情
Like before, I don't want to make a class for every single image, I would prefer if they were all in one class. Additionally, when I try to use
getGraphics()
on theJFrame
I'm using, I do through this GIGANTIC loop between making thing statics and not being able to apply static terms to non-static context, such as making a variable static, but then it says can't be used in static context, but then when you don't make it static a different variable says change it back to static, you don't make that static, and you go through this huge loop between making it static and changing to not be static (sorry for long explanation, was trying to be specific).
您的图像可以存储在您喜欢的任何地方,也许存储在某种Map
中。但它们必须在有效的上下文中绘制,即组件的绘制方法。您可以在单个组件中绘制任意数量的图像,也可以根据您的喜好将它们展开,就这样...
静态问题可能是因为你......
- 仍在
main
方法中,该方法是静态的和/或 - 尝试引用您尚未为其创建实例的其他类中的(非静态)变量。
只有可运行的示例才会突出显示哪个(如果不是两者)
关于java - 当我使用自己的多种方法来绘制图像时出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623253/