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
Nested ClassesModifier and TypeClassDescriptionstatic enumDescribes the presence and order of color channels in an array of pixels. -
Constructor Summary
ConstructorsConstructorDescriptionImage(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(int width, int height, int[] pixels) Creates a new image using raw pixel data from the given array, one int per pixel.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 voidFor internal use.Returns the bounding box of this graphics object in its local coordinates.protected ObjectFor internal use only.intGet the height of the underlying image in pixels.intGet the width of the underlying image in pixels.voidsetImagePath(String path) Changes the image displayed by this graphics element.voidsetMaxHeight(double maxHeight) Causes the image to shrink (preserving aspect ratio) if the image height is larger than the given maximum height.voidsetMaxWidth(double maxWidth) Causes the image to shrink (preserving aspect ratio) if the image width is larger than the given maximum width.booleantestHitInLocalCoordinates(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.int[]Returns the pixels in this image as an array of ints, one int per pixel.toString()Methods inherited from class edu.macalester.graphics.GraphicsObject
addObserver, changed, equals, getAnchor, getBoundsInParent, getCanvas, getCenter, getElementAt, getElementAtLocalCoordinates, getEmbeddedComponent, getHeight, getParent, 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 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
public Image(int width, int height, int[] pixels) Creates a new image using raw pixel data from the given array, one int per pixel. Each pixel is in 32-bit ARGB format.- Parameters:
width- Image width in pixelsheight- Image height in pixelspixels- Raw pixel data. Length must exactly match the number of required samples.
-
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. -
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.
-
-
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:GraphicsObjectFor internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.- Specified by:
drawInLocalCoordinatesin 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:
testHitInLocalCoordinatesin classGraphicsObject
-
getBounds
Description copied from class:GraphicsObjectReturns the bounding box of this graphics object in its local coordinates.- Specified by:
getBoundsin 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.
-
toIntArray
public int[] toIntArray()Returns the pixels in this image as an array of ints, one int per pixel. Pixels use 32-bit ARGB encoding.Note that the other methods that convert images to arrays return one array entry per color channel, while this method returns one array entry per pixel.
-
getEqualityAttributes
Description copied from class:GraphicsObjectFor 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:
getEqualityAttributesin classGraphicsObject
-
toString
-