Thread Vs javax.swing.Timer controlling animation of a game in Java?

I have this very direct question regarding the Thread and Timer classes in Java as a way to create game animations. Which is better than two? In fact, the differences between the two and the most preferred in terms of animation in Java? I hope my question arises as it is related to the answers I received from one of my questions here ... question

+4
source share
1 answer

The GUI needs to be updated on the EDT to ensure that the painting is done correctly. Read the section in the Swing Concurrency Guide for a full explanation of this concept.

When you use the Swing timer, the event is executed in EDT, so you can simply tell the component to repaint itself to a new location.

When you use Thread, the loop executes with EDT, and you need to use SwingUtilities.invokeLater (...) to return the drawing code to EDT.

In general, if you code simply moves a component from one place to another, then probably the easiest way to use a timer. However, if you play with a lot of complex logic, then you do not want this logic to run on EDT, as it will not allow the graphical interface to respond to events and repaint itself. In this case, you can use Thread.

+6
source

Source: https://habr.com/ru/post/1303020/


All Articles