java 在 Netbeans 中读取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7014123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:17:08  来源:igfitidea点击:

Reading an image in Netbeans

javaswingpathbufferedimage

提问by Tharwen

I have an image file in my project. The hierarchy looks like this:

我的项目中有一个图像文件。层次结构如下所示:

Project hierarchy

项目层次

I'm trying to read Manling.png into Manling.java using this code:

我正在尝试使用以下代码将 Manling.png 读入 Manling.java:

public BufferedImage sprite;

public Manling()
{
    try
    {
    File file = new File("resources/Manling.png");
    sprite = ImageIO.read(file);
    } catch (IOException e) {}

    System.out.println(sprite.toString()); //This line is to test if it works
}

I always get a NullPointerExceptionon the printlnstatement, so I assume the path is wrong. I've tried moving the image to different places in the project and I've tried changing the file path (e.g. 'mine/resources/Manling.png' and '/resources/Manling.png'). Any ideas?

我总是NullPointerExceptionprintln声明中得到一个,所以我认为路径是错误的。我尝试将图像移动到项目中的不同位置,并且尝试更改文件路径(例如“mine/resources/Manling.png”和“/resources/Manling.png”)。有任何想法吗?

If you want a full compilable example, try this one:

如果您想要一个完整的可编译示例,请尝试以下示例:

package minesscce;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.URL;

public class Mine extends JFrame
{
private BufferedImage sprite;

public static void main(String args[])
{
    Mine mine = new Mine();
}

public Mine()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(800, 600);
    setExtendedState(Frame.MAXIMIZED_BOTH);
    setBackground(Color.WHITE);

    try
    {
        File file = new File("resources/Manling.png");
        sprite = ImageIO.read(file);
    } catch (IOException e) {}

    System.out.println(sprite.toString());
}

public void paint(Graphics g)
{
    g.translate(getInsets().left, getInsets().top);
    Graphics2D g2d = (Graphics2D)g;

    g2d.drawImage(sprite, 0, 0, this);
    Toolkit.getDefaultToolkit().sync();
    g2d.dispose();
}

}

}

Just set up the project like this, using any image you want:

只需像这样设置项目,使用您想要的任何图像:

SSCCE

上合生

回答by mre

Try

尝试

ImageIO.read(Mine.class.getResource("../minesscce.resources/Manling.png"));


Here's an example:

下面是一个例子:

  • Hierarchy
  • 等级制度

enter image description here

在此处输入图片说明

  • Result
  • 结果

enter image description here

在此处输入图片说明

And here's the code...

这是代码......

public final class ImageResourceDemo {
    private static BufferedImage bi;

    public static void main(String[] args){
        try {
            loadImage();

            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();             
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void loadImage() throws IOException{
        bi = ImageIO.read(
                ImageResourceDemo.class.getResource("../resource/avatar6.jpeg"));
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.WHITE);
        frame.add(new JLabel(new ImageIcon(bi)));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

回答by Mohayemin

If I am not wrong, root directory of your application is the project directory or the source directory. (Not sure exactly which one is)

如果我没记错的话,你的应用程序的根目录是项目目录或源目录。(不知道具体是哪个)

If it is project directory then resources/Manling.pngis MineSSCCE/resources/Manling.png. Nothing is there!

如果是项目目录,则resources/Manling.pngMineSSCCE/resources/Manling.png. 什么都没有!

If it is the source directory, resources/Manling.pngis MineSSCCE/Source/resources/Manling.png. Nothing is there either!

如果是源目录,resources/Manling.png则是MineSSCCE/Source/resources/Manling.png. 也没有什么!

The actual location is MineSSCCE/Source/minesscce/resources/Manling.pngThat is why it was not working.

实际位置是MineSSCCE/Source/minesscce/resources/Manling.png这就是它不起作用的原因。