Java: Using Swing to Program Games

I am relatively new to game development. I have been developing games and studying game development for 2-3 months. I am using Java.

I always used Swing for my graphics (aka, the whole game is mapped onto JPanelusing the object Graphics2D). I still have not worried about this.

More recently, I ran into a problem in my last project. Some method has problems calling in successive periods of time (sometimes it starts every 15-16 milliseconds, as it should be, and sometimes it starts every 3000 (!) Milliseconds).

I was a little disappointed in debugging and some research, and found out that the reason for this is probably because I am not handling Swing and threads correctly.

My entrie game loop works inside a run()thread method (which is not an EDT). Therefore, I am constantly changing Swing elements outside of EDT. Obviously, this was supposed to cause problems.

When I found out that this was a problem, I thought:

"Hey, I just use SwingUtilities.invokeLater()to run gameloop inside EDT!"

But then I remembered that just as it was "forbidden" to manipulate Swing elements outside of EDT , it was also problematic to manipulate non-Swing objects from within EDT (I think ... is this correct?).

If so, then I have no idea how to develop Java games using Swing without getting into strange problems .

My question is:

, Swing? , , , Swing ? , , Swing, ?

, Java, , .

+4
1

, , , . , -EDT- . - gui, , .., Swing , , EDT. .

, , , EDT, - , , , , ( ++, delete , , - ).

, " , , ", undefined, JVM.

, , EDT, , repaint() 20 ( , , ). , . synchronized paint() paint() , . synchronized , . , .

EDT, , , , , , . , , .

, , ( , ) ? ; , GC .

+3

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


All Articles