简体   繁体   English

使用我自己的多重方法绘制图像时出现NullpointerException

[英]NullpointerException when I use my own multiple method to paint an image

I am trying to paint multiple images depending on whether a Boolean is true or not. 我试图根据Boolean是否为true来绘制多个图像。 But instead when the boolean becomes true or not, I get a NullPointerException involving anything with my methods. 但是,无论boolean true ,我都会得到一个NullPointerException ,该NullPointerException涉及我的方法。 (comment in code specifically pointing to where), and I'm 99% sure its because of the graphics and it's null . (代码中的注释专门指向何处),我有99%的把握是因为图形,并且它为null

Basically I'm also asking how to fix this and how to properly paint images using my own methods in one class. 基本上,我还问如何解决此问题以及如何在一个类中使用我自己的方法正确绘制图像。 I know how to do it with each image in one class, but I have WAY more than just 2 images, I think I have almost 100, so i don't want to make 100 classes (:|). 我知道如何在一类中使用每个图像,但是我拥有的不仅仅是2个图像,我想我几乎有100个图像,所以我不想进行100个类(:|)。 Here's my code: 这是我的代码:

List of images class: 图片类别列表:

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
   }
}

Then, I cite and use it in my class that paints the image with my boolean like so: 然后,我在类中引用和使用它,用我的布尔值绘制图像,如下所示:

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
      }
   }
}

The exception also points to the keybindings methods I use to make upheld true or not, another thing that is strange. 异常还指向我用来使upheld与否true的键绑定方法,这很奇怪。

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 the JFrame 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). 此外,当我尝试在正在使用的JFrame上使用getGraphics()时,我会通过GIGANTIC循环在使事物成为静态对象和无法将静态术语应用于非静态上下文(例如使变量变为静态getGraphics()之间进行,但是然后它说不能在静态上下文中使用,但是当您不将其设置为静态时,另一个变量表示将其更改为静态,就不会使其变为静态,并且您会经历在使其变为静态之间的巨大循环静态并且更改为非静态(很抱歉,长期解释,我们试图具体说明)。

" when I try to use getGraphics() on the JFrame im using" - DON'T , this is not how custom is painting is done in Swing. “当我尝试在JFrame im上使用getGraphics()时” JFrame N'T ,这不是在Swing中完成自定义绘制的方式。 getGraphics will, at best, return a snap shot of the last paint cycle and anything painted to it will be painted over on the next paint cycle and at worst, will return null . getGraphics最多将返回上一个绘制周期的快照,并且对其进行绘制的所有内容将在下一个绘制周期中进行绘制,并且在最坏的情况下,将返回null

Take a look at Painting in AWT and Swing and Performing Custom Painting for dore details about how painting works in Swing and how you should work with it... 看看AWT和Swing中的 绘画以及执行自定义绘画 ,了解有关绘画如何在Swing中工作以及应该如何使用的详细信息...

Instead, create your self a custom component, extending from something like JPanel and use it's paintComponent to paint your images. 取而代之的是,为自己创建一个自定义组件,该组件从诸如JPanel之类的东西扩展而来,并使用它的paintComponent来绘制图像。

Normally, content stored in the src will be made available your application as embedded resources (this will depend on your IDE and build system), but generally speaking, you should not access any resource with the path containing src , this should be an immediate read flag for potential issues. 通常,存储在src中的内容将作为嵌入式资源供您的应用程序使用(这将取决于您的IDE和构建系统),但是通常来说,您不应使用包含src的路径访问任何资源,这应该是立即读取的标记潜在问题。

In this case you should be using Class#getResource to load the resource... 在这种情况下,您应该使用Class#getResource加载资源...

 public class Images{
    public static final Image Image1 = tk.getImage(Images.class.getResource("/images/image1.png"));

for example. 例如。

I would discourage you from using Toolkit.getImage as it will use a background thread to load the image and doesn't throw any kind of exception if the image wasn't loaded 我不鼓励您使用Toolkit.getImage ,因为它将使用后台线程加载图像,并且如果未加载图像也不会引发任何异常

Instead considering using ImageIO.read , see Reading/Loading an Image for more details 代替考虑使用ImageIO.read ,有关更多详细信息,请参见读取/加载图像

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 the JFrame 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). 此外,当我尝试在正在使用的JFrame上使用getGraphics()时,我会通过GIGANTIC循环在使事物成为静态对象和无法将静态术语应用于非静态上下文(例如使变量变为静态getGraphics()之间进行,但是然后它说不能在静态上下文中使用,但是当您不将其设置为静态时,另一个变量表示将其更改为静态,就不会使其变为静态,并且您会经历在使其变为静态之间的巨大循环静态并且更改为非静态(很抱歉,长期解释,我们试图具体说明)。

Your images can be stored wherever you like, maybe in a Map of some kind. 您的图像可以存储在任何您喜欢的地方,也许存储在某种Map中。 But they must be painted within a valid context, ie a component's paint method. 但是它们必须在有效的上下文中进行绘制,即组件的绘制方法。 You can paint as many images you want within in single component or spread them out if your prefer, that's tp up... 您可以在单个组件中绘制任意数量的图像,也可以根据需要将它们分散开来,这就是...

The issue with static is probably because you're ... 静态问题可能是因为您...

  1. Still in the main method, which is static and/or 仍然是main方法,它是静态的和/或
  2. Trying to references (non-static) variables in other classes to which you've not created an instance to. 尝试引用尚未创建实例的其他类中的(非静态)变量。

Only a runnable example will highlight which (if not both) 仅一个可运行的示例将突出显示哪个(如果不是两个)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM