如何立即重新绘制/更新paintcomponent?

wsxa1bj1  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(166)

这个问题已经存在

在java中,我到底可以在哪里调用repain()方法[[关闭]
上个月关门了。
有没有什么方法可以让我每次都立即更新/重新绘制屏幕 move() 是在嵌套循环中调用的吗?因为只有 move()actionPerfomed() 实际上是在更新我的屏幕并重新绘制它。

public void startGame() {

    running = true;
    timer = new Timer(DELAY,this);
    timer.start();
}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    draw(g);
 }

public void draw(Graphics g) {

    if(running){

    g.fillRect(x, y, UNIT_SIZE, UNIT_SIZE);

    }

 }

public void move()
{
    switch(direction) {
        case 'U':
        y = y - UNIT_SIZE;
        break;
        case 'D':
        y = y + UNIT_SIZE;
        break;
        case 'L':
        x  = x  - UNIT_SIZE;
        break;
        case 'R':
        x  = x  + UNIT_SIZE;
        break;

        // changing coordinates of the graphics should be repainted after this method call
 }

    public void enter( )
    {
        for(int i = 0; i < numtiles; i++)
        {
            if (state == 'P' && i != 0)
            {
                while(x != UNIT_SIZE && y != UNIT_SIZE)
                    move();    // this doesn't update the screen //

            }

        move(); // this also doesn't update the screen //

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
       if(running){

            move();   // only this method updates the screen //
            enter();
       }

            repaint();
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题