Java 图像读取错误
我正在尝试制作一个图像编写器,它将读取模板图像,然后在其上写入文本,然后用新文本创建一个新图像。由于某种原因,当我的代码看起来很好时,我会出错。代码如下:Java 图像读取错误,java,image,jlabel,Java,Image,Jlabel,我正在尝试制作一个图像编写器,它将读取模板图像,然后在其上写入文本,然后用新文本创建一个新图像。由于某种原因,当我的代码看起来很好时,我会出错。代码如下: public class GUI extends JFrame{ private JPanel p1 = new JPanel();//Puts tiles in, and organizes them for you private JPanel p2 = new JPanel();//Holds trash tiles
public class GUI extends JFrame{
private JPanel p1 = new JPanel();//Puts tiles in, and organizes them for you
private JPanel p2 = new JPanel();//Holds trash tiles
JLabel ll = new JLabel();
//private JPanel p3 = new JPanel();//Holds trash tiles
public GUI(){
this.setTitle("Tile Game");
this.setSize(600,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
try {
createTile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.add(ll);
}
public void createTile() throws IOException{
final BufferedImage image = ImageIO.read(getClass().getResource("/src/tile.png"));
//File outPut = new File("saved.png");
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString("Hello World!", 100, 100);
g.dispose();
ImageIO.write(image, "png", new File("/src/test2.png"));
final BufferedImage image2 = ImageIO.read(new File("/src/test2.png"));
ImageIcon icon1 = new ImageIcon(image2);
ll.setIcon(icon1);
p1.add(ll); }
}
当我运行上述代码时,我最终得到以下错误:
GUI.<init>() line: 36
Start.main(String[]) line: 7
GUI.()行:36
Start.main(字符串[])行:7
我不明白为什么还要求我调试GUI构造函数……这一切对我来说似乎都很好。您的图像位置不合适!(忽略其他类,它只是一个测试项目^) 在eclipse项目中创建一个文件夹,但不在源目录中(!!!),然后参考如下图像:
Image img1 = Toolkit.getDefaultToolkit().getImage("img/index.png");
尝试在路径前面添加“/”并确保文件存在:getClass().getResource(“/src/tile.png”);我这样做了,但仍然有错误…请将错误消息的文本添加到您的问题中。不要将图像存储在源文件夹中!看到这篇旧帖子了吗