请问如何用 swing 做出流畅的动画?

2020-01-04 23:27:43 +08:00
 kusanoyui

小弟最近在做学校的一个作业,一个迷宫游戏,用户在可以手动控制的同时也可以直接按键生成路径。路径的生成过程需要用动画演示出来,像贪食蛇那样一扭一扭地到达终点。 但我每次运行完之后都直接将路径输了出来,没有路径的生成过程,repaint 啊 updateUI 什么的啊都试过,而且也新建了一个线程来进行刷新。但总是不行。。。 部分代码如下,大佬们可以帮小弟看看吗?

public void paint(Graphics g) {
    super.paint(g);
    if (flag == 0)
        g.drawImage(wall, 0, 0, getWidth(), getHeight(), this);
    else if (flag == 1)
        g.drawImage(road, 0, 0, getWidth(), getHeight(), this);
    else if (flag == 2)
        g.drawImage(mouse, 0, 0, getWidth(), getHeight(), this);
    else
        g.drawImage(liangc, 0, 0, getWidth(), getHeight(), this);
}//重写 paintComponent 方法,画图
//面板重绘
public void change(int f)
{
	flag = f;
	new Thread(new Runnable() {

		   public void run() {

		    while (true) {

		     try {

		      Thread.sleep(3000);
		      repaint();

		     } catch (InterruptedException e) {

		      e.printStackTrace();

		     }
		     
		     

		    }

		   }

		  }).start();

		
}

if (currey + 1 < n && !map.tp[currex][currey + 1].isWall() && !isBeVisit[currex * n + currey + 1]) {
                    point[step] = currex * n + currey + 1;
                    isBeVisit[currex * n + currey + 1] = true;                       
                    map.tp[point[step] / n][point[step] % n].change(2);
                    currey++;
                    step++;
                    direction = 0;

                } else direction++;
                //这是找路径的一部分代码,满足条件的点入栈,然后 tp 是整个面板的二维数组,调用 change 方法改变面板,但总是不行。
                
                
 麻烦各位大佬了
1515 次点击
所在节点    Java
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/635126

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX