📄 animator.java
字号:
package like.graphics;
import java.util.*;
import java.awt.*;
/**定义一个自定义的播放动画帧的方法
*/
public abstract class Animator
{
public static final int AnimatorMode_Indexed = 0;
public static final int AnimatorMode_Looped = 1;
public static final int AnimatorMode_OneShot = 2;
public static final int AnimatorMode_Randoms = 3;
public static final int AnimatorMode_Single = 4;
/**对图象帧链表的引用
*/
protected LinkedList frames;
/**动画的当前索引
*/
protected int currIndex;
/**创建一个新的Animator对象,其动画帧序列为空
*/
protected Animator()
{
frames = null;
currIndex = 0;
}
public final void setFrames(LinkedList list)
{
this.frames = list;
}
public final LinkedList getFrames()
{
return frames;
}
//重置动画
public void reset()
{
this.currIndex = 0;
}
//返回当前下标
public int getCurrIndex()
{
return this.currIndex;
}
//设置当前下标
public void setCurrIndex(int index)
{
this.currIndex = index;
}
//返回动画当前帧
public Image getCurrFrame()
{
if(frames != null)
{
return (Image)frames.get(currIndex);
}
return null;
}
//定义如何播放帧
public abstract void nextFrame();
//用传入的数组作为索引的动画帧
public static class Indexed extends Animator
{
protected int[] indices;
protected int arrayIndex;
public Indexed()
{
super();
arrayIndex = 0;
}
public Indexed(int [] idx)
{
indices = idx;
arrayIndex = 0;
currIndex = indices[0];
}
public void reset()
{
arrayIndex = 0;
this.currIndex = indices[0];
}
public void nextFrame()
{
if(frames != null)
{
if(++arrayIndex >= indices.length)
{
arrayIndex = 0;
}
currIndex = indices[arrayIndex];
}
}
}//Indexed
//动画帧按顺序循环播放
public static class Looped extends Animator
{
public Looped()
{
super();
}
public void nextFrame()
{
if(frames != null)
{
if(++currIndex >= frames.size())
{
reset();
}
}
}
}//Looped
//顺序播放,不循环
public static class OneShot extends Animator
{
public void nextFrame()
{
if(frames != null)
{
if(++currIndex >= frames.size())
{
currIndex = frames.size()-1;
}
}
}
}//OneShot
//随机播放
public static class Randoms extends Animator
{
public void nextFrame()
{
if(frames != null)
{
currIndex = Math.abs((int)Math.random()%frames.size());
}
}
}//Randoms
//只含一帧的动画,不做任何处理
public static class Single extends Animator
{
public void nextFrame()
{
//do nothing:)
}
}//Single
}//Animator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -