我整晚都在寻找/寻找指南,试图找到如何平滑我的多图像动画。我尝试了一些指南,找到了我喜欢的一个,但它很不稳定。
atm 我的动画有效但闪烁。
我的动画课:
package graphics;
导入 java.awt.Image;导入 java.util.ArrayList;
公共类动画{
private ArrayList<OneScene> scenes;
private int sceneIndex;
private long movieTime;
private long totalTime;
//Constructor
public Animation() {
scenes = new ArrayList<OneScene>();
totalTime = 0;
start();
}
//Add scene to ArayList & set time for each scene
public synchronized void addScene(Image i, long t) {
totalTime += t;
scenes.add(new OneScene(i, totalTime));
}
//Start animation
public synchronized void start() {
movieTime = 0;
sceneIndex = 0;
}
//Change Scenes
public synchronized void update(long timePassed) {
if (scenes.size() > 1) {
movieTime += timePassed;
if (movieTime >= totalTime) {
movieTime = 0;
sceneIndex = 0;
}
while(movieTime > getScene(sceneIndex).endTime) {
sceneIndex++;
}
}
}
//Get animation (Image) current scene
public synchronized Image getImage() {
if (scenes.size() ==0) {
return null;
}else{
return getScene(sceneIndex).pic;
}
}
//Get Scene
private OneScene getScene(int x) {
return (OneScene)scenes.get(x);
}
//Private Class
public class OneScene {
Image pic;
long endTime;
public OneScene(Image pic, long endTime) {
this.pic = pic;
this.endTime = endTime;
}
}
&我的一些方法:
Image load1 = new ImageIcon(""+cachemainDir+"1.png").getImage();
Image load2 = new ImageIcon(""+cachemainDir+"2.png").getImage();
Image load3 = new ImageIcon(""+cachemainDir+"3.png").getImage();
Image load4 = new ImageIcon(""+cachemainDir+"4.png").getImage();
Image load5 = new ImageIcon(""+cachemainDir+"5.png").getImage();
Image load6 = new ImageIcon(""+cachemainDir+"6.png").getImage();
Image load7 = new ImageIcon(""+cachemainDir+"7.png").getImage();
Image load8 = new ImageIcon(""+cachemainDir+"8.png").getImage();
Image load9 = new ImageIcon(""+cachemainDir+"9").getImage();
Image load10 = new ImageIcon(""+cachemainDir+"10.png").getImage();
Image load11 = new ImageIcon(""+cachemainDir+"11.png").getImage();
Image load12 = new ImageIcon(""+cachemainDir+"12.png").getImage();
Anim = new Animation();
Anim.addScene(load1, 100);
Anim.addScene(load2, 100);
Anim.addScene(load3, 100);
Anim.addScene(load4, 100);
Anim.addScene(load5, 100);
Anim.addScene(load6, 100);
Anim.addScene(load7, 100);
Anim.addScene(load8, 100);
Anim.addScene(load9, 100);
Anim.addScene(load10, 100);
Anim.addScene(load11, 100);
Anim.addScene(load12, 100);
和...
while (AppLoading) {
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
Anim.update(timePassed);
Graphics g = getGraphics();
draw(g);
g.dispose();
try{
Thread.sleep(0);
}catch(Exception ex){}
}
最后....
public void draw(Graphics g) {
new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
g.drawImage(Anim.getImage(), 300, 300, null);
}