(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.
PNG
• Handle transparency very well; individual pixels can be partly or completely transparent
• Compresses well
JPEG
• Compresses well
• Cannot have transparent pixels
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();
View the video below to learn more.
Modifier and Type | Method and Description |
---|---|
void |
clear (), Clears the image. |
int | getHeight() Return the height of the image. |
int | getTransparency () Return the current transparency of the image. |
int | getWidth() Return the width of the image. |
void | mirrorHorizontally () Mirrors the image horizontally (the left of the image becomes the right, and vice versa). |
void | mirror Vertically () Mirrors the image vertically (the top of the image becomes the bottom, and vice versa). |
void | rotate (int degrees) Rotates this image around the center. |
void | scale (int width, int height) Scales this image to a new size. |
void | setTransparency (int t) Set the transparency of the image. |
View the video below to learn more.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION