在Java中,AWT,重画法似乎赞成启动方法被忽略 [英] In Java, AWT, repaint-method seems to be ignored in favor of start-method

查看:151
本文介绍了在Java中,AWT,重画法似乎赞成启动方法被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个棋盘游戏的一个小程序,并处理用户输入大致是这样的:

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();
    }
}

一个人的输入之后,计算机选择一个举动,并呼吁重绘()和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()?

我怀疑这可能是因为重绘()启动一个新的线程(是吗?),但是为什么会等待启动()?

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

如果有必要,我可以提供更多的code,当然。

I could provide more code if necessary, of course.

推荐答案

重绘()调用不做重绘 - 它时间表重绘工作要做。实际重绘由事件线程任何电流和已经预定的事件已经完成之后,后来进行(可能甚至晚于在这里不相关的其他原因发生)。调度完成后,作为应对当前事件的一部分的开始()方法被立即调用。所以,是的,实际的油漆总是会发生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.

请参阅的说明重绘()油漆机制的描述了解更多信息。

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

在一般调用start()这样的可能是坏的。虽然开始()被调用的UI无法回应任何东西(如游戏窗口被调整或发现),除非启动()是一个很短的行动,这将导致在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,重画法似乎赞成启动方法被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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