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

2016/2/12

scale method

Vellyxenya Vellyxenya

2016/2/12

#
Hi, I've been trying to scale an image with the following code:
public void act() 
    {
        counter++;
        if(counter%2 == 0 && counter<240)
        {
            try
            {
                double newWidth = image.getWidth()*0.99;
                double newHeight = image.getHeight()*0.99;
                image.scale((int)(newWidth), (int)(newHeight));
                setImage(image);
            }catch(IllegalArgumentException e){getWorld().removeObject(this);}
        }
        else
        {
            getImage().setTransparency(getImage().getTransparency()-4);
            if(getImage().getTransparency()<20)
            {
                getWorld().removeObject(this);
            }
        }
    }
The problem is that it doesn't work properly with images that contain an alpha canal (some parts are deleted and the others don't get scaled) which gives a very bad quality of animation. Any idea how to fix it please?
danpost danpost

2016/2/12

#
I do not think there is a need for the 'try-catch' in this code. Just asking if the image is too small should be enough. Your problem is not the image and what it contains. The problem is that you are scaling the same image multiple times. What you need to do is keep a reference to the original image and every other act cycle, create a new image from it and scale that new image. Holing a scaling factor may help as well:
// instance field
private GreenfootImage baseImage;
private double imageFactor = 1.0;

// in constructor
baseImage = getImage();

// in act while setting new image
GreenfootImage image = new GreenfootImage(baseImage);
imageFactor *= 0.99;
double width = (double)image.getWidth()*imageFactor;
double height =(double)image.getHeight()*imageFactor;
image.scale((int)width, (int)height);
setImage(image);
if (width < 2 || height < 2)
{
    getWorld().removeObject(this);
}
Vellyxenya Vellyxenya

2016/2/12

#
Ah didn't think about it, thank you very much, it works! :)
You need to login to post a reply.