Package greenfoot

Class GreenfootImage

java.lang.Object
greenfoot.GreenfootImage

public class GreenfootImage extends Object
An image to be shown on screen. The image may be loaded from an image file and/or drawn by using various drawing methods.
  • Constructor Summary

    Constructors
    Constructor
    Description
    GreenfootImage(int width, int height)
    Create an empty (transparent) image with the specified size.
    Create a GreenfootImage from another GreenfootImage.
    Create an image from an image file.
    GreenfootImage(String string, int size, Color foreground, Color background)
    Creates an image with the given string drawn as text using the given font size, with the given foreground Color on the given background Color.
    GreenfootImage(String string, int size, Color foreground, Color background, Color outline)
    Creates an image with the given string drawn as text using the given font size, with the given foreground color on the given background color.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the image.
    void
    drawImage(GreenfootImage image, int x, int y)
    Draws the given Image onto this image
    void
    drawLine(int x1, int y1, int x2, int y2)
    Draw a line, using the current drawing color, between the points (x1, y1) and (x2, y2).
    void
    drawOval(int x, int y, int width, int height)
    Draw an oval bounded by the specified rectangle with the current drawing color.
    void
    drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
    Draws a closed polygon defined by arrays of x and y coordinates.
    void
    drawRect(int x, int y, int width, int height)
    Draw the outline of the specified rectangle.
    void
    Draw a shape directly on the image.
    void
    drawString(String string, int x, int y)
    Draw the text given by the specified string, using the current font and color.
    void
    Fill the entire image with the current drawing color.
    void
    fillOval(int x, int y, int width, int height)
    Fill an oval bounded by the specified rectangle with the current drawing color.
    void
    fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
    Fill a closed polygon defined by arrays of x and y coordinates.
    void
    fillRect(int x, int y, int width, int height)
    Fill the specified rectangle.
    Returns the java.awt.image.BufferedImage that backs this GreenfootImage.
    Return the current drawing color.
    getColorAt(int x, int y)
    Return the color at the given pixel.
    Get the current font.
    int
    Return the height of the image.
    int
    Return the current transparency of the image.
    int
    Return the width of the image.
    void
    Mirrors the image horizontally (the left of the image becomes the right, and vice versa).
    void
    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
    setColor(Color color)
    Set the current drawing color.
    void
    setColorAt(int x, int y, Color color)
    Sets the given pixel to the given color.
    void
    Set the current font.
    void
    Set the transparency of the image.
    Return a text representation of the image.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • GreenfootImage

      public GreenfootImage(String filename) throws IllegalArgumentException
      Create an image from an image file. Supported file formats are JPEG, GIF and PNG.

      The file name may be an absolute path, or a base name for a file located in the project directory.

      Parameters:
      filename - Typically the name of a file in the images directory within the project directory.
      Throws:
      IllegalArgumentException - If the image can not be loaded.
    • GreenfootImage

      public GreenfootImage(int width, int height)
      Create an empty (transparent) image with the specified size.
      Parameters:
      width - The width of the image in pixels.
      height - The height of the image in pixels.
    • GreenfootImage

      public GreenfootImage(GreenfootImage image) throws IllegalArgumentException
      Create a GreenfootImage from another GreenfootImage.
      Parameters:
      image - The source image to be copied.
      Throws:
      IllegalArgumentException
    • GreenfootImage

      public GreenfootImage(String string, int size, Color foreground, Color background)
      Creates an image with the given string drawn as text using the given font size, with the given foreground Color on the given background Color. If the string has newline characters, it is split into multiple lines which are drawn horizontally-centred.
      Parameters:
      string - the string to be drawn
      size - the requested height in pixels of each line of text (the actual height may be different by a pixel or so)
      foreground - the color of the text. Since Greenfoot 3.0.4, passing null will use black.
      background - the color of the image behind the text. Since Greenfoot 3.0.4, passing null with leave the background transparent.
      Since:
      2.0.1
    • GreenfootImage

      public GreenfootImage(String string, int size, Color foreground, Color background, Color outline)
      Creates an image with the given string drawn as text using the given font size, with the given foreground color on the given background color. If the string has newline characters, it is split into multiple lines which are drawn horizontally-centred.
      Parameters:
      string - the string to be drawn
      size - the requested height in pixels of each line of text (the actual height may be different by a pixel or so)
      foreground - the color of the text. Since Greenfoot 3.0.4, passing null will use black.
      background - the color of the image behind the text. Since Greenfoot 3.0.4, passing null with leave the background transparent.
      outline - the Color of the outline that will be drawn around the text. Passing null will draw no outline.
      Since:
      3.0.4
  • Method Details

    • getAwtImage

      public BufferedImage getAwtImage()
      Returns the java.awt.image.BufferedImage that backs this GreenfootImage. Any changes to the returned image will be reflected in the GreenfootImage.
      Returns:
      The java.awt.image.BufferedImage backing this GreenfootImage
      Since:
      Greenfoot version 1.0.2
    • getWidth

      public int getWidth()
      Return the width of the image.
      Returns:
      Width of the image.
    • getHeight

      public int getHeight()
      Return the height of the image.
      Returns:
      Height of the image.
    • rotate

      public void rotate(int degrees)
      Rotates this image around the center.
      Parameters:
      degrees - The number of degrees the object will rotate for.
    • scale

      public void scale(int width, int height)
      Scales this image to a new size.
      Parameters:
      width - Width of new image
      height - Height of new image
    • mirrorVertically

      public void mirrorVertically()
      Mirrors the image vertically (the top of the image becomes the bottom, and vice versa).
    • mirrorHorizontally

      public void mirrorHorizontally()
      Mirrors the image horizontally (the left of the image becomes the right, and vice versa).
    • fill

      public void fill()
      Fill the entire image with the current drawing color.
    • drawImage

      public void drawImage(GreenfootImage image, int x, int y)
      Draws the given Image onto this image
      Parameters:
      image - The image to draw onto this one.
      x - x-coordinate for drawing the image.
      y - y-coordinate for drawing the image.
    • setFont

      public void setFont(Font f)
      Set the current font. This font will be used for subsequent text operations.
      Parameters:
      f - The new Font to be used.
    • getFont

      public Font getFont()
      Get the current font.
      Returns:
      The current used font, if none, set it as the Graphics font, then return it.
    • setColor

      public void setColor(Color color)
      Set the current drawing color. This color will be used for subsequent drawing operations.
      Parameters:
      color - The color to be used.
    • getColor

      public Color getColor()
      Return the current drawing color.
      Returns:
      The current color.
    • getColorAt

      public Color getColorAt(int x, int y)
      Return the color at the given pixel.
      Parameters:
      x - The horizontal coordinate of the pixel.
      y - The vertical coordinate of the pixel.
      Returns:
      The Color at the specific pixel.
      Throws:
      IndexOutOfBoundsException - If the pixel location is not within the image bounds.
    • setColorAt

      public void setColorAt(int x, int y, Color color)
      Sets the given pixel to the given color.
      Parameters:
      x - The horizontal coordinate of the pixel.
      y - The vertical coordinate of the pixel.
      color - The Color to be assigned at the specific pixel.
    • setTransparency

      public void setTransparency(int t)
      Set the transparency of the image.
      Parameters:
      t - A value in the range 0 to 255. 0 is completely transparent (invisible) and 255 is completely opaque (the default).
    • getTransparency

      public int getTransparency()
      Return the current transparency of the image.
      Returns:
      A value in the range 0 to 255. 0 is completely transparent (invisible) and 255 is completely opaque (the default).
    • fillRect

      public void fillRect(int x, int y, int width, int height)
      Fill the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the current color.
      Parameters:
      x - the x coordinate of the rectangle to be filled.
      y - the y coordinate of the rectangle to be filled.
      width - the width of the rectangle to be filled.
      height - the height of the rectangle to be filled.
    • clear

      public void clear()
      Clears the image.
    • drawRect

      public void drawRect(int x, int y, int width, int height)
      Draw the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the current color.
      Parameters:
      x - the x coordinate of the rectangle to be drawn.
      y - the y coordinate of the rectangle to be drawn.
      width - the width of the rectangle to be drawn.
      height - the height of the rectangle to be drawn.
    • drawString

      public void drawString(String string, int x, int y)
      Draw the text given by the specified string, using the current font and color. The baseline of the leftmost character is at position ( x y ).
      Parameters:
      string - the string to be drawn.
      x - the x coordinate.
      y - the y coordinate.
    • drawShape

      public void drawShape(Shape shape)
      Draw a shape directly on the image. Shapes are specified by the shape interface.
      Parameters:
      shape - the shape to be drawn.
    • fillOval

      public void fillOval(int x, int y, int width, int height)
      Fill an oval bounded by the specified rectangle with the current drawing color.
      Parameters:
      x - the x coordinate of the upper left corner of the oval to be filled.
      y - the y coordinate of the upper left corner of the oval to be filled.
      width - the width of the oval to be filled.
      height - the height of the oval to be filled.
    • drawOval

      public void drawOval(int x, int y, int width, int height)
      Draw an oval bounded by the specified rectangle with the current drawing color.
      Parameters:
      x - the x coordinate of the upper left corner of the oval to be drawn.
      y - the y coordinate of the upper left corner of the oval to be drawn.
      width - the width of the oval to be drawn.
      height - the height of the oval to be drawn.
    • fillPolygon

      public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
      Fill a closed polygon defined by arrays of x and y coordinates.

      This method draws the polygon defined by nPoint line segments, where the first nPoint - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤  i  ≤  nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.

      The area inside the polygon is defined using an even-odd fill rule, also known as the alternating rule.

      Parameters:
      xPoints - an array of x coordinates.
      yPoints - an array of y coordinates.
      nPoints - the total number of points.
    • drawPolygon

      public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
      Draws a closed polygon defined by arrays of x and y coordinates. Each pair of (xy) coordinates defines a point.

      This method draws the polygon defined by nPoint line segments, where the first nPoint - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ i ≤ nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.

      Parameters:
      xPoints - an array of x coordinates.
      yPoints - an array of y coordinates.
      nPoints - the total number of points.
    • drawLine

      public void drawLine(int x1, int y1, int x2, int y2)
      Draw a line, using the current drawing color, between the points (x1, y1) and (x2, y2).
      Parameters:
      x1 - the first point's x coordinate.
      y1 - the first point's y coordinate.
      x2 - the second point's x coordinate.
      y2 - the second point's y coordinate.
    • toString

      public String toString()
      Return a text representation of the image.
      Overrides:
      toString in class Object