java - 图像数组创建动画
问题描述
我正在尝试创建动画。两个图像应该移动 - 汽车和人类。但是人类由两个图像组成,因此它看起来像是一个适当的运动。我正在尝试将图像数组放入我的代码中,但它不起作用。你能帮忙吗?这是我的代码
public class Main extends JFrame implements Runnable{
int x=0;
public Image car = new ImageIcon( "car.png").getImage();
public Image[] human;
private Thread thread;
public Main() {
super("Animation");
this.setSize(700,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());//background
g.drawImage(car,x,150,null);
human = new Image[2];
human[0] = new ImageIcon("move.png").getImage();
human[1] = new ImageIcon("stop.png").getImage();
for (int i=0; i<2; i++)
{
g.drawImage(human[i], x , 200,null);
}
}
public static void main(String[] args) {
Main f = new Main();
f.setVisible(true);
f.setLocationRelativeTo(null);
}
@Override
public void run() {
while(true)
{
x++;
repaint();
try {
Thread.sleep(100);
}catch(InterruptedException e) {
System.out.println("Error!");
}
}
}
}
解决方案
推荐阅读
- amazon-web-services - 从 Lambda 更新 AWS S3 文件出现可怕的性能峰值
- visual-studio-code - 如何撤消 Visual Studio Code 中“在文件中替换”的更改
- javascript - 如何通过nodejs从mongoDB提供下载链接到图像(JPG)
- python - 清理数据直到计数相等
- javascript - 如何在javascript中将arraybuffers数组转换为float32array或普通数组?
- python - Python - 通过每 n 行取 n 个连续行来重塑矩阵
- c++ - 允许传递不同构造函数参数的对象注册表
- r - 如何在R中将时间格式转换为数字
- asp.net-core - 如何对 API 进行版本控制,以使一个版本的 api 需要不记名令牌,而另一个版本不需要
- java - 无法访问 magnolia cms 中的链接和子页面