如何删除动画中的最后一张图片 [英] How To Remove The Last Image In An Animation
问题描述
我想要做的是创建一个动画来创建跑步"动作.每当我在屏幕上绘制它时,动画中的最后一帧都会被留下(所以当精灵移动时会留下一串动画帧).我已经尝试过 if 语句并在框架更改时更改图像的绘制位置:
What I am trying to do is create an animation that creates a 'running' motion. Whenever I draw it on the screen, the last frame in the animation is left behind (So there is a trail of animation frames left behind when the sprite moves). I've tried if statements and changing the image's draw position when the frame changes:
if(a2.sceneNum() == 0)
spectre_Draw1 = (screenWidth() / 2 - 120 / 2 + 120 - 6);
else
spectre_Draw1 = 0;
g.drawImage(pic[2], spectre_Draw1, (screenHeight() / 2 - 180 / 2), null);
if(a2.sceneNum() == 1)
spectre_Draw2 = (screenWidth() / 2 - 120 / 2 + 120 - 6);
else
spectre_Draw2 = 0;
g.drawImage(pic[3], spectre_Draw2, (screenHeight() / 2 - 180 / 2), null);
if(a2.sceneNum() == 2)
spectre_Draw3 = (screenWidth() / 2 - 120 / 2 + 120 - 6);
else
spectre_Draw3 = 0;
g.drawImage(pic[4], spectre_Draw3, (screenHeight() / 2 - 180 / 2), null);
有没有办法在删除尾随图像的同时做到这一点?
Is there a way to do this while removing the trailing images?
推荐答案
注意:示例程序中的以下代码存在一些逻辑错误.请参阅Warren K 的回答以获取解释和修复
Note: Code below in example program has some logic errors. Please see answer from Warren K for explanation and fixes
我注意到您尝试为动画图像使用不同的图像.您知道您可以使用单个动画精灵(假设它以类似网格的模式格式化),并且只需更改 中某些
方法x
y
位置的位置drawImage
I noticed that you're trying to use different images for the animation image. You know you could use a single animation sprite (given it is formatted in a grid like pattern) and just change the locations of the certain x
y
positions in the drawImage
method
public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer)
img - the specified image to be drawn. This method does nothing if img is null.
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.
查看完整说明 API一个>
See full description API
话虽如此,您可以使用 javax.swing.Timer
来制作动画并更改源图像的位置.
That being said, You can use a javax.swing.Timer
to animate and change the locations of the source image.
这里有一些示例使用相同的代码用于您在下面看到的所有示例.我只是更改了图像并相应地更改了 SPRITE_ROWS
、SPRITE_COLUMNS
和 DELAY
.请参阅如何使用摆动时间
here are some examples using this same code for all the example you see below. I just changed the image and change the SPRITE_ROWS
, SPRITE_COLUMNS
, and DELAY
accordingly. See more at How to Use Swing Times
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class NerdGirl extends JPanel {
private static final int SPRITE_ROWS = 5;
private static final int SPRITE_COLUMNS = 2;
private static final int DELAY = 150;
private int DIM_W;
private int DIM_H;
private int x1Src;
private int y1Src;
private int x2Src;
private int y2Src;
private BufferedImage img;
public NerdGirl() {
try {
img = ImageIO.read(getClass().getResource("/resources/nerd-girl.jpg"));
} catch (IOException ex) {
Logger.getLogger(NerdGirl.class.getName()).log(Level.SEVERE, null, ex);
}
DIM_W = img.getWidth() / SPRITE_ROWS;
DIM_H = img.getHeight() / SPRITE_COLUMNS;
x1Src = 0;
y1Src = 0;
x2Src = x1Src + DIM_W;
y2Src = y1Src + DIM_H;
Timer timer = new Timer(DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (x1Src >= img.getWidth() - DIM_H - 5) { // 5 to take care of precision loss
x1Src = 0;
x2Src = x1Src + DIM_W;
if (y1Src >= DIM_H - 5) { // 5 to take care of precision loss
y1Src = 0;
y2Src = y1Src + DIM_H;
} else {
y1Src += DIM_H;
y2Src = y1Src + DIM_H;
}
} else {
x1Src += DIM_W;
x2Src = x1Src + DIM_W;
}
repaint();
}
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), x1Src, y1Src, x2Src, y2Src, this);
}
@Override
public Dimension getPreferredSize() {
return (img == null) ? new Dimension(300, 300) : new Dimension(DIM_W, DIM_H);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new NerdGirl());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
<小时>
这篇关于如何删除动画中的最后一张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!