Class Image


public class Image extends GraphicsObject
A bitmap image that can be drawn to the screen.

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().

  • Constructor Details

    • Image

      public Image(double x, double y)
      Creates an Image placeholder with no current image.
    • Image

      public Image(String path)
      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

      public Image(double x, double y, String path)
      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

      public Image(int width, int height, float[] pixels, Image.PixelFormat format)
      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 (see Image.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 pixels
      height - Image height in pixels
      pixels - 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, byte[] pixels, Image.PixelFormat format)
      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 (see Image.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 pixels
      height - Image height in pixels
      pixels - 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(BufferedImage 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 call CanvasWindow.draw() to see the changes.
      Parameters:
      image -
    • Image

      public Image(double x, double y, BufferedImage 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 call CanvasWindow.draw() to see the changes.
      Parameters:
      image -
  • Method Details

    • setImagePath

      public void setImagePath(String path)
      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

      protected void drawInLocalCoordinates(Graphics2D gc)
      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 class GraphicsObject
    • 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 class GraphicsObject
    • getBounds

      public Rectangle2D getBounds()
      Description copied from class: GraphicsObject
      Returns the bounding box of this graphics object in its local coordinates.
      Specified by:
      getBounds in class GraphicsObject
    • toByteArray

      public 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 by format. 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.

      See Also:
    • toFloatArray

      public 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 by format. 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.

      See Also:
    • getEqualityAttributes

      protected Object 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 class GraphicsObject
    • toString

      public String toString()
      Overrides:
      toString in class Object