在Eclipse中访问映像路径 [英] Accessing image paths in Eclipse

查看:156
本文介绍了在Eclipse中访问映像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问我的Java代码中的一些图像。调用图像的代码在下面。

  public final ImageIcon whitePiece = new ImageIcon(getClass()。getResource(Images / whitecircle.PNG)); 
public final ImageIcon blackPiece = new ImageIcon(getClass()。getResource(Images / blackcircle.png));
public final ImageIcon tiePiece = new ImageIcon(getClass()。getResource(Images / tiecircle.PNG));
public final ImageIcon boardPic = new ImageIcon(getClass()。getResource(Images / gameboard.PNG));

但是,这会返回以下错误:

 线程main中的异常java.lang.NullPointerException 
在javax.swing.ImageIcon。< init>(未知来源)
在jared.othello。 GameInterface。< init>(GameInterface.java:26)
at jared.othello.Main.main(Main.java:22)

第26行是上述代码的第一行。我几乎是积极的,我的文件目录命名有问题,但我无法弄清楚出了什么问题或如何解决它。我在项目中创建了一个名为Images的源文件,并将图像放在文件夹中,如下所示:





我做错了什么,什么是正确的文件名语法?谢谢!

解决方案

您应该有一个名为图像的文件夹的资源文件夹



示例:





我如何访问这些图标:

  public BufferedImage icon32 = loadBufferedImage(/ icon / icon32.png); 
public BufferedImage icon64 = loadBufferedImage(/ icon / icon64.png);

private BufferedImage loadBufferedImage(String string)
{
try
{
BufferedImage bi = ImageIO.read(this.getClass()。getResource ));
return bi;
} catch(IOException e)
{
e.printStackTrace();
}
返回null;
}


I'm trying to access some images in my Java code. The code to call the images is below.

public final ImageIcon whitePiece = new ImageIcon(getClass().getResource("Images/whitecircle.PNG"));
public final ImageIcon blackPiece = new ImageIcon(getClass().getResource("Images/blackcircle.png"));
public final ImageIcon tiePiece = new ImageIcon(getClass().getResource("Images/tiecircle.PNG"));
public final ImageIcon boardPic = new ImageIcon(getClass().getResource("Images/gameboard.PNG"));

However, this returns the following error:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at jared.othello.GameInterface.<init>(GameInterface.java:26)
    at jared.othello.Main.main(Main.java:22)

Line 26 is the first line of the above code. I am almost positive that something is wrong with my file directory naming, but I can't figure out what's wrong or how to fix it. I've created a source file named 'Images' within my project and placed the images in the folder, like so:

What am I doing wrong, and what is the correct filename syntax? Thanks!

解决方案

You should have a resource folder with the folder named Images in that, then it should work.

Example:

How I access those icons:

public BufferedImage icon32 = loadBufferedImage("/icon/icon32.png");
public BufferedImage icon64 = loadBufferedImage("/icon/icon64.png");

private BufferedImage loadBufferedImage(String string)
{
    try
    {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
        return bi;
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    return null;
}

这篇关于在Eclipse中访问映像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆