如何在使用Eclipse生成一个.jar文件后加载图像?

huangapple go评论110阅读模式
英文:

How do I load an image after I generated a .jar file with eclipse?

问题

我刚刚用Java写了一个小程序,可以将图像打印在窗口上。我使用了JPanel来加载图像:

public class Board extends JPanel {

    private Image mummy;

    public Board() {
        initBoard();
    }

    private void initBoard() {
        loadImage();
        int w = mummy.getWidth(this);
        int h = mummy.getHeight(this);
        setPreferredSize(new Dimension(w, h));
    }

    private void loadImage() {
        ImageIcon ii = new ImageIcon("mummy.png");
        mummy = ii.getImage();
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(mummy, 0, 0, null);
    }
}

然后使用JFrame来打印图像。然后,我通过Eclipse的导出部分(Runnable JAR文件)创建了.jar文件。

问题是,现在当我运行.jar文件时无法加载图像。我尝试将图像放在.jar压缩包的各个位置,但代码根本不会加载它。

是否有人知道如何使用这种方法加载图像,或者我应该使用包装器来创建.jar文件,而不是使用Eclipse?

英文:

I just wrote a mini-program in java that takes an image and prints it on frame.
I used JPanel to load the image:

public class Board extends JPanel {

    private Image mummy;

    public Board() {

        initBoard();

    }

    private void initBoard() {
    
        loadImage();
    
        int w = mummy.getWidth(this);
        int h =  mummy.getHeight(this);
        setPreferredSize(new Dimension(w, h));        
    }

    private void loadImage() {
    
        ImageIcon ii = new ImageIcon("mummy.png");
        mummy = ii.getImage();        
    }

    @Override
    public void paintComponent(Graphics g) {

        g.drawImage(mummy, 0, 0, null);
    }
}

and JFrame to print it.
Then I created the .jar file via the export section of Eclipse (Runnable JAR file).

The problem is that now I can't load the image when I run the .jar file. I tried putting the image everywhere on the .jar zip but simply the code doesn't load it.

Everyone knows how to load the images using this method, or should I use a wrapper to create the .jar file instead of eclipse?

答案1

得分: -1

大家,我找到解决方法了:

ImageIcon("<fileName>"),默认的起始目录是代码所在的位置,但在Eclipse项目和Eclipse生成的可运行jar文件之间会有所不同。

  • 当它是一个项目时:它是项目的目录,即src和bin目录所在的位置。
  • 当它是一个.jar文件时:src目录不存在,起始目录现在是.jar文件所在的目录。

举个例子:
为了简单起见,让我们将创建的可运行文件放在~目录下。

使用 new ImageIcon("src/images/mummy.png"),你需要将目录"images"与相关文件放在项目的src目录中,即源文件和包所在的位置,形成以下结构:

...eclipse-workspace/Project/src/cli/*.java -源代码所在位置。

...eclipse-workspace/Project/src/images/mummy.png -文件所在位置。

在使用Eclipse创建了可运行的.jar文件后,你需要在与.jar文件相同的目录中放置一个新的"src"目录,并在其中放置"images"目录,形成以下结构:

~/project.jar (.jar文件)

~/src/images/mummy.png (我在代码中使用new ImageIcon("src/images/mummy.png")加载的图片)

不管怎样,感谢大家的评论。

英文:

Guys I figured it out:

The ImageIcon("<fileName>"), has as default starting directory where all the code is put, but varies between an eclipse project and an eclipse generated Runnable jar file.
-When it's a project: it's the directory of the project, so where the src and bin directory are.
-When it's a .jar: src doesn't exist, and the starting directory now is the directory where the .jar file is situated.

For example:
for simplicity let's situate the created runnable files in the ~ directory.

-Using new ImageIcon(&quot;src/images/mummy.png&quot;), you'll have to put the directory "images" with the relative files in the src directory of the project, where the source files and packages are, making a structure like this:

...eclipse-workspace/Project/src/cli/*.java -where the source code is.

...eclipse-workspace/Project/src/images/mummy.png -where the file is.

After you created the runnable .jar with eclipse, you'll have to put a new "src" directory with the "images" in the same directory where the .jar is, making a structure like this:

~/project.jar (the .jar file)

~/src/images/mummy.png (the image I loaded in the code with new ImageIcon(&quot;src/images/mummy.png&quot;)

Thanks for the comments anyways.

huangapple
  • 本文由 发表于 2020年10月9日 17:52:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64277702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定