Class Image

java.lang.Object
org.openpatch.scratch.internal.Image

public class Image extends Object
The base class for representing scratch costumes and backdrops.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Image(String name, String imagePath)
    Construct a ScratchImage object by a name and a path to an image.
    Image(String name, String spriteSheetPath, int x, int y, int width, int height)
    Construct a ScratchImage object by a name and a path to a sprite sheet.
    Copies a ScratchImage object
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    changeTint(double h)
    Changes the tint by adding a hue value to the current hue value.
    void
    changeTransparency(double step)
    Changes the transparency by adding a step value to the current transparency.
    void
    Disables the nine-slice scaling for the image.
    void
    draw(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style, Shader shader)
    Draw the scaled image at a given position.
    void
    draw(processing.core.PGraphics buffer, float size, float degrees, float x, float y, Shader shader)
    Draw the scaled image at a given position.
    void
    drawAsBackground(processing.core.PGraphics buffer)
    Draw the image as a background.
    void
    drawDebug(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style)
    Draw the scaled image at a given position.
    int[]
    Returns the smallest rectangle that holds every pixel which is not fully transparent, in coordinates of the image before it was resized.
    int
    Returns the height
    Returns the name
    int
    Returns the height of the image before it was resized.
    int
    Returns the width of the image before it was resized.
    Returns the tint
    double
    Returns the transparency
    int
    Returns the width
    static processing.core.PImage
    Loads an image from a given path and returns it.
    static processing.core.PImage
    loadImage(String path, int x, int y, int width, int height)
    Loads an image from a given path and returns a tile of the image.
    static processing.core.PImage
    loadImageOrBuiltin(String pathOrBuiltin)
    Loads a picture from either the name of a sprite bundled with Scratch for Java or a path to an image file.
    static Image
    ofNameOrPath(String name, String pathOrBuiltin)
    Creates an image from either the name of a sprite bundled with Scratch for Java or a path to an image file.
    void
    setHeight(int height)
    Sets the height
    void
    Sets the name
    void
    setNineSlice(int top, int right, int bottom, int left)
    Sets the nine-slice scaling parameters for the image.
    void
    setSize(double percentage)
    Sets the size of the image to the specified percentage of the original size.
    void
    setSize(int width, int height)
    Sets the size of the image to the specified width and height.
    void
    setTint(double h)
    Sets the tint with hue
    void
    setTint(double r, double g, double b)
    Sets the tint with rgb
    void
    setTransparency(double transparency)
    Sets the transparency, as Scratch's ghost effect: 0 shows the image and 100 hides it.
    void
    setWidth(int width)
    Sets the width
    static List<Image>
    tilesOf(String prefix, String pathOrBuiltin, int tileWidth, int tileHeight)
    Cuts a sheet into equally sized tiles, named prefix0, prefix1 and so on.

    Methods inherited from class java.lang.Object

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

    • Image

      public Image(String name, String imagePath)
      Construct a ScratchImage object by a name and a path to an image.
      Parameters:
      name - a a name
      imagePath - a path to an image
    • Image

      public Image(String name, String spriteSheetPath, int x, int y, int width, int height)
      Construct a ScratchImage object by a name and a path to a sprite sheet.
      Parameters:
      name - a name
      spriteSheetPath - a path to a sprite sheet
      x - the x coordinate of the tile
      y - the y coordinate of the tile
      width - the width of the tile
      height - the height of the tile
    • Image

      public Image(Image i)
      Copies a ScratchImage object
      Parameters:
      i - the ScratchImage object to copy
  • Method Details

    • ofNameOrPath

      public static Image ofNameOrPath(String name, String pathOrBuiltin)
      Creates an image from either the name of a sprite bundled with Scratch for Java or a path to an image file.

      A string without a file extension is looked up in BuiltinAssets, everything else is treated as a path, so existing code keeps working.

      Parameters:
      name - the name the image should have
      pathOrBuiltin - a bundled sprite name or a path to an image
      Returns:
      the image
    • loadImageOrBuiltin

      public static processing.core.PImage loadImageOrBuiltin(String pathOrBuiltin)
      Loads a picture from either the name of a sprite bundled with Scratch for Java or a path to an image file.

      This is ofNameOrPath(java.lang.String, java.lang.String) for the places that want the picture itself rather than a costume: the mouse cursor, the loading screen, and the sheet addCostumes cuts up. Without it a built-in name worked when naming a costume and nowhere else.

      Parameters:
      pathOrBuiltin - a bundled sprite name or a path to an image
      Returns:
      the picture
    • tilesOf

      public static List<Image> tilesOf(String prefix, String pathOrBuiltin, int tileWidth, int tileHeight)
      Cuts a sheet into equally sized tiles, named prefix0, prefix1 and so on.

      A built-in name is a region of a shared sheet rather than a file of its own, so the tiles are cut from inside that region instead of from the top left corner of the file it happens to live in.

      Parameters:
      prefix - the name every tile starts with
      pathOrBuiltin - a bundled sprite name or a path to an image
      tileWidth - the width of a single tile
      tileHeight - the height of a single tile
      Returns:
      the tiles, in the order the sheet holds them
    • loadImage

      public static processing.core.PImage loadImage(String path)
      Loads an image from a given path and returns it.
      Parameters:
      path - the path to the image
      Returns:
      the image
    • loadImage

      public static processing.core.PImage loadImage(String path, int x, int y, int width, int height)
      Loads an image from a given path and returns a tile of the image.
      Parameters:
      path - the path to the image
      x - the x coordinate of the tile
      y - the y coordinate of the tile
      width - the width of the tile
      height - the height of the tile
      Returns:
      a tile of the image
    • getOriginalWidth

      public int getOriginalWidth()
      Returns the width of the image before it was resized.
      Returns:
      the width in pixels
    • getOriginalHeight

      public int getOriginalHeight()
      Returns the height of the image before it was resized.
      Returns:
      the height in pixels
    • getContentBounds

      public int[] getContentBounds()
      Returns the smallest rectangle that holds every pixel which is not fully transparent, in coordinates of the image before it was resized.

      Costumes usually have transparent space around them - a walking pose drawn into a costume tall enough for a jumping one, say. Measuring that space makes a sprite collide with what a player can actually see instead of with its whole costume.

      Returns:
      x, y, width and height of the painted area; the whole image if it is fully transparent
    • getName

      public String getName()
      Returns the name
      Returns:
      the name
    • setName

      public void setName(String name)
      Sets the name
      Parameters:
      name - unique name
    • getWidth

      public int getWidth()
      Returns the width
      Returns:
      the width
    • setWidth

      public void setWidth(int width)
      Sets the width
      Parameters:
      width - a width value
    • getHeight

      public int getHeight()
      Returns the height
      Returns:
      the height
    • setHeight

      public void setHeight(int height)
      Sets the height
      Parameters:
      height - a height value
    • setTint

      public void setTint(double r, double g, double b)
      Sets the tint with rgb
      Parameters:
      r - a red value [0...255]
      g - a green value [0...255]
      b - a blue value [0...255]
    • setTint

      public void setTint(double h)
      Sets the tint with hue
      Parameters:
      h - a hue value [0...255]
    • changeTint

      public void changeTint(double h)
      Changes the tint by adding a hue value to the current hue value.
      Parameters:
      h - a hue value [0...255]
    • getTint

      public Color getTint()
      Returns the tint
      Returns:
      the tint
    • setTransparency

      public void setTransparency(double transparency)
      Sets the transparency, as Scratch's ghost effect: 0 shows the image and 100 hides it. Values outside that are pinned to it, the way Scratch pins them.
      Parameters:
      transparency - [0...100]
    • changeTransparency

      public void changeTransparency(double step)
      Changes the transparency by adding a step value to the current transparency.
      Parameters:
      step - a step value
    • getTransparency

      public double getTransparency()
      Returns the transparency
      Returns:
      the transparency [0...100]
    • setSize

      public void setSize(double percentage)
      Sets the size of the image to the specified percentage of the original size.
      Parameters:
      percentage - The desired size as a percentage of the original size
    • setSize

      public void setSize(int width, int height)
      Sets the size of the image to the specified width and height. If a resized version with the requested dimensions already exists in cache, it will use that version. Otherwise, it creates a new resized copy from the original image and caches it for future use.
      Parameters:
      width - The desired width of the image in pixels
      height - The desired height of the image in pixels
    • setNineSlice

      public void setNineSlice(int top, int right, int bottom, int left)
      Sets the nine-slice scaling parameters for the image. This allows the image to be scaled while preserving the corners and edges.
      Parameters:
      top - the top slice height
      right - the right slice width
      bottom - the bottom slice height
      left - the left slice width
    • disableNineSlice

      public void disableNineSlice()
      Disables the nine-slice scaling for the image. This will make the image scale normally without preserving corners and edges.
    • draw

      public void draw(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style, Shader shader)
      Draw the scaled image at a given position.
      Parameters:
      buffer - a buffer
      size - a percentage value
      degrees - direction
      x - a x coordinate
      y - a y coordinate
      style - a rotation style
      shader - a shader
    • drawDebug

      public void drawDebug(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style)
      Draw the scaled image at a given position.
      Parameters:
      size - a percentage value
      degrees - direction
      x - a x coordinate
      y - a y coordinate
      style - a rotation style
    • draw

      public void draw(processing.core.PGraphics buffer, float size, float degrees, float x, float y, Shader shader)
      Draw the scaled image at a given position.
      Parameters:
      buffer - a buffer
      size - a percentage value
      degrees - direction
      x - a x coordinate
      y - a y coordinate
      shader - a shader
    • drawAsBackground

      public void drawAsBackground(processing.core.PGraphics buffer)
      Draw the image as a background. The image is automatically scaled to fit the window size.
      Parameters:
      buffer - a buffer