Thread.sleep()停止我的绘画? [英] Thread.sleep() stopping my paint?

查看:98
本文介绍了Thread.sleep()停止我的绘画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个试图在屏幕上移动卡片的程序,就像您实际上将它从桌子上拉出一样。下面是动画代码:

  public void move(int x,int y){
int curX = this.x; //整个类扩展矩形
int curY = this.y;

//将动作放置到位置
(int i = curX; i> x; i--){
this.x = i;
}

this.x = x;
this.y = y;
}

此矩形对象位于jframe内的面板内部。重新绘制面板,我有这样的:

  public void run(){
while(Core.isRunning() ){
gamePanel.repaint(); //我的矩形对象位于

中的面板try {
Thread.sleep(50);
} catch(InterruptedException e){
e.printStackTrace();



$ / code $ / pre

这是一个线程,重新绘制gamePanel每50毫秒。



现在,我意识到这可能不是做这种事情的最佳方法。如果有更好的方法来做这个整个重新绘制的东西,请告诉我!

但是,我遇到的问题是当我调用 move()命令为我的矩形,它通过线程,但图像没有更新,直到结束,所以它只是从a点跳到最后的位置。



为什么会发生这种情况?任何人都可以批评/改进我的代码吗?谢谢!

解决方案

问题是你调用 Thread.sleep()事件调度线程中导致GUI无响应。为避免这种情况,您可能需要使用 Swing Timer

 计时器计时器=新的计时器(50,新的ActionListener(){
@Override
public void actionPerformed ActionEvent e){
if(!stop){
gamePanel.repaint();
} else {
((Timer)e.getSource())。
}
}
});
timer.setRepeats(true);
timer.setDelay(50);
timer.start();

其中 stop 是一个布尔型标志,动画必须停止。


I'm making a program that is trying to animate a card moving across the screen as if you actually drew it from a desk. Here is the code for the animating:

public void move(int x, int y) {
    int curX = this.x; //the entire class extends rectangle
    int curY = this.y;

    // animate the movement to place
    for (int i = curX; i > x; i--) {
        this.x = i;
    }

    this.x = x;
    this.y = y;
}

this rectangle object is inside of a panel inside of a jframe. for repainting the panel, i have this:

public void run() {
    while (Core.isRunning()) {
        gamePanel.repaint(); //panel in which my rectangle object is in

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

this is a thread, repainting the gamePanel every 50 milliseconds.

Now, I realize this may not be the best way to do this sort of thing. if there is a better way to do this whole repainting thing, please inform me!

But, the problem i'm having is when i call the move() command for my rectangle, it goes through the thread, but the image isnt updating till the end, so it's just a jump from point a to the final location.

why is this happening? can anyone critique/improve my code? thanks!

解决方案

The problem is you call Thread.sleep() in the Event Dispatch Thread causing the GUI become unresponsive. To avoid this you may want to use Swing Timer instead:

   Timer timer = new Timer(50, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(!stop) {
                gamePanel.repaint();
            } else {
                ((Timer)e.getSource()).stop();
            }
        }
    });
    timer.setRepeats(true);
    timer.setDelay(50);
    timer.start();

Where stop is a boolean flag that indicates the animation must stop.

这篇关于Thread.sleep()停止我的绘画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆