小弟最近在做学校的一个作业,一个迷宫游戏,用户在可以手动控制的同时也可以直接按键生成路径。路径的生成过程需要用动画演示出来,像贪食蛇那样一扭一扭地到达终点。 但我每次运行完之后都直接将路径输了出来,没有路径的生成过程,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 方法改变面板,但总是不行。 麻烦各位大佬了 