Class ImageAnimator
java.lang.Object
ca.corbett.extras.image.animation.ImageAnimator
Smoothly animates an image from a source location to a destination location, with optional
acceleration and deceleration parameters on either end of the image movement.
Controlling the animation
- maxSpeed is how many pixels the image will be moved per frame when it has attained maximum speed (i.e. after acceleration).
- easingStrength describes the strength of the "easing" function, which determines how quickly the image accelerates from a standstill up to maxSpeed, and how quickly it decelerates from max speed back down to a standstill.
- easingZonePercentage this is the distance (expressed as a percentage of the distance between source location and destination location) that the image will spend in acceleration or deceleration. For example, a value of 0.1 (ten percent) means that the image will spend the first ten percent of its voyage accelerating up to full speed, then spend 80 percent of the voyage at full speed, and then spend the last ten percent of its voyage decelerating down to zero. Valid values are 0.0 (instance acceleration) up to 0.5 (first half of the trip is acceleration, second half of the trip is slowing down).
- easingType allows you to choose whether easing occurs only at the start of the voyage (EASE_IN), only at the end of the voyage (EASE_OUT), or at each end of the voyage (EASE_IN_OUT). You can also disable easing altogether with LINEAR.
- Since:
- swing-extras 2.3
- Author:
- scorbo2 (with help from Claude!)
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected doubleprotected doubleprotected doubleprotected doubleprotected doubleprotected doubleprotected doubleprotected ImageAnimator.EasingTypeprotected doubleprotected BufferedImageprotected longprotected doubleprotected booleanprotected doubleprotected doubleprotected doubleprotected float -
Constructor Summary
ConstructorsConstructorDescriptionImageAnimator(BufferedImage image, double startX, double startY, double destX, double destY, double maxSpeed) Creates an ImageAnimator with default easing settings.ImageAnimator(BufferedImage image, double startX, double startY, double destX, double destY, double maxSpeed, double easingStrength, ImageAnimator.EasingType easingType, double easingZonePercentage) Creates an ImageAnimator with configurable easing. -
Method Summary
Modifier and TypeMethodDescriptionprotected doublecalculateEasing(double progress) protected doublecalculateEasing(double progress, boolean forceEaseOut, boolean forceEaseIn) protected doublecalculateEasingWithZones(double progress) protected voiddoubledoubledoublegetImage()doublefloatbooleanvoidUpdates movement of the image and renders it at its new position.voidsetDestination(double newDestX, double newDestY) Sets a new destination for the image.voidsetEasingStrength(double easingStrength) Updates the easing strength.voidsetEasingType(ImageAnimator.EasingType easingType) Updates the easing type.voidsetEasingZonePercentage(double easingZonePercentage) Updates the easing zone percentage.voidsetImage(BufferedImage newImage) Sets a new image to animate (maintains current position and destination).voidsetMaxSpeed(double maxSpeed) Updates the maximum movement speed.voidsetPosition(double x, double y) Immediately positions the image at the specified coordinates without animation.voidsetTransparency(float transparency) Sets the alpha value to use when drawing the image.
-
Field Details
-
image
-
startX
protected double startX -
startY
protected double startY -
destX
protected double destX -
destY
protected double destY -
currentX
protected double currentX -
currentY
protected double currentY -
maxSpeed
protected double maxSpeed -
easingStrength
protected double easingStrength -
easingZonePercentage
protected double easingZonePercentage -
transparency
protected float transparency -
lastUpdateTime
protected long lastUpdateTime -
movementComplete
protected boolean movementComplete -
totalDistance
protected double totalDistance -
directionX
protected double directionX -
directionY
protected double directionY -
easingType
-
-
Constructor Details
-
ImageAnimator
public ImageAnimator(BufferedImage image, double startX, double startY, double destX, double destY, double maxSpeed) Creates an ImageAnimator with default easing settings. -
ImageAnimator
public ImageAnimator(BufferedImage image, double startX, double startY, double destX, double destY, double maxSpeed, double easingStrength, ImageAnimator.EasingType easingType, double easingZonePercentage) Creates an ImageAnimator with configurable easing.- Parameters:
image- The BufferedImage to animatestartX- Starting X coordinatestartY- Starting Y coordinatedestX- Destination X coordinatedestY- Destination Y coordinatemaxSpeed- Maximum movement speed in pixels per secondeasingStrength- Strength of easing effect (1.0 = linear, higher = more curved)easingType- Type of easing to applyeasingZonePercentage- Percentage of travel distance for easing zones (0.0 to 0.5)
-
-
Method Details
-
calculateMovementParameters
protected void calculateMovementParameters() -
calculateEasingWithZones
protected double calculateEasingWithZones(double progress) -
calculateEasing
protected double calculateEasing(double progress) -
calculateEasing
protected double calculateEasing(double progress, boolean forceEaseOut, boolean forceEaseIn) -
renderFrame
Updates movement of the image and renders it at its new position. -
isMovementComplete
public boolean isMovementComplete()- Returns:
- true if the image has reached its destination
-
setDestination
public void setDestination(double newDestX, double newDestY) Sets a new destination for the image. The image will start moving towards this new destination from its current position. -
setPosition
public void setPosition(double x, double y) Immediately positions the image at the specified coordinates without animation. -
getCurrentX
public double getCurrentX()- Returns:
- current X position of the image
-
getCurrentY
public double getCurrentY()- Returns:
- current Y position of the image
-
getImage
- Returns:
- the BufferedImage being animated
-
setImage
Sets a new image to animate (maintains current position and destination). -
getProgress
public double getProgress()- Returns:
- current movement progress as a value between 0.0 and 1.0
-
setMaxSpeed
public void setMaxSpeed(double maxSpeed) Updates the maximum movement speed. -
setEasingStrength
public void setEasingStrength(double easingStrength) Updates the easing strength. -
setEasingType
Updates the easing type. -
setEasingZonePercentage
public void setEasingZonePercentage(double easingZonePercentage) Updates the easing zone percentage.- Parameters:
easingZonePercentage- Percentage of travel distance for easing zones (0.0 to 0.5)
-
getEasingZonePercentage
public double getEasingZonePercentage()- Returns:
- current easing zone percentage
-
getTransparency
public float getTransparency() -
setTransparency
public void setTransparency(float transparency) Sets the alpha value to use when drawing the image. A value of 0.0 means the image will be fully transparent (invisible). A value of 1.0 (the default value) means the image will be fully opaque (will completely obstruct whatever is behind it. Values in between 0 and 1 will cause partial transparency.
-