V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kusanoyui
V2EX  ›  Java

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

  •  
  •   kusanoyui · 2020-01-04 23:27:43 +08:00 · 1503 次点击
    这是一个创建于 1572 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小弟最近在做学校的一个作业,一个迷宫游戏,用户在可以手动控制的同时也可以直接按键生成路径。路径的生成过程需要用动画演示出来,像贪食蛇那样一扭一扭地到达终点。 但我每次运行完之后都直接将路径输了出来,没有路径的生成过程,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 方法改变面板,但总是不行。
                    
                    
     麻烦各位大佬了
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1416 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:52 · PVG 07:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.