【问题标题】:Java ImageIO.read(Unknown Source) [duplicate]Java ImageIO.read(未知来源)[重复]
【发布时间】:2018-11-22 10:47:28
【问题描述】:

所以我正在尝试为游戏制作一个简单的图形界面,所以我制作了一个精灵表来配合它。但是在我的类 中,我遇到了这个错误:

线程“main”java.lang.IllegalArgumentException 中的异常:输入 == null!
在 javax.imageio。 ImageIO.read(未知来源) 在 matrix.game.gfx.SpriteSheet.(SpriteSheet.java:18)

这里是

package matrix.game.gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
    public String path;
    public int width;
    public int height;

    public int[] pixels;

    public SpriteSheet(String path) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(image == null) {
            return;
        }

        this.path = path;
        this.width = image.getWidth();
        this.height = image.getHeight();

        pixels = image.getRGB(0,0, width, height, null, 0, width);
        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = (pixels[i] & 0xff)/64;
        }
        for (int i = 0; i < 8; i++) {
            System.out.println(pixels[i]);
        }
    }
}

【问题讨论】:

    标签: java graphics javax.imageio sprite-sheet


    【解决方案1】:

    您正在尝试执行未在您的类中声明的函数:

    image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));

    因为它失败了 try { 并抛出了它无法处理的异常。

    【讨论】:

      【解决方案2】:

      如下代码所示,ImageIO类的read方法在参数输入时会抛出IllegalArgumentException 为空。

      public static BufferedImage read(InputStream input) throws IOException {
          if (input == null) {
              throw new IllegalArgumentException("input == null!");
          }
      
          ImageInputStream stream = createImageInputStream(input);
          BufferedImage bi = read(stream);
          if (bi == null) {
              stream.close();
          }
          return bi;
      }
      

      下面这行试图在matrix.game.gfx包中找到资源。

      SpriteSheet.class.getResourceAsStream(path)
      

      如果您尝试从类包以外的目录访问文件,则应将代码更改如下:

      SpriteSheet.class.getClassLoader().getResourceAsStream(fullPath);
      

      在上面的代码中,类加载器将在类路径的根目录中开始搜索。

      【讨论】:

        【解决方案3】:

        这对我有用。

        希望它也适合你。

        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.IOException;
        
        import javax.imageio.ImageIO;
        
        public class SpriteSheet {
        public String path;
        public int width;
        public int height;
        
        public int[] pixels;
        
        public SpriteSheet(String path) {
            BufferedImage image = null;
        
            //Adding a directory(FILE) object for the path specified
            File dir = new File(path);
        
            try {
                //Reading from the specifies directory
                image = ImageIO.read(dir);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(image == null) {
                return;
            }
        
            this.path = path;
            this.width = image.getWidth();
            this.height = image.getHeight();
        
            pixels = image.getRGB(0,0, width, height, null, 0, width);
            for (int i = 0; i < pixels.length; i++) {
                pixels[i] = (pixels[i] & 0xff)/64;
            }
            for (int i = 0; i < 8; i++) {
                System.out.println(pixels[i]);
            }
        }
        public static void main(String[] args) {
            new SpriteSheet(path/to/file);
        
        
        }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-05-04
          • 1970-01-01
          • 2013-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多