(CSGC) Manipulating Images Lesson
Manipulating Images
Greenfoot provides a library of images to use in your programs. However, the library may not contain a specific image you need and you may need to find or create an image. If you are using an image from the internet, make sure it is free use. Do not use copyrighted images.
Before using an image from the internet in your program, you want to make sure
- it is in the correct file format for your program
- it is the correct size for your program
- it doesn't have a background or border around the image
- it isn't copyrighted
File Format
Greenfoot can read images in JPEG, PNG, GIF, BMP and TIFF formats. JPEG and PNG are the two preferred formats with PNG being the best.
There are many free tools to use to convert file formats.
Resizing
Pictures need to be resized to fit in the Greenfoot world. To view the size of Greenfoot images right click on the picture in the image project folder and click on properties. You will notice the flappybird1.png image is 34x24 pixels.
Removing Backgrounds or Border
If the image has a background or border, it will need to be removed so it is transparent. There are several free programs you can use on the internet.
Manipulating Images with Methods
The GreenfootImage class has several methods for manipulating and changing images.
To set an image to another image, first create a GreenfootImage to temporarily store the image using the getImage() method.
GreenfootImage image = getImage(); The current image is now stored in image.
Now we can do any of the following:
- image.mirrorHorizontally();
- image.mirrorVertically();
- image.getTransparency();
- image.setTransparency();
- image.getWidth()
- image.getHeight();
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION