Files
OAC-Swing/src/main/java/org/openautonomousconnection/oacswing/animated/AnimatedComponent.java

63 lines
1.5 KiB
Java
Raw Normal View History

2026-02-07 18:54:19 +01:00
/* Author: Maple
* Jan. 23 2026
* */
package org.openautonomousconnection.oacswing.animated;
import javax.swing.*;
import java.util.concurrent.atomic.AtomicInteger;
public interface AnimatedComponent {
void setCurrentRun(Timer timer);
Timer getCurrentRun();
void setAnimationPath(AnimationPath animationPath);
AnimationPath getAnimationPath();
void setBounds(int x, int y, int width, int height);
default void play(double speed, boolean loop) {
AtomicInteger ticksPassed = new AtomicInteger();
this.setCurrentRun(new Timer(0, e -> {
if(ticksPassed.get() * speed / (100) < 1) {
ticksPassed.addAndGet(this.getAnimationPath().getInbetweens());
return;
}
KeyFrame next = this.getAnimationPath().getNext();
if(next == null) {
if(loop)
this.getAnimationPath().reset();
else
((Timer) e.getSource()).stop();
return;
}
this.setBounds(next.position().x, next.position().y, next.width(), next.height());
ticksPassed.set(0);
}));
this.getCurrentRun().start();
}
default void play(double speed) {
this.play(speed, false);
}
default void play() {
this.play(1, false);
}
default void stop() {
if(this.getCurrentRun() != null)
if(this.getCurrentRun().isRunning())
this.getCurrentRun().stop();
this.getAnimationPath().reset();
}
}