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);
}