Class ImageUtil

  • All Implemented Interfaces:
    IImageUtilities

    @Component
    public class ImageUtil
    extends java.lang.Object
    implements IImageUtilities
    Utility class for handling the cropping and scaling for JavaFX images.
    Author:
    Andi Hotz, (c) Sahits GmbH, 2013 Created on Nov 10, 2013
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageUtil()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javafx.scene.image.Image addCityNames​(IMap map, javafx.scene.image.Image image, javafx.scene.text.Font font)
      Add the city names to an existing map image.
      (package private) Scaling computeScaleFactor​(javafx.geometry.Dimension2D targetDim, javafx.geometry.Dimension2D imgDim, EScaling scaling)  
      (package private) Scaling computeScaleFactorCropLimited​(ImageScaleState state, EScaling scaling)
      Compute the scale factor so the image can be displayed within targetDim.
      private double computeScalingX​(javafx.geometry.Dimension2D targetDim, javafx.geometry.Dimension2D imgDim)
      Compute the scaling factor for the x axis
      private double computeScalingY​(javafx.geometry.Dimension2D targetDim, javafx.geometry.Dimension2D imgDim)
      Compute the scaling factor for the y axis
      private javafx.geometry.Rectangle2D createDefaultViewPortSize​(javafx.scene.image.Image image)
      Create a viewport without any cropping.
      javafx.scene.image.Image createMapWithCities​(IMap map, IPlayer player)
      Create the map image with all the cities.
      javafx.scene.image.Image createMediterraneanMap​(IPlayer player, IMediterreanMap map)
      Create the mediterrean image map with all the discovered cities and the explored loacations for a player.
      private javafx.geometry.Rectangle2D createViewPort​(ImageScaleState scaleState, javafx.scene.image.Image image, double targetHeight, double targetWidth)
      Calculate the viewport which is defined by the cropping.
      javafx.scene.image.ImageView cropAndScale​(java.lang.String imageName, ImageScaleState scaleState)
      Apply proper scaling to the image as defined in the state.
      private double getScaledMaxCrop​(ImageScaleState scaleState)
      Maximum cropping with considered scaling factor.
      private double getTargetHeight​(ImageScaleState scaleState)
      Retrieve the target height of the image
      private double getTargetWidth​(ImageScaleState scaleState)
      Retrieve the target width of the image
      private java.awt.image.BufferedImage scale​(java.awt.image.BufferedImage sbi, int dWidth, int dHeight, double fWidth, double fHeight)
      scale image
      javafx.scene.image.Image scale​(javafx.scene.image.Image input, javafx.geometry.Dimension2D destDim, double scale)
      Scale an image.
      • Methods inherited from class java.lang.Object

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

      • ImageUtil

        public ImageUtil()
    • Method Detail

      • cropAndScale

        public javafx.scene.image.ImageView cropAndScale​(java.lang.String imageName,
                                                         ImageScaleState scaleState)
        Description copied from interface: IImageUtilities
        Apply proper scaling to the image as defined in the state. If possible the image is cropped to provided optimal scaling
        Specified by:
        cropAndScale in interface IImageUtilities
        Parameters:
        imageName - name of the Image to be scaled
        scaleState - current state of the image
        Returns:
        Scaled/cropped/bordered image
      • createViewPort

        private javafx.geometry.Rectangle2D createViewPort​(ImageScaleState scaleState,
                                                           javafx.scene.image.Image image,
                                                           double targetHeight,
                                                           double targetWidth)
        Calculate the viewport which is defined by the cropping.
        Parameters:
        scaleState - state
        image - current image
        targetHeight - target height
        targetWidth - target width
        Returns:
        viewport
      • getScaledMaxCrop

        private double getScaledMaxCrop​(ImageScaleState scaleState)
        Maximum cropping with considered scaling factor.
        Parameters:
        scaleState - scaling meta data for the image
        Returns:
        maximum cropping in pixels.
      • getTargetWidth

        private double getTargetWidth​(ImageScaleState scaleState)
        Retrieve the target width of the image
        Parameters:
        scaleState - scaling meta data for the image
        Returns:
        target with of the scaled image
      • getTargetHeight

        private double getTargetHeight​(ImageScaleState scaleState)
        Retrieve the target height of the image
        Parameters:
        scaleState - scaling meta data for the image
        Returns:
        target height of the scaled image
      • createDefaultViewPortSize

        private javafx.geometry.Rectangle2D createDefaultViewPortSize​(javafx.scene.image.Image image)
        Create a viewport without any cropping.
        Parameters:
        image - for which the view port should be created
        Returns:
        viewport encompassing the whole image.
      • computeScaleFactorCropLimited

        Scaling computeScaleFactorCropLimited​(ImageScaleState state,
                                              EScaling scaling)
        Compute the scale factor so the image can be displayed within targetDim. Consider potential cropping capabilities so the maximum scale up can be compensated by cropping in the other dimension
        Parameters:
        state - State of the image containing the target and original dimensions
        scaling - Indicate if it is to be scaled up or down
        Returns:
        Scale state for scaling ans cropping.
      • computeScalingX

        private double computeScalingX​(javafx.geometry.Dimension2D targetDim,
                                       javafx.geometry.Dimension2D imgDim)
        Compute the scaling factor for the x axis
        Parameters:
        targetDim - target dimension
        imgDim - current dimension
        Returns:
        scaliing factor in the width
      • computeScalingY

        private double computeScalingY​(javafx.geometry.Dimension2D targetDim,
                                       javafx.geometry.Dimension2D imgDim)
        Compute the scaling factor for the y axis
        Parameters:
        targetDim - target dimension
        imgDim - current dimension
        Returns:
        scaling factor in the height.
      • computeScaleFactor

        Scaling computeScaleFactor​(javafx.geometry.Dimension2D targetDim,
                                   javafx.geometry.Dimension2D imgDim,
                                   EScaling scaling)
      • createMapWithCities

        public javafx.scene.image.Image createMapWithCities​(IMap map,
                                                            IPlayer player)
        Description copied from interface: IImageUtilities
        Create the map image with all the cities. Cities differentiate between normal city, home town and city with trading office.
        Specified by:
        createMapWithCities in interface IImageUtilities
        Parameters:
        map - model.
        player - to which the map belongs
        Returns:
        Image of the Map
      • addCityNames

        public javafx.scene.image.Image addCityNames​(IMap map,
                                                     javafx.scene.image.Image image,
                                                     javafx.scene.text.Font font)
        Description copied from interface: IImageUtilities
        Add the city names to an existing map image.
        Specified by:
        addCityNames in interface IImageUtilities
        Parameters:
        map - model
        image - base image of the map
        font - to be used for the city names.
        Returns:
        Image with the localized city names added.
      • createMediterraneanMap

        public javafx.scene.image.Image createMediterraneanMap​(IPlayer player,
                                                               IMediterreanMap map)
        Description copied from interface: IImageUtilities
        Create the mediterrean image map with all the discovered cities and the explored loacations for a player.
        Specified by:
        createMediterraneanMap in interface IImageUtilities
        Parameters:
        player - for which the mag should be created.
        map - model of the map.
        Returns:
        JavaFX image of the map.
      • scale

        private java.awt.image.BufferedImage scale​(java.awt.image.BufferedImage sbi,
                                                   int dWidth,
                                                   int dHeight,
                                                   double fWidth,
                                                   double fHeight)
        scale image
        Parameters:
        sbi - image to scale
        dWidth - width of destination image
        dHeight - height of destination image
        fWidth - x-factor for transformation / scaling
        fHeight - y-factor for transformation / scaling
        Returns:
        scaled image
      • scale

        public javafx.scene.image.Image scale​(javafx.scene.image.Image input,
                                              javafx.geometry.Dimension2D destDim,
                                              double scale)
        Description copied from interface: IImageUtilities
        Scale an image.
        Specified by:
        scale in interface IImageUtilities
        Parameters:
        input - image that is to be scaled
        destDim - dimension of the target image
        scale - factor
        Returns:
        scaled instance of the image.