(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

  1. it is in the correct file format for your program
  2. it is the correct size for your program
  3. it doesn't have a background or border around the image
  4. 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.  

screenshot properties of png:
flappybird1.png Properties

Dimensions 34 x 24
Width 34 pixels
Height 24 pixels
Bit depth 32

flappybird 1.png
Name flappybird1.png
Item type PNG image
Folder path C:\Users\Gail\Downloads \program...
Date created 11/22/2016 9:19 PM
Date modified 11/22/2016 9:19 PM
Size 3.29 KB
Attributes A
Offline availability
Offline status
Shared with
Remove Properties and Personal Information

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.

 

 

Manipulating Images with Methods
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