Class Image
An image’s position
is the upper left corner of its bounding box.
Its size is the size of the underying image file by default, but you can shrink it using
setMaxWidth()
and setMaxHeight()
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Describes the presence and order of color channels in an array of pixels. -
Constructor Summary
ConstructorDescriptionImage
(double x, double y) Creates an Image placeholder with no current image.Image
(double x, double y, BufferedImage image) Creates a bitmap image from the given BufferedImage.Creates a bitmap image from the given file.Image
(int width, int height, byte[] pixels, Image.PixelFormat format) Creates a new image using raw pixel data from the given array.Image
(int width, int height, float[] pixels, Image.PixelFormat format) Creates a new image using raw pixel data from the given array.Image
(BufferedImage image) Creates a bitmap image from the given BufferedImage, positioned at (0, 0).Creates a bitmap image from the given file, positioned at (0,0). -
Method Summary
Modifier and TypeMethodDescriptionprotected void
For internal use.Returns the bounding box of this graphics object in its local coordinates.protected Object
For internal use only.int
Get the height of the underlying image in pixels.int
Get the width of the underlying image in pixels.void
setImagePath
(String path) Changes the image displayed by this graphics element.void
setMaxHeight
(double maxHeight) Causes the image to shrink (preserving aspect ratio) if the image height is larger than the given maximum height.void
setMaxWidth
(double maxWidth) Causes the image to shrink (preserving aspect ratio) if the image width is larger than the given maximum width.boolean
testHitInLocalCoordinates
(double x, double y) Tests whether the point (x, y) touches the image.byte[]
toByteArray
(Image.PixelFormat format) Returns the pixels in this image as an array of bytes, one byte per color channel per pixel, interleaved in the order specified byformat
.float[]
toFloatArray
(Image.PixelFormat format) Returns the pixels in this image as an array of floats, one number per color channel per pixel, interleaved in the order specified byformat
.toString()
Methods inherited from class edu.macalester.graphics.GraphicsObject
addObserver, changed, equals, getAnchor, getBoundsInParent, getCanvas, getCenter, getElementAt, getElementAtLocalCoordinates, getEmbeddedComponent, getHeight, getPosition, getRotation, getScale, getScaleX, getScaleY, getSize, getSizeInParent, getWidth, getX, getY, hashCode, isInBounds, moveBy, moveBy, removeObserver, renderToBuffer, rotateBy, setAnchor, setAnchor, setCenter, setCenter, setPosition, setPosition, setRotation, setScale, setScale, setScale, setX, setY, testHit
-
Constructor Details
-
Image
public Image(double x, double y) Creates an Image placeholder with no current image. -
Image
Creates a bitmap image from the given file, positioned at (0,0). Acceptable file formats include: GIF, PNG, JPEG, BMP, and WBMP.- Parameters:
path
- path of image file to load, relative to the res/ directory.
-
Image
Creates a bitmap image from the given file. Acceptable file formats include: GIF, PNG, JPEG, BMP, and WBMP.- Parameters:
path
- path of image file to load, relative to the res/ directory.
-
Image
Creates a new image using raw pixel data from the given array. The range for sample values is [0...1], and values outside that range are pinned to it when generating the image (i.e. any value ≥ 1 is full intensity, and any value ≤ 0 is zero intensity). There is one array element per color channel, with channels interleaved (seeImage.PixelFormat
.)For example, the array
{ 1, 0.5f, 0, 0, 0, 0.5f }
with the RGB pixel format specifies one orange pixel (R=100%, G=50%, B=0%), then one dark blue pixel (R=0%, G=0%, B=50%).- Parameters:
width
- Image width in pixelsheight
- Image height in pixelspixels
- Raw pixel data. Length must exactly match the number of required samples.format
- Color space and format of channels in the pixels array
-
Image
Creates a new image using raw pixel data from the given array. This method interprets bytes as unsigned: zero intensity is 0, and full intensity is 255 (but Java represents this as -1, because the language does not have unsigned primitive types). There is one array element per color channel, with channels interleaved (seeImage.PixelFormat
.)For example, the array
{ -1, 127, 0, 0, 0, 127 }
with the RGB pixel format specifies one orange pixel (R=100%, G=50%, B=0%), then one dark blue pixel (R=0%, G=0%, B=50%).- Parameters:
width
- Image width in pixelsheight
- Image height in pixelspixels
- Raw pixel data. Length must exactly match the number of required samples.format
- Color space and format of channels in the pixels array
-
Image
Creates a bitmap image from the given BufferedImage, positioned at (0, 0). Note that changing the BufferedImage externally does not automatically force it to redraw. You will need to callCanvasWindow.draw()
to see the changes.- Parameters:
image
-
-
Image
Creates a bitmap image from the given BufferedImage. Note that changing the BufferedImage externally does not automatically force it to redraw. You will need to callCanvasWindow.draw()
to see the changes.- Parameters:
image
-
-
-
Method Details
-
setImagePath
Changes the image displayed by this graphics element.- Parameters:
path
- path of image file to load, relative to the res/ directory.
-
setMaxWidth
public void setMaxWidth(double maxWidth) Causes the image to shrink (preserving aspect ratio) if the image width is larger than the given maximum width. -
setMaxHeight
public void setMaxHeight(double maxHeight) Causes the image to shrink (preserving aspect ratio) if the image height is larger than the given maximum height. -
drawInLocalCoordinates
Description copied from class:GraphicsObject
For internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.- Specified by:
drawInLocalCoordinates
in classGraphicsObject
-
getImageWidth
public int getImageWidth()Get the width of the underlying image in pixels. -
getImageHeight
public int getImageHeight()Get the height of the underlying image in pixels. -
testHitInLocalCoordinates
public boolean testHitInLocalCoordinates(double x, double y) Tests whether the point (x, y) touches the image. Does not take into account image transparency.- Specified by:
testHitInLocalCoordinates
in classGraphicsObject
-
getBounds
Description copied from class:GraphicsObject
Returns the bounding box of this graphics object in its local coordinates.- Specified by:
getBounds
in classGraphicsObject
-
toByteArray
Returns the pixels in this image as an array of bytes, one byte per color channel per pixel, interleaved in the order specified byformat
. Zero is minimum intensity, and 255 (or -1, since bytes are signed) is full intensity.Note that when requesting a grayscale image, this method averages the color channels, which produces results that correspond poorly to perceived brightness.
-
toFloatArray
Returns the pixels in this image as an array of floats, one number per color channel per pixel, interleaved in the order specified byformat
. Zero is minimum intensity, and 1 is full intensity.Note that when requesting a grayscale image, this method averages the color channels, which produces results that correspond poorly to perceived brightness.
-
getEqualityAttributes
Description copied from class:GraphicsObject
For internal use only. Used to compute equals() and hashCode(). Returns an object whose equals() and hashCode() methods encompass the subclass-specific values that should be used to compute equality for the whole GraphicsObject.- Specified by:
getEqualityAttributes
in classGraphicsObject
-
toString
-