Class ScaleCache


  • public final class ScaleCache
    extends java.lang.Object
    A cache that stores a pre-scaled version of a source image. This can be used to draw static images on a sheet. Example use:
     // When setting up the sheet:
     sc = new ScaleCache( imageToCache );
     // When drawing the sheet, instead of drawing the original image:
     sc.draw( g, this, target, x, y );
     

    Drawing an image via a scale cache can produce nicer results than drawing the image directly via g.drawImage in a scaled graphics context, particularly when the image must be scaled to less than half its true size.

    Since:
    2.1
    Author:
    Chris Jennings
    • Constructor Summary

      Constructors 
      Constructor Description
      ScaleCache​(java.awt.image.BufferedImage source)
      Creates a scale cache.
      ScaleCache​(java.awt.image.BufferedImage source, int widthInTemplatePixels, int heightInTemplatePixels)
      Creates a scale cache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void draw​(java.awt.Graphics2D gScaled, Sheet<?> sheet, RenderTarget target, int x, int y)
      Draws the image onto a sheet at the specified location.
      java.awt.image.BufferedImage getScaledImage​(RenderTarget target, float factor)
      Returns a version of the image that is scaled up or down from the source image by the requested scaling factor.
      java.awt.image.BufferedImage getScaledImage​(RenderTarget target, int width, int height)
      Returns a version of the image that is scaled to the requested size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ScaleCache

        public ScaleCache​(java.awt.image.BufferedImage source)
        Creates a scale cache. The source image is assumed to have the same resolution as the template image.
        Parameters:
        source - the image to scale for drawing
      • ScaleCache

        public ScaleCache​(java.awt.image.BufferedImage source,
                          int widthInTemplatePixels,
                          int heightInTemplatePixels)
        Creates a scale cache. The source image may have a different resolution than the template image. The specified width and height define the size that image should be drawn at on the template image. For example, if you have an image im that is 300 ppi, but the template image is 150 ppi, then you would use:
        new ScaleCache( im, im.getWidth()/2, im.getHeight()/2 ).
        Parameters:
        source - the image to scale for drawing
        widthInTemplatePixels - the width to draw the image at, measured as if it were being drawn on the original template image
        heightInTemplatePixels - the width to draw the image at, measured as if it were being drawn on the original template image
        Since:
        3.0
    • Method Detail

      • getScaledImage

        public java.awt.image.BufferedImage getScaledImage​(RenderTarget target,
                                                           int width,
                                                           int height)
        Returns a version of the image that is scaled to the requested size. If the dimensions of the original source image are used, the source image is returned. If the dimensions match a previously cached value, then the cached image is returned. Otherwise, a new, scaled image will be created and returned on the fly.
        Parameters:
        target - the target of the rendering operation
        width - the required image width
        height - the required image height
        Returns:
        a version of the source image scaled to the requested size
      • getScaledImage

        public java.awt.image.BufferedImage getScaledImage​(RenderTarget target,
                                                           float factor)
        Returns a version of the image that is scaled up or down from the source image by the requested scaling factor. If the size of the scaled image matches a previously cached result, then the cached result will be returned. If the scale is 1, then the source image will be returned.
        Parameters:
        target - the target of the rendering operation
        factor - a scaling factor to apply to the image
        Returns:
        a version of the source image whose dimensions are scaled by the requested amount
      • draw

        public void draw​(java.awt.Graphics2D gScaled,
                         Sheet<?> sheet,
                         RenderTarget target,
                         int x,
                         int y)
        Draws the image onto a sheet at the specified location.
        Parameters:
        gScaled - the sheet's scaled graphics context
        sheet - the sheet being drawn
        target - the render target value for the sheet
        x - the x-offset into the template to draw the image at
        y - the y-offset into the template to draw the image at