#java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0. (Real message)

16 messages · Page 1 of 1 (latest)

errant robin
#

Hi people! I suspect that my merge method is causing the error. What should I do to avoid it?

public static ImageIcon merge(ArrayList<ImageIcon> images, ArrayList<Float> transparency)
{
BufferedImage dest = null;
Graphics2D destG = null;
int rule; // This is SRC for the top image, and DST_OVER for the other ones
float alpha;

    for (int i = 0, size = images.size(); i < size; i++)
    {
        Image image = images.get(i).getImage();

        rule = AlphaComposite.SRC_OVER; // Default value
        alpha = transparency.get(i);

        if (i == 0)
        {
            dest = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
            destG = dest.createGraphics();

            rule = AlphaComposite.SRC; // Rule for 1st image
        }

        dest = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        destG.setComposite(AlphaComposite.getInstance(rule, alpha));
        destG.drawImage(image, 0, 0, null);
    }

    return new ImageIcon(dest);
}
rancid creekBOT
#

This post has been reserved for your question.

Hey @errant robin! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

balmy rain
#

Image.getWidth()/getHeight() usually returns -1 when the image isn't fully loaded yet. Loading images is an asynchronous operation, done outside of the GUI thread.
You'd need to wait for your images to have finished loaded before you start that kind of operations, but how to wait for that depends on your general design.

errant robin
#

This is an example of a sprite initialized: private static final ImageIcon background = new ImageIcon(new ImageIcon("resources/sprites/background.png").getImage().getScaledInstance(TOKEN_INITIAL_SIZE, TOKEN_INITIAL_SIZE, Image.SCALE_FAST));

balmy rain
#

Yes, but what matters is everything

errant robin
#

ahahah your right. Do you want the Github repo to take a look?

#

and does having the png files on the computer significant? Because I have them in the resource file in the project.

balmy rain
#

Fundamentally you shouldn't be trying to advance on GUIs if you don't know what to do when you're told "you need to wait until your resources are loaded from outside the GUI thread before you proceed"

balmy rain
balmy rain
#

One frowned upon way to solve the issue is to load the images as BufferedImages with ImageIO and giving that to the ImageIcons. Loading the images will be blocking and freezing the GUI but it will make do of the issue.

errant robin
#

Okkkk I think I get it now. Because BufferedImages will always be accepted in ImageIO

errant robin
balmy rain
errant robin
#

ok no problem, thk for the help though!