top of page

There are several GreenfootImage constructors available to create images with.  The one that creates an image with text content is the one with the following signature:

 

GreenfootImage(java.lang.String, int, java.awt.Color, java.awt.Color)


Refer to the GreenfootImage API documentation for information on its use.  Within these pages, 'null' will be used for the color values.  For new GreenfootImage objects, the default colors produce an image of black text on a transparent background.  The font size, or 'int' value, will be represented by 'size'.  The String value can be any expression that produces a string.  The following are some examples of valid expressions:

​

    "Level 1" // (a string literal)
    text // String text
    ""+score // int score
    "Health: "+health // int health
    running ? "Running" : "Paused" // boolean running
    phaseName[phase] // String[] phaseName, int phase
    "GAME\nOVER" // (a string literal creating multiple lines of text)


In the last example, notice the use of the escape sequence '\n' within the String value.  This starts a new line of text within the image.  The image created will always have a width as wide as the longest line of text and its height will always be the number of lines multiplied by the height of one line; each line of text will be centered within the image.  To align each line of text to the left edge of the image, the lines would need to be created individually and drawn onto a blank image large enough to contain all the lines of text (the same size as the image created for all lines centered).

CREATING AN IMAGE WITH TEXT

bottom of page