Search code examples
javaimageloadfade

Make an image "load" or "show" in a fade transition in Java


I'm basically looking to make an image to be revealed as time passes, (i've got the settings for time etc) I'm just not sure how to set it to show the image pixel by pixel, growing in height. Aka, you start with no displayed image and then as time grows, it reveals more and more of the image, (going up) and finally reveals the image at the end.

It's like a transition in Java.

I've been messing around with it for ages, Googling everywhere for at least 3 hours. Can't get anywhere,

Here's my images as base:

BufferedImage img = null, img2 = null;
try {
    img = ImageIO.read(new File("9278.png"));
    img2 = ImageIO.read(new File("9279.png"));
} catch (IOException e) {
    e.printStacktrace();
}

g.drawImage(img, 0, 0, null);
g.drawImage(img2, 0, 0, null);

Thanks


Solution

  • I'm assuming you want some sort of curtain effect, like when an image loads over a slow internet connection. I would approach it this way:

    You want to use the setClip method of your Graphics2D object. Create a new Rectangle of the size you want the image to be drawn in, and pass that to setClip in graphics. This will cause the drawing of the image to be performed only inside the area specified by the rectangle.