Packaging a program containing images

一曲冷凌霜 提交于 2020-01-11 10:26:26

问题


I'm having massive issues packaging my java program which contains images into a jar for conversion into and executable file. The images have been used in the background of the program and buttons. Please see the diagram below which shows the program I desire to convert to a jar.

IMAGE

As you see above the program runs OK. I created the same program with no custom background and custom buttons containing no images and I successfully packaged it into a jar and subsequently into an .exe file.

With regards to drawing my background I'm doing this as follows:

public void paintComponent(Graphics g) {
    Image img = new ImageIcon("imgs/Bgnd1.jpg").getImage();
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
    g.drawImage(img, 0, 0, null);
} 

With regards to creating my 4 custom buttons with images, I'm doing the following:

// Prepare rollover images
ImageIcon F1 = new ImageIcon("imgs/btn_f1_not_selected.jpg");
ImageIcon F1rollOver = new ImageIcon("imgs/btn_f1_selected.jpg");

// Create F1 button
final JButton btnF1 = new JButton(F1);
//btnF1.setOpaque(false);
btnF1.setContentAreaFilled(false);
btnF1.setBorder(null);
btnF1.setBorderPainted(false);
btnF1.setFocusPainted(false);
btnF1.setRolloverIcon(F1rollOver);

I attempted placing the images in the bin folder and for the creation of the background I altered the above method with regards to the declaration/fetching of the image.

public void paintComponent(Graphics g) {
        String path = "Bgnd11.jpg";
        java.net.URL imgURL = getClass().getResource(path);     
        Image img = new ImageIcon(imgURL).getImage();
        Dimension size = new Dimension(img.getWidth(observer), img.getHeight(observer));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
        g.drawImage(img, 0, 0, null);
}

I also attempted fetching the images needed for the creation of my buttons as indicated below and then passing them to my button but this did not work.

String path = "Bgnd11.jpg";
java.net.URL imgURL = getClass().getResource(path);     
Image img = new ImageIcon(imgURL).getImage();

How to locate & load the images?


回答1:


In your first attempt, you're loading images from the file system, in the current directory, which is the directory from which the java of javaw command is started. That's what prevents you from bundling the images with your applications. Obviously, the end user of your app won't have the images in his current directory, and his current directory will change depending on how he launches the application.

You should instead have the images packaged inside the jar file, and thus be present in the classpath, and thus load them using the ClassLoader as you're doing in your second attempt.

Let's say they're in the folder /resources/images of the jar, which thus corresponds to the package resources.images.

Using getClass().getResource("Bgnd11.jpg"), as the javadoc indicates, tries to find Bgnd11.jpg in the same package as the class returned by getClass(). So, it would work in our example if the class was in the package resources.images. If it's not, you should use the absolute path of the resource:

URL imgURL = getClass().getResource("/resources/images/Bgnd11.jpg");   

Also, don't mess with the bin folder. This is the destination folder of Eclipse, and doing a clean build will remove everything from this directory. Just add the images to the appropriate package in the source directory, and Eclipse will automatically copy them to the destination directory when building the project.



来源:https://stackoverflow.com/questions/16486858/packaging-a-program-containing-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!