在 Netbeans 中读取图像
我的项目中有一个图像文件。层次结构如下所示:
我正在尝试将 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();
}
}
只需像这样设置项目,使用您想要的任何图像:
I have an image file in my project. The hierarchy looks like this:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
以下示例:
< img src="https://i.sstatic.net/AcgKk.png" alt="在此处输入图像描述">
这是代码...
Try
Here's an example:
And here's the code...
如果我没记错的话,应用程序的根目录是项目目录或源目录。 (不确定到底是哪一个)
如果是项目目录,则
resources/Manling.png
是MineSSCCE/resources/Manling.png
。什么都没有!如果是源目录,则
resources/Manling.png
为MineSSCCE/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
isMineSSCCE/resources/Manling.png
. Nothing is there!If it is the source directory,
resources/Manling.png
isMineSSCCE/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.