Class Image
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a ScratchImage object by a name and a path to an image.Construct a ScratchImage object by a name and a path to a sprite sheet.Construct a ScratchImage object by a name and a picture that is already loaded.Copies a ScratchImage object -
Method Summary
Modifier and TypeMethodDescriptionvoidchangeTint(double h) Changes the tint by adding a hue value to the current hue value.voidchangeTransparency(double step) Changes the transparency by adding a step value to the current transparency.voidDisables the nine-slice scaling for the image.voiddraw(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style, Shader shader) Draw the scaled image at a given position.voiddraw(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style, Shader shader, double centerX, double centerY) Draw the scaled image so that a given point of it - its rotation center - sits at a given position, turned or mirrored about that point.voidDraw the scaled image at a given position.voiddrawAsBackground(processing.core.PGraphics buffer) Draw the image as a background.voiddrawDebug(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.intReturns the heightgetName()Returns the nameintReturns the height of the image before it was resized.int[]Returns the pixels of the image before it was resized, row by row, as ARGB colours.intReturns the width of the image before it was resized.getTint()Returns the tintdoubleReturns the transparencyintgetWidth()Returns the widthstatic booleanisMirrored(double direction, RotationStyle style) Whether a costume is drawn mirrored: with rotation style LEFT_RIGHT, a sprite that does not face right shows its costume flipped.static processing.core.PImageLoads an image from a given path and returns it.static processing.core.PImageLoads an image from a given path and returns a tile of the image.static processing.core.PImageloadImageOrBuiltin(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 ImageofNameOrPath(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.voidsetHeight(int height) Sets the heightvoidSets the namevoidsetNineSlice(int top, int right, int bottom, int left) Sets the nine-slice scaling parameters for the image.voidsetSize(double percentage) Sets the size of the image to the specified percentage of the original size.voidsetSize(int width, int height) Sets the size of the image to the specified width and height.voidsetTint(double h) Sets the tint with huevoidsetTint(double r, double g, double b) Sets the tint with rgbvoidsetTransparency(double transparency) Sets the transparency, as Scratch's ghost effect: 0 shows the image and 100 hides it.voidsetWidth(int width) Sets the widthCuts a sheet into equally sized tiles, namedprefix0,prefix1and so on.static processing.core.PImageturnToFaceRight(processing.core.PImage image, double direction) Turns a picture that is drawn facingdirectionso that it faces right, the way a sprite faces in Scratch when its direction is 90.
-
Constructor Details
-
Image
-
Image
Construct a ScratchImage object by a name and a path to a sprite sheet.- Parameters:
name- a namespriteSheetPath- a path to a sprite sheetx- the x coordinate of the tiley- the y coordinate of the tilewidth- the width of the tileheight- the height of the tile
-
Image
Construct a ScratchImage object by a name and a picture that is already loaded.- Parameters:
name- a nameimage- the picture
-
Image
-
-
Method Details
-
ofNameOrPath
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 havepathOrBuiltin- a bundled sprite name or a path to an image- Returns:
- the image
-
loadImageOrBuiltin
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(String, String)for the places that want the picture itself rather than a costume: the mouse cursor, the loading screen, and the sheetaddCostumescuts up. Without it a built-in name worked when naming a costume and nowhere else.A built-in is turned to face right, the way its atlas says it is drawn. A file is taken as it is: costumes of your own are expected to face right.
- 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, namedprefix0,prefix1and 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 withpathOrBuiltin- a bundled sprite name or a path to an imagetileWidth- the width of a single tiletileHeight- the height of a single tile- Returns:
- the tiles, in the order the sheet holds them
-
loadImage
Loads an image from a given path and returns it.- Parameters:
path- the path to the image- Returns:
- the image
-
loadImage
Loads an image from a given path and returns a tile of the image.- Parameters:
path- the path to the imagex- the x coordinate of the tiley- the y coordinate of the tilewidth- the width of the tileheight- the height of the tile- Returns:
- a tile of the image
-
turnToFaceRight
public static processing.core.PImage turnToFaceRight(processing.core.PImage image, double direction) Turns a picture that is drawn facingdirectionso that it faces right, the way a sprite faces in Scratch when its direction is 90.A picture facing up is turned a quarter clockwise, one facing down a quarter anticlockwise. A picture facing left is mirrored rather than turned half way round: turned, a fish facing left would swim on its back.
- Parameters:
image- the picturedirection- the way it is drawn facing: 0 up, 90 right, 180 down, -90 left. 270 and -180 work as well.- Returns:
- a new picture facing right, or
imageitself if it already did - Throws:
IllegalArgumentException- if the direction is not one of the four
-
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
-
getOriginalPixels
public int[] getOriginalPixels()Returns the pixels of the image before it was resized, row by row, as ARGB colours. The array belongs to the image and must not be changed.- Returns:
- the pixels,
getOriginalWidth()per row
-
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
-
setName
-
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
-
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 pixelsheight- 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 heightright- the right slice widthbottom- the bottom slice heightleft- 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. -
isMirrored
Whether a costume is drawn mirrored: with rotation style LEFT_RIGHT, a sprite that does not face right shows its costume flipped. Its hitbox has to be flipped the same way, so both ask here.- Parameters:
direction- the sprite's directionstyle- the sprite's rotation style- Returns:
- true if the costume is drawn mirrored
-
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 buffersize- a percentage valuedegrees- directionx- a x coordinatey- a y coordinatestyle- a rotation styleshader- a shader
-
draw
public void draw(processing.core.PGraphics buffer, double size, double degrees, double x, double y, RotationStyle style, Shader shader, double centerX, double centerY) Draw the scaled image so that a given point of it - its rotation center - sits at a given position, turned or mirrored about that point.- Parameters:
buffer- a buffersize- a percentage valuedegrees- directionx- a x coordinatey- a y coordinatestyle- a rotation styleshader- a shadercenterX- the rotation center's distance from the left edge, in drawn pixelscenterY- the rotation center's distance from the top edge, in drawn pixels
-
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 valuedegrees- directionx- a x coordinatey- a y coordinatestyle- 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 buffersize- a percentage valuedegrees- directionx- a x coordinatey- a y coordinateshader- 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
-