116 lines
2.3 KiB
Java
116 lines
2.3 KiB
Java
import java.awt.Color;
|
|
|
|
class LinearColorAnimator
|
|
{
|
|
private Color _start, _end;
|
|
private final AnimationListener _listener;
|
|
private int _duration;
|
|
private double _elapsed;
|
|
private boolean _started;
|
|
private boolean _finished;
|
|
|
|
/*
|
|
* @param duration Duration in seconds
|
|
*/
|
|
public LinearColorAnimator(Color start, Color end, int duration, AnimationListener listener)
|
|
{
|
|
_start = start;
|
|
_end = end;
|
|
_duration = duration;
|
|
_listener = listener;
|
|
}
|
|
|
|
/*
|
|
* @param elapsed Elapsed time since last call in ms
|
|
*/
|
|
public void update(double elapsed)
|
|
{
|
|
if (_started)
|
|
{
|
|
_elapsed += elapsed * 1e-3;
|
|
|
|
if (_elapsed > _duration)
|
|
{
|
|
_started = false;
|
|
_finished = true;
|
|
if (_listener != null)
|
|
{
|
|
_listener.onAnimationFinished();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void start()
|
|
{
|
|
_started = true;
|
|
_finished = false;
|
|
_elapsed = 0;
|
|
}
|
|
|
|
public void reset()
|
|
{
|
|
_started = _finished = false;
|
|
_elapsed = 0;
|
|
}
|
|
|
|
public void setStart(Color c)
|
|
{
|
|
_start = c;
|
|
}
|
|
|
|
public void setEnd(Color c)
|
|
{
|
|
_end = c;
|
|
}
|
|
|
|
public void setDuration(int duration)
|
|
{
|
|
_duration = duration;
|
|
}
|
|
|
|
public boolean hasStarted()
|
|
{
|
|
return _started;
|
|
}
|
|
|
|
public boolean hasFinished()
|
|
{
|
|
return _finished;
|
|
}
|
|
|
|
public Color getStart()
|
|
{
|
|
return _start;
|
|
}
|
|
|
|
public Color getEnd()
|
|
{
|
|
return _end;
|
|
}
|
|
|
|
public Color getCurrentColor()
|
|
{
|
|
if (_finished)
|
|
{
|
|
return _end;
|
|
}
|
|
if (!_started)
|
|
{
|
|
return _start;
|
|
}
|
|
int diffR = _end.getRed() - _start.getRed();
|
|
int diffG = _end.getGreen() - _start.getGreen();
|
|
int diffB = _end.getBlue() - _start.getBlue();
|
|
double elapsedRatio = _elapsed / _duration;
|
|
return new Color((int)(_start.getRed() + diffR * elapsedRatio),
|
|
(int)(_start.getGreen() + diffG * elapsedRatio),
|
|
(int)(_start.getBlue() + diffB * elapsedRatio));
|
|
}
|
|
|
|
interface AnimationListener
|
|
{
|
|
public abstract void onAnimationFinished();
|
|
}
|
|
}
|