top of page

The variable 'image' within this page is used as a reference to the GreenfootImage object that visually represents the value.  There are two main ways of displaying an image in greenfoot:

​

    (1) the image can be drawn onto the background image of a World object, 'world', with:

​

world.getBackground().drawImage(image, 10, 10);


Refer to the GreenfootImage API documentation for information on the 'drawImage' method.  The 'image' will be drawn with its top-left corner at the point (10, 10) in the background image of 'world'.  This change in the background image is permanent and must be removed or replaced before drawing a new text image at that location.  This would involve re-drawing or replacing all or the altered portion of the original background image before drawing the new text image.  The drawn 'image' will always be maintained behind any actors in the world.

​

    (2) the image can be set as the image of an active Actor object, 'actor', with:

​

actor.setImage(image);



Refer to the Actor API documentation for information on the 'setImage' method.  There are two methods with that name; refer to the one that uses a GreenfootImage object for its parameter.  The placement of the image with respect to other actors in the world that may intersect it is determined by the currently set paint order.  There is actually another way to display text.  The 'showText' method of the World class can be used.  This method is used to do the following:

​

     (a) add a text object into the world;

​

     (b) change the displayed text of the object; and

​

     (c) remove the text object from the world.

 

Unfortunately, you have no control over any aspect of the image (text style, font size, colors, transparency) nor where in the paint order the text is displayed (always on top of any actors in the world).

DISPLAYING AN IMAGE IN GREENFOOT

bottom of page