Package ch.sahits.game.graphic.image
Class ImageUtil
- java.lang.Object
-
- ch.sahits.game.graphic.image.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
-
-
Field Summary
Fields Modifier and Type Field Description static intCITY_RADIUSprivate IFontLoaderfontLoaderprivate IDataImageLoaderimageLoaderprivate ITextSizingsizingprivate SwingFXImageConvertorswingUtils
-
Constructor Summary
Constructors Constructor Description ImageUtil()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javafx.scene.image.ImageaddCityNames(IMap map, javafx.scene.image.Image image, javafx.scene.text.Font font)Add the city names to an existing map image.(package private) ScalingcomputeScaleFactor(javafx.geometry.Dimension2D targetDim, javafx.geometry.Dimension2D imgDim, EScaling scaling)(package private) ScalingcomputeScaleFactorCropLimited(ImageScaleState state, EScaling scaling)Compute the scale factor so the image can be displayed withintargetDim.private doublecomputeScalingX(javafx.geometry.Dimension2D targetDim, javafx.geometry.Dimension2D imgDim)Compute the scaling factor for the x axisprivate doublecomputeScalingY(javafx.geometry.Dimension2D targetDim, javafx.geometry.Dimension2D imgDim)Compute the scaling factor for the y axisprivate javafx.geometry.Rectangle2DcreateDefaultViewPortSize(javafx.scene.image.Image image)Create a viewport without any cropping.javafx.scene.image.ImagecreateMapWithCities(IMap map, IPlayer player)Create the map image with all the cities.javafx.scene.image.ImagecreateMediterraneanMap(IPlayer player, IMediterreanMap map)Create the mediterrean image map with all the discovered cities and the explored loacations for a player.private javafx.geometry.Rectangle2DcreateViewPort(ImageScaleState scaleState, javafx.scene.image.Image image, double targetHeight, double targetWidth)Calculate the viewport which is defined by the cropping.javafx.scene.image.ImageViewcropAndScale(java.lang.String imageName, ImageScaleState scaleState)Apply proper scaling to the image as defined in the state.private doublegetScaledMaxCrop(ImageScaleState scaleState)Maximum cropping with considered scaling factor.private doublegetTargetHeight(ImageScaleState scaleState)Retrieve the target height of the imageprivate doublegetTargetWidth(ImageScaleState scaleState)Retrieve the target width of the imageprivate java.awt.image.BufferedImagescale(java.awt.image.BufferedImage sbi, int dWidth, int dHeight, double fWidth, double fHeight)scale imagejavafx.scene.image.Imagescale(javafx.scene.image.Image input, javafx.geometry.Dimension2D destDim, double scale)Scale an image.
-
-
-
Field Detail
-
CITY_RADIUS
public static final int CITY_RADIUS
- See Also:
- Constant Field Values
-
imageLoader
@Autowired private IDataImageLoader imageLoader
-
fontLoader
@Autowired private IFontLoader fontLoader
-
sizing
@Autowired private ITextSizing sizing
-
swingUtils
@Autowired private SwingFXImageConvertor swingUtils
-
-
Method Detail
-
cropAndScale
public javafx.scene.image.ImageView cropAndScale(java.lang.String imageName, ImageScaleState scaleState)Description copied from interface:IImageUtilitiesApply proper scaling to the image as defined in the state. If possible the image is cropped to provided optimal scaling- Specified by:
cropAndScalein interfaceIImageUtilities- Parameters:
imageName- name of the Image to be scaledscaleState- 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- stateimage- current imagetargetHeight- target heighttargetWidth- 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 withintargetDim. 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 dimensionsscaling- 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 dimensionimgDim- 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 dimensionimgDim- 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:IImageUtilitiesCreate the map image with all the cities. Cities differentiate between normal city, home town and city with trading office.- Specified by:
createMapWithCitiesin interfaceIImageUtilities- 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:IImageUtilitiesAdd the city names to an existing map image.- Specified by:
addCityNamesin interfaceIImageUtilities- Parameters:
map- modelimage- base image of the mapfont- 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:IImageUtilitiesCreate the mediterrean image map with all the discovered cities and the explored loacations for a player.- Specified by:
createMediterraneanMapin interfaceIImageUtilities- 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 scaledWidth- width of destination imagedHeight- height of destination imagefWidth- x-factor for transformation / scalingfHeight- 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:IImageUtilitiesScale an image.- Specified by:
scalein interfaceIImageUtilities- Parameters:
input- image that is to be scaleddestDim- dimension of the target imagescale- factor- Returns:
- scaled instance of the image.
-
-