Class ImageAnimator

java.lang.Object
ca.corbett.extras.image.animation.ImageAnimator

public class ImageAnimator extends Object
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.
You can specify an alpha value if you wish the image to be drawn partially transparent.
Since:
swing-extras 2.3
Author:
scorbo2 (with help from Claude!)
  • Field Details

    • image

      protected BufferedImage 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

      protected ImageAnimator.EasingType 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 animate
      startX - Starting X coordinate
      startY - Starting Y coordinate
      destX - Destination X coordinate
      destY - Destination Y coordinate
      maxSpeed - Maximum movement speed in pixels per second
      easingStrength - Strength of easing effect (1.0 = linear, higher = more curved)
      easingType - Type of easing to apply
      easingZonePercentage - 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

      public void renderFrame(Graphics2D g)
      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

      public BufferedImage getImage()
      Returns:
      the BufferedImage being animated
    • setImage

      public void setImage(BufferedImage newImage)
      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

      public void setEasingType(ImageAnimator.EasingType easingType)
      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.