0

(Link to the Q/A mentioned: Merging two images) (sorry if any of this sounds a tad weird, it was originally posted under "Answer" as I was unsure of how to handle a question to an answer when I was unable to comment, its fixed now though)

I am using the 1st answer/suggestion that includes the sample bit of code and has over 100 votes/likes/whatever it is on stackoverflow, and everything that references ImageIO throws an IOException. I am quite honestly fairly new to coding, I've been doing it for... 7 years I believe with the first 4 being something called scratch which is a simple block programming language similar to lego mindstorms except it makes a digital program rather than robotic one. Anyway, as of 3 years ago I finally got deeper into java with modding minecraft. I still haven't gotten too far into java :( and am not entirely sure what exceptions are despite trying to look them up.

public void registerIcons(IconRegister iconRegister)
{
    File path = new File("mymod:"); // base path of the images

    // load source images
    BufferedImage core = ImageIO.read(new File(path, "cores/CoreOak.png"));
    BufferedImage cap = ImageIO.read(new File(path, "caps/CapGold.png"));
    BufferedImage gem = ImageIO.read(new File(path, "overlay2.png"));

    // create the new image, canvas size is the max. of both image sizes
    int w = Math.max(core.getWidth(), cap.getWidth());
    w = Math.max(w, gem.getWidth());
    int h = Math.max(core.getHeight(), cap.getHeight());
    h = Math.max(h, gem.getHeight());
    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    // paint both images, preserving the alpha channels
    Graphics g = combined.getGraphics();
    g.drawImage(core, 0, 0, null);
    g.drawImage(cap, 0, 0, null);
    g.drawImage(gem, 0, 0, null);

    // Save as new image
    ImageIO.write(combined, "PNG", new File(path, "wand/combined.png"));

    this.itemIcon = iconRegister.registerIcon(texturePath + "combined");
} 

There's the code. The exceptions are under the bufferedImage things, and the ImageIO.write method, they specifically say "Unhandled exception type IOException" I'm not sure if it being minecraft matters, but when I asked in the sense of java in general as this guy did people yelled at me for having it be too broad, so I thought I'd include it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Elijah S
  • 7
  • 6

1 Answers1

0

So, fundamental problem here.

File path = new File("mymod:"); // base path of the images

"mymod:overlay2.png" is not a file path, it's a Resource Location. That is, it's a magic string that Minecraft internals use to figure out how to reference a file that may be in one of several possible locations: a jar file, or one of any number of resource pack zip files. It is not a File path.

If you want to read data from files inside a jar file, you cannot do it with standard File IO, you need to use getResourceAsStream() as explained in this answer.