1

I created a GUI program in Java, and I have a class with a method and switch statement that'll display an image in a new JFrame depending on the button that is clicked. The code in general is clunky and doesn't have any error-handling (I initially did it for an assignment and I'm building on it myself now), but I wanted to know if there's any way to load the images without hard coding their location. Right now, the images are in the root directory so NetBeans can access it without hard coding, but is there any way to have it display the images once I convert it to a .jar file without hard coding the file location, especially since I want to be able to use it on different computers? I've included the method below.

Thanks!

/* method to display images of 3D shapes using switch cases and a new JFrame
    depending on which shape button is selected */
    public void displayOutput(int x) {
        JFrame f = new JFrame();
        f.setBounds(200, 200, 850, 600);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLocationRelativeTo(null);

        ImageIcon icon = new ImageIcon("");
        switch(x) {
            case 1:
                icon = new ImageIcon("sphere.png");
                break;
            case 2:
                icon = new ImageIcon("cube.jpg");
                break;
            case 3:
                icon = new ImageIcon("cone.png");
                break;
            case 4:
                icon = new ImageIcon("cylinder.png");
                break;
            case 5:
                icon = new ImageIcon("torus.png");
                break;
        }

        JLabel image = new JLabel(icon);
        f.add(image);
        f.setVisible(true);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
nbaq
  • 45
  • 6
  • If you want it to be able to load any image at runtime (when you run the exported jar) you need a way to tell it where they are. You could have the user tell it for example. (JTextField for user input). If it always has to load the same images, but not always in the same location, consider using a relative path instead, or embedding the images in the jar itself. – Athamas Feb 20 '20 at 21:49
  • 1
    If the sizes of the images aren't prohibitively large, then put them into the jar and extract them as resources, as has been well-described in many questions on this site. – Hovercraft Full Of Eels Feb 20 '20 at 21:50
  • 2
    All you have to do is 1) Create a new directory (e.g. `images/`) in your project's "src" folder, 2) Copy the images you want to save, 3) At runtime, your code will do `myUrl = getClass().getResource("/images/xyz.png");` to read the file. See: [In java, how do you retrieve images from a jar file?](https://stackoverflow.com/questions/10875892/) or [Java Image Loading from .jar File](https://stackoverflow.com/questions/38175263) – FoggyDay Feb 20 '20 at 22:06

1 Answers1

1

You could embed the images inside the JAR file. Put them inside the source root folder (If it is maven/gradle project, the location would be src/main/resources/images where images folder will be copied to your JAR file on build), then load them like this:

var resourceURL = Thread.currentThread().getContextClassLoader().getResource("/images/sphere.png");
var imageIcon = new ImageIcon(resourceURL);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417