在 Java、AWT 中,repaint-method 似乎被忽略而支持 start-method [英] In Java, AWT, repaint-method seems to be ignored in favor of start-method

查看:15
本文介绍了在 Java、AWT 中,repaint-method 似乎被忽略而支持 start-method的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建棋盘游戏的小程序,处理用户输入大致如下所示:

I'm building an applet of a board game, and handling user input roughly looks like this:

public void mousePressed(MouseEvent event) {
    int row = event.getX() / (getSize().width / 8) ;
    int column = event.getY() / (getSize().height / 8) ;
    if(possibleMove(column, row) {
        makeMove(column,row,whosTurn); 
        repaint();
        start();
    }
}

在人工输入之后,计算机选择一个移动并像这个方法一样调用 repaint() 和 start().
但是屏幕似乎只有在计算机移动后才会更新,因此在调用 start() 之后.怎么会发生这种情况,因为 repaint() 在 start() 之前被调用?

After a human input, the computer chooses a move and calls repaint() and start() like this method does.
But the screen seems to update only after the computer has made a move, so after start() is called. How can this happen, since repaint() is called before start()?

我怀疑这可能是因为 repaint() 启动了一个新线程(是吗?),但它为什么要等待 start()?

I have a suspicion this might be because repaint() launches a new thread (does it?), but why would it wait for start()?

如果需要,我当然可以提供更多代码.

I could provide more code if necessary, of course.

推荐答案

repaint() 调用不执行重绘 - 它安排重绘完成.在任何当前和已经安排的事件完成后,事件线程稍后会执行实际的重绘(由于其他与此处无关的原因,它甚至可能发生得更晚).start() 方法在调度完成后立即调用,作为响应当前事件的一部分.所以是的,实际绘制总是在 start() 被调用之后发生.

The repaint() call does not do the repaint - it schedules a repaint to be done. The actual repaint is carried out later by the event thread after any current and already scheduled events have been finished (it may happen even later than that for other reasons not relevant here). The start() method is called immediately after the scheduling is done, as part of responding to the current event. So yes, the actual paint will always take place after start() is called.

参见对repaint()绘制机制的描述 了解更多详情.

See the description of repaint() and the description of the paint mechanism for more details.

一般来说,像这样调用 start() 可能很糟糕.当 start() 被调用时,UI 无法响应任何事情(例如调整游戏窗口的大小或未覆盖),除非 start() 是一个非常短的操作,否则这将导致 UI 看起来没有响应.

In general calling start() like this is probably bad. While start() is being called the UI cannot respond to anything (such as the game window being resized or uncovered), and unless start() is a very short action this will result in the UI seeming unresponsive.

这篇关于在 Java、AWT 中,repaint-method 似乎被忽略而支持 start-method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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