从Java中另一个类调用repaint? [英] Call repaint from another class in Java?

查看:540
本文介绍了从Java中另一个类调用repaint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能这样做不对,所以请好的。
我正在开发一个Java游戏,我在测试人物移动/动画的舞台。

I'm probably doing this wrong, so please be nice. I'm developing a Java game, and I'm at the stage of testing character movement / animation.

人可以上下移动,左侧和右侧上的网格。
网格中绘制的类是类的GamePanel。
这些按钮是在gameControlPanel类。

The "person" can move up down left and right on a grid. The class the grid is drawn in is the gamePanel class. The buttons are in the gameControlPanel class.

我有一个按钮,滋生对电网的人。
然后,我有一个按钮,将人上下左右。

I have a button which spawns a person on the grid. I then have a button to move the person up down left and right.

在拉升按钮pssed $ P $,它调用从Person类的拉升方法。
(此刻,我只测试一个人在同一时间。)
在该方法是下述code ...

When the move up button is pressed, it calls the move up method from the person class. (At the moment, I'm only testing one "person" at a time.) In that method is the following code...

int move = 10;
while(move!=0)
{
    setTopLeftPoint(new Point((int)getTopLeftPoint().getX(),
                              (int)getTopLeftPoint().getY() - 3));

    try
    {
        Thread.sleep(300);
    } catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    move-=1;
}

问题是,我似乎无法从Person类中呼吁的GamePanel类重绘方法。
为了解决这个问题,我创建了重绘每20ms的的GamePanel类的计时器。

The problem is that I can't seem to call the repaint method for the gamePanel class from within the Person class. To get around this, I created a timer in the gamePanel class which repaints every 20ms.

当我preSS的人催生后向上按钮,该按钮依然pssed,直到while循环的周期$ P $已经完成,那么此人的圈子再presentation是显示在以上的方格。

When I press the up button after the person is spawned, the button remains pressed down until the cycles of the while loop have been completed, and then the circle representation of the person is displayed in the grid square above.

我会尽量回答这方面的任何问题。

I will try to answer any questions regarding this.

推荐答案

如果你想在一定的间隔重新绘制, javax.swing.Timer中的可能是该类您。在特定的情况下,重绘,你可以从一个非EDT线程调用它,但你可能让自己陷入困难,因为你现在要处理多个线程。

If you want to repaint at a certain interval, javax.swing.Timer is probably the class for you. In the specific case of repaint you can call it from a non-EDT thread, but you may get yourself into difficulty as you are now dealing with multiple threads.

这篇关于从Java中另一个类调用repaint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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