This site requires JavaScript, please enable it in your browser!
Greenfoot back
GingaSnap wrote ...


Dematerializing image

GingaSnap GingaSnap


Hello, i'm trying to experiment with image manipulation and I'm trying to get the effect of an image dematerializing by randomly setting each individual pixel transparent until the entire image is transparent. It works but it takes way too long for the entire image to become completely transparent as it only checks one pixel at a time. Here is the code I have been using. Are there any recommendations on what I should change/add?
 public void dematerialize()
        int w = getImage().getWidth();
        int h = getImage().getHeight();
        int randW= Greenfoot.getRandomNumber(w);
        int randH=Greenfoot.getRandomNumber(h);
        Color transparent = new Color(255,255,255,0);

            getImage().setColorAt(randW, randH, transparent);

danpost danpost


You could use a for loop to remove multiple pixels at a time. However, the two big problems would be (1) as pixels get removed, the chance lessens in hitting one that needs removed; and (2) determining when ALL pixels have been removed would not be an easy matter. Fading might be a favorable alternative -- gradually diminishing the transparency of the entire image. Or, maybe a combination of the two -- that way, at least the time for total removal is limited to a specific amount.
You need to login to post a reply.