简体   繁体   English

在Java Swing游戏循环中正确使用SwingWorker

[英]Correct use of SwingWorker in Java Swing game loop

I'm currently developing a simulation involving several thousand 1x1 pixel 2D Rectangles moving around a JPanel . 我目前正在开发一个涉及在JPanel周围移动的数千个1x1像素2D矩形的模拟。 The rectangles move and can collide and join together. 矩形移动并可以碰撞并连接在一起。

I have created an Event Dispatch Thread which in turn creates my GUI. 我创建了一个Event Dispatch Thread ,它又创建了我的GUI。 I am then creating an instance of the simulation and using a game-loop to control the system with move() , detectCollision() and repaint() methods, with all of the rectangles stored in a global ArrayList . 然后,我创建了一个模拟实例,并使用游戏循环来控制系统,使用move()detectCollision()repaint()方法,所有矩形都存储在全局ArrayList move() shifts each rectangle by 1 pixel while detectCollision() checks to see if two rectangles are next to each other and joins them together if applicable. move()将每个矩形移动1个像素,而detectCollision()检查两个矩形是否彼此相邻,并将它们连接在一起(如果适用)。

The system currently works, however it runs incredibly slowly. 该系统目前正在运行,但运行速度极慢。 Placing a timer around each method showed that my detectCollision() method can take up to 1000ms to complete. 在每个方法周围放置一个计时器表明我的detectCollision()方法最多可能需要1000毫秒才能完成。 My question is, can I use a worker thread inside the detectCollision() method to improve the efficiency of the program? 我的问题是,我可以在detectCollision()方法中使用工作线程来提高程序的效率吗?

Broad-phase algorithm 广义算法

  • It is any algorithm to limit the number of pairs to be checked for collision in a simulation system. 任何算法都可以限制在仿真系统中检查碰撞的对的数量。

  • If you are performing n^2 collision checks, you need a broad-phase algorithm. 如果您正在执行n ^ 2碰撞检查,则需要一个广泛的算法。

  • Since your objects are already rectangles, I think some space partitioning technique should help you. 由于您的对象已经是矩形,我认为一些空间分区技术可以帮助您。

Resource: Broad-phase collision detection methods? 资源: 广角碰撞检测方法?

In most application, I have seen an efficient broad-phase algorithm reduce the time taken by orders of magnitude. 在大多数应用中,我已经看到有效的宽相算法减少了数量级所需的时间。 But of course that varies with the situation. 但当然这会因情况而异。

Good luck. 祝好运。

To deal with computationally intensive simulations, applications generally divide the work into two domains: the graphics engine, including your JPanel's subsclass's #repaint() ; 为了处理计算密集型模拟,应用程序通常将工作分为两个领域:图形引擎,包括JPanel的子类的#repaint(); the simulation engine which would contain the #move() and #detectCollision() methods. 包含#move()和#detectCollision()方法的模拟引擎。

The simulation would be run within it's own Thread, updating the displayable state via graphics engine using SwingUtilities#invokeLater method. 模拟将在它自己的Thread中运行,使用SwingUtilities#invokeLater方法通过图形引擎更新可显示状态。 The simulation generally attempts to achieve a certain number of simulation ticks per second, sleeping in between. 模拟通常试图在每秒之间实现一定数量的模拟滴答。

Generally an Event Dispatcher Thread (EDT) is created inside the native AWT to digest events arriving from the underlying windowing system. 通常,在本机AWT内部创建事件调度程序线程(EDT)以消化从底层窗口系统到达的事件。 If you are manually creating this somehow it's sign you are probably using Swing or AWT incorrectly. 如果您以某种方式手动创建它,则表示您可能错误地使用Swing或AWT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM