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

2015/2/13

Can I scale an image if it is not the default image for the class?

Kendall Kendall

2015/2/13

#
For example, I have an anvil that falls to the ground in my game. I originally scaled the default image because it was much too large. I have coded so that when the anvil hits the ground, the image changes to a more realistic image. However, this image is too small. I was wondering how to scale it since there is no constructor for it?
danpost danpost

2015/2/13

#
Please post the code for your Anvil class. Use the 'code' link below the reply box to insert code into your posts. And, by the way, all objects in java have a constructor. All classes may not, however. Those that do not will use the constructor of the class it extends. Besides, you can always add one to any that does not (overriding the one in the class it extends).
danpost danpost

2015/2/13

#
To answer the question in the title of the discussion, yes. I will use "anvil.png" as the filename of the image.
GreenfootImage image = new GreenfootImage("anvil.png");
int percentSize = 50; // to make half size (50% of original)
image.scale(image.getWidth()*percentSize/100, image.getHeight()*percentSize/100);
setImage(image);
Of course, the '50' that was used to reduce the image to half width and half height could be changed to any positive value.
You need to login to post a reply.