在JavaFX中强制立即绘制 [英] Force immediate paint in JavaFX

查看:107
本文介绍了在JavaFX中强制立即绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在继续之前强制JavaFX应用程序重绘自己?类似于Swing Panel的paint(Graphic g)方法(我可能会在那里得到关键字错误)。

Is there a way to force a JavaFX app to repaint itself before proceeding? Similar to a Swing Panel's paint(Graphic g) method (I might be getting the keywords wrong there).

请考虑以下示例:您编写一个TicTacToe应用程序以及计算机播放器需要AI。你希望有能力向两位电脑玩家展示它。也许你在计算机转弯之间暂停了两秒钟,让它具有逼真的效果。当你点击开始按钮时,会有一个很大的无响应停顿(9个回合所需的时间,假装计算机暂停'决定')然后突然应用程序的视觉效果更新完成游戏的状态。

Consider the following example: you write a TicTacToe app along with the AI required for a computer player. You would like the ability to show two computer players duke it out. Maybe you put in a two second pause between computer turns to give it a life-like affect. When you hit your "Go" button, there's a large pause of unresponsiveness (the time it takes for the 9 turns to go by with faked pauses for the computer to 'decide') and then suddenly the app's visual is updated in with the completed game's state.

一旦应用程序线程中的处理完成,似乎JavaFX会重新绘制?我在这里不太确定。

It seems like JavaFX repaints once processing in the app's thread is finished? I'm not completely sure here.

谢谢!

推荐答案

你是对的。 JavaFX是事件驱动和单线程的。这意味着不能同时完成重绘和事件响应。长时间运行的任务应该在单独的线程上执行,这样它们就不会阻止UI的呈现。当任务完成后,它可以通过调用FX.deferAction()来同步回FX线程,这将简单地执行主要代码线程。

You are right. JavaFX is event-driven and single-threaded. This means that repaint and event response can not be done simultaneously. Long-running task should be executed on separate thread so they do not block the rendering of the UI, When the task is finished it can sync back to the FX thread by calling FX.deferAction() which will simply execute the code on the main thread.

这篇关于在JavaFX中强制立即绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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