Package helma.image

Class ImageWrapper

java.lang.Object
helma.image.ImageWrapper

public class ImageWrapper extends Object
Abstract base class for Image Wrappers.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected ImageGenerator
     
    protected int
     
    protected Image
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    ImageWrapper(Image image, int width, int height, ImageGenerator generator)
    Creates a new ImageWrapper object.
    ImageWrapper(Image image, ImageGenerator generator)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates and returns a copy of this image.
    void
    crop(int x, int y, int w, int h)
    Crops the image.
    void
    Dispose the Graphics context and null out the image.
    void
    drawImage(ImageWrapper image, int x, int y)
    Draws another image to this image.
    void
    Draws another image to this image.
    void
    drawImage(String filename, int x, int y)
    Draws another image to this image.
    void
    drawLine(int x1, int y1, int x2, int y2)
    Draws a line to this image from x1/y1 to x2/y2.
    void
    drawRect(int x, int y, int w, int h)
    Draws a rectangle to this image.
    void
    drawString(String str, int x, int y)
    Draws a string to this image at the given coordinates.
    void
    fillRect(int x, int y, int w, int h)
    Draws a filled rectangle to this image.
    Converts the internal image object to a BufferedImage (if it's not already) and returns it.
    Returns the Graphics object to directly paint to this Image.
    int
    Returns the height of this image.
    Returns the Image object represented by this ImageWrapper.
    int
    getPixel(int x, int y)
    Returns the pixel at x, y.
    Returns the ImageProducer of the wrapped image
    int
    Returns the width of this image.
    void
    reduceColors(int colors)
    Reduces the colors used in the image.
    void
    reduceColors(int colors, boolean dither)
    Reduces the colors used in the image.
    void
    reduceColors(int colors, boolean dither, boolean alphaToBitmask)
    Reduce the colors used in this image.
    void
    resize(int w, int h)
    Resizes the image
    protected void
    resize(int w, int h, boolean smooth)
    Resizes the image using the Graphics2D approach
    void
    resizeFast(int w, int h)
    Resize the image, using a fast and cheap algorithm
    void
    saveAs(OutputStream out, String mimeType)
    Saves the image.
    void
    saveAs(OutputStream out, String mimeType, float quality)
    Saves the image.
    void
    saveAs(OutputStream out, String mimeType, float quality, boolean alpha)
    Saves the image.
    void
    saveAs(String filename)
    Save the image.
    void
    saveAs(String filename, float quality)
    Saves the image.
    void
    saveAs(String filename, float quality, boolean alpha)
    Saves the image.
    void
    setColor(int color)
    Sets the color used to write/paint to this image.
    void
    setColor(int red, int green, int blue)
    Sets the color used to write/paint to this image.
    void
    setColor(Color color)
    Sets the color used to write/paint to this image.
    void
    Sets the color used to write/paint to this image.
    void
    setFont(String name, int style, int size)
    Set the font used to write on this image.
    protected void
    Sets the internal image and clears the stored graphics object.
    void
    Sets the palette index of the transparent color for Images with an IndexColorModel.
    void
    trim(int x, int y)
    Trims the image.
    void
    trim(int x, int y, boolean trimLeft, boolean trimTop, boolean trimRight, boolean trimBottom)
    Trims the image.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • image

      protected Image image
    • width

      protected int width
    • height

      protected int height
    • generator

      protected ImageGenerator generator
  • Constructor Details

    • ImageWrapper

      public ImageWrapper(Image image, int width, int height, ImageGenerator generator)
      Creates a new ImageWrapper object.
      Parameters:
      image - ...
      width - ...
      height - ...
    • ImageWrapper

      public ImageWrapper(Image image, ImageGenerator generator)
  • Method Details

    • getBufferedImage

      public BufferedImage getBufferedImage()
      Converts the internal image object to a BufferedImage (if it's not already) and returns it. this is necessary as not all images are of type BufferedImage. e.g. images loaded from a resource with the Toolkit are not. By using getBufferedImage, images are only converted to a getBufferedImage when this is actually needed, which is better than storing images as BufferedImage in general.
      Returns:
      the Image object as a BufferedImage
    • getGraphics

      public Graphics2D getGraphics()
      Returns the Graphics object to directly paint to this Image. Converts the internal image to a BufferedImage if necessary.
      Returns:
      the Graphics object for drawing into this image
    • setImage

      protected void setImage(Image img)
      Sets the internal image and clears the stored graphics object. Any code that is changing the internal image should do it through this function to make sure getGraphcis() returns a valid graphics object the next time it is called.
    • clone

      public Object clone()
      Creates and returns a copy of this image.
      Overrides:
      clone in class Object
      Returns:
      a clone of this image.
    • getImage

      public Image getImage()
      Returns the Image object represented by this ImageWrapper.
      Returns:
      the image object
    • getSource

      public ImageProducer getSource()
      Returns the ImageProducer of the wrapped image
      Returns:
      the images's ImageProducer
    • dispose

      public void dispose()
      Dispose the Graphics context and null out the image.
    • setFont

      public void setFont(String name, int style, int size)
      Set the font used to write on this image.
    • setColor

      public void setColor(int red, int green, int blue)
      Sets the color used to write/paint to this image.
      Parameters:
      red - ...
      green - ...
      blue - ...
    • setColor

      public void setColor(int color)
      Sets the color used to write/paint to this image.
      Parameters:
      color - ...
    • setColor

      public void setColor(Color color)
      Sets the color used to write/paint to this image.
      Parameters:
      color - ...
    • setColor

      public void setColor(String color)
      Sets the color used to write/paint to this image.
      Parameters:
      color - ...
    • drawString

      public void drawString(String str, int x, int y)
      Draws a string to this image at the given coordinates.
      Parameters:
      str - ...
      x - ...
      y - ...
    • drawLine

      public void drawLine(int x1, int y1, int x2, int y2)
      Draws a line to this image from x1/y1 to x2/y2.
      Parameters:
      x1 - ...
      y1 - ...
      x2 - ...
      y2 - ...
    • drawRect

      public void drawRect(int x, int y, int w, int h)
      Draws a rectangle to this image.
      Parameters:
      x - ...
      y - ...
      w - ...
      h - ...
    • drawImage

      public void drawImage(String filename, int x, int y) throws IOException
      Draws another image to this image.
      Parameters:
      filename - ...
      x - ...
      y - ...
      Throws:
      IOException
    • drawImage

      public void drawImage(ImageWrapper image, int x, int y) throws IOException
      Draws another image to this image.
      Parameters:
      image - ...
      x - ...
      y - ...
      Throws:
      IOException
    • drawImage

      public void drawImage(ImageWrapper image, AffineTransform at) throws IOException
      Draws another image to this image.
      Parameters:
      image - ...
      at - ...
      Throws:
      IOException
    • fillRect

      public void fillRect(int x, int y, int w, int h)
      Draws a filled rectangle to this image.
      Parameters:
      x - ...
      y - ...
      w - ...
      h - ...
    • getWidth

      public int getWidth()
      Returns the width of this image.
      Returns:
      the width of this image
    • getHeight

      public int getHeight()
      Returns the height of this image.
      Returns:
      the height of this image
    • crop

      public void crop(int x, int y, int w, int h)
      Crops the image.
      Parameters:
      x - ...
      y - ...
      w - ...
      h - ...
    • trim

      public void trim(int x, int y)
      Trims the image.
      Parameters:
      x - the x-coordinate of the pixel specifying the background color
      y - the y-coordinate of the pixel specifying the background color
    • trim

      public void trim(int x, int y, boolean trimLeft, boolean trimTop, boolean trimRight, boolean trimBottom)
      Trims the image.
      Parameters:
      x -
      y -
      trimLeft -
      trimTop -
      trimRight -
      trimBottom -
    • resize

      protected void resize(int w, int h, boolean smooth)
      Resizes the image using the Graphics2D approach
    • resize

      public void resize(int w, int h)
      Resizes the image
      Parameters:
      w - ...
      h - ...
    • resizeFast

      public void resizeFast(int w, int h)
      Resize the image, using a fast and cheap algorithm
      Parameters:
      w - ...
      h - ...
    • reduceColors

      public void reduceColors(int colors)
      Reduces the colors used in the image. Necessary before saving as GIF.
      Parameters:
      colors - colors the number of colors to use, usually <= 256.
    • reduceColors

      public void reduceColors(int colors, boolean dither)
      Reduces the colors used in the image. Necessary before saving as GIF.
      Parameters:
      colors - colors the number of colors to use, usually <= 256.
      dither - ...
    • reduceColors

      public void reduceColors(int colors, boolean dither, boolean alphaToBitmask)
      Reduce the colors used in this image. Useful and necessary before saving the image as GIF file.
      Parameters:
      colors - the number of colors to use, usually <= 256.
      dither - ...
      alphaToBitmask - ...
    • saveAs

      public void saveAs(String filename) throws IOException
      Save the image. Image format is deduced from filename.
      Parameters:
      filename - ...
      Throws:
      IOException
    • saveAs

      public void saveAs(String filename, float quality) throws IOException
      Saves the image. Image format is deduced from filename.
      Parameters:
      filename - ...
      quality - ...
      Throws:
      IOException
    • saveAs

      public void saveAs(String filename, float quality, boolean alpha) throws IOException
      Saves the image. Image format is deduced from filename.
      Parameters:
      filename - ...
      quality - ...
      alpha - ...
      Throws:
      IOException
    • saveAs

      public void saveAs(OutputStream out, String mimeType) throws IOException
      Saves the image. Image format is deduced from mimeType.
      Parameters:
      out - ...
      mimeType - ...
      Throws:
      IOException
    • saveAs

      public void saveAs(OutputStream out, String mimeType, float quality) throws IOException
      Saves the image. Image format is deduced from mimeType.
      Parameters:
      out - ...
      mimeType - ...
      quality - ...
      Throws:
      IOException
    • saveAs

      public void saveAs(OutputStream out, String mimeType, float quality, boolean alpha) throws IOException
      Saves the image. Image format is deduced from mimeType.
      Parameters:
      out - ...
      mimeType - ...
      quality - ...
      alpha - ...
      Throws:
      IOException
    • setTransparentPixel

      public void setTransparentPixel(int trans)
      Sets the palette index of the transparent color for Images with an IndexColorModel. This can be used together with getPixel(int, int).
    • getPixel

      public int getPixel(int x, int y)
      Returns the pixel at x, y. If the image is indexed, it returns the palette index, otherwise the rgb code of the color is returned.
      Parameters:
      x - the x coordinate of the pixel
      y - the y coordinate of the pixel
      Returns:
      the pixel at x, y