在 Netbeans 中读取图像

发布于 2024-11-29 01:29:18 字数 1740 浏览 1 评论 0原文

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

Project hierarchy

我正在尝试将 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
}

我总是在 println 语句上收到 NullPointerException ,所以我认为路径是错误的。我尝试将图像移动到项目中的不同位置,并且尝试更改文件路径(例如“mine/resources/Manling.png”和“/resources/Manling.png”)。有什么想法吗?

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

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();
}

}

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

SSCCE

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:

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 NullPointerException on the println statement, 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-12-06 01:29:18

尝试

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

以下示例:

  • 层次结构

在此处输入图像描述

  • 结果

< img src="https://i.sstatic.net/AcgKk.png" alt="在此处输入图像描述">

这是代码...

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

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);
    }
}
暖阳 2024-12-06 01:29:18

如果我没记错的话,应用程序的根目录是项目目录或源目录。 (不确定到底是哪一个)

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

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

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

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.png is MineSSCCE/resources/Manling.png. Nothing is there!

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文