I want to package images in an executable .jar file in Eclipse, but even after storing them and linking to them, they do not seem to show up. I am trying to follow this guide as it seems to be the closest to what I am trying to do.
From my understanding, I need to read each of the 4 image files as a BufferredImage from an InputStream, but then need to change them back to regular Images so they work properly within JavaFX. I have tried many variations, but this one which should seem to work best is giving me a java.lang.reflect.InvocationTargetException, so it does not even work in Eclipse properly. I know there are similar questions but none seem to answer my problem specifically (most deal with ImageIcons and JFrames) or I can't piece them together properly.
public class CardPane extends Pane
{
private static Image[] images;
private static BufferedImage img1;
private static BufferedImage img2;
private static BufferedImage img3;
private static BufferedImage img4;
private static Image image1;
private static Image image2;
private static Image image3;
private static Image image4;
static
{
try {
img1 = ImageIO.read(ResourceLoader.getImage("green-club-100.png"));
img2 = ImageIO.read(ResourceLoader.getImage("pink-heart-100.png"));
img3 = ImageIO.read(ResourceLoader.getImage("black-spade-100.png"));
img4 = ImageIO.read(ResourceLoader.getImage("blue-diamond-100.png"));
}
catch (IOException e) {
e.printStackTrace();
}
image1 = SwingFXUtils.toFXImage(img1,null);
image2 = SwingFXUtils.toFXImage(img2,null);
image3 = SwingFXUtils.toFXImage(img3,null);
image4 = SwingFXUtils.toFXImage(img4,null);
images[0] = image1;
images[1] = image2;
images[2] = image3;
images[3] = image4;
}
}
final public class ResourceLoader {
public static InputStream getImage(String path) {
InputStream input = ResourceLoader.class.getResourceAsStream(path);
if (input == null)
input = ResourceLoader.class.getResourceAsStream("/" + path);
return input;
}
}
Here is what it looks like on the file explorer. I configured the build path to have the res folder marked as the source folder, which I believe is what I am supposed to do.
Is there a quick fix to get rid of the invocation error, or am I going about this whole thing entirely wrong? Sorry if the question is confusing or seems stupid, I'm pretty new to coding and to this site. Any help would be greatly appreciated, and if any more code needs to be seen, I will be glad to share! Thanks!
Edit: I have tried to different ways with the advice in mind, but neither seem to work. They run in Eclipse fine and on my computer, but the images will not show up when the jar is opened on other devices. Here is the first with the resource loader
private static Image[] images = new Image[4];
private static Image image1 = new Image(ResourceLoader.getImage("green-club-100.png"));
private static Image image2 = new Image(ResourceLoader.getImage("pink-heart-100.png"));
private static Image image3 = new Image(ResourceLoader.getImage("black-spade-100.png"));
private static Image image4 = new Image(ResourceLoader.getImage("blue-diamond-100.png"));
static
{
images[0] = image1;
images[1] = image2;
images[2] = image3;
images[3] = image4;
}
And the second without, using URLs as in example 2
public static URL url1 = CardGridPaneTest.class.getResource("green-club-100.png");
public static URL url2 = CardGridPaneTest.class.getResource("pink-heart-100.png");
public static URL url3 = CardGridPaneTest.class.getResource("black-spade-100.png");
public static URL url4 = CardGridPaneTest.class.getResource("blue-diamond-100.png");
public static Image image1 = new Image(url1.toString());
public static Image image2 = new Image(url2.toString());
public static Image image3 = new Image(url3.toString());
public static Image image4 = new Image(url4.toString());
public static Image[] images = new Image[4];
static {
images[0] = image1;
images[1] = image2;
images[2] = image3;
images[3] = image4;
}