0

If I create a custom JPanel (where all of the game I'm making will be displayed on the screen), and override it's paintComponent() for that purpose. Is everything inside that method going to be run on the EDT? And what about other methods inside that same class?

What if I call repaint() on the custom JPanel from inside the run() method of a thread I made? Will paintComponent() still be run on the EDT?

Zoyd
  • 3,449
  • 1
  • 18
  • 27
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131

1 Answers1

2

Yes, everything inside that method is going to be run on the EDT including any methods that you invoke within paintComponent().

And what about other methods inside that same class?

If they are called from paintComponent() or from actionPerformed(), etc, then they will also be on the EDT!

What if I call repaint() on the custom JPanel from inside the run() method of a thread I made? Will paintComponent() still be run on the EDT?

repaint() on a non-EDT thread will schedule a repaint -> paintComponent() to be run on the EDT. So yes, still on the EDT! Btw, you can test this by placing a break-point in your paintComponent method and seeing which thread is suspended.

Muel
  • 4,309
  • 1
  • 23
  • 32
  • My game loop is inside the run() method of a different thread. `repaint()` on that custon JPanel (and thus `paintComponent()`) is called from that `run()` method. Will it still run on the EDT? Or on my thread? – Aviv Cohn Mar 17 '14 at 08:06
  • Okay, last question. If this JPanel is the major class for my game. It's paintComponent() does the drawing to the screen, and it has lots of other data to do game logic and change game state. These methods either get called from outside the class, or from different methods inside that class (but not from paintComponent or actionPerformed). These methods don't run on the EDT, right? They run on whatever thread they were called from, even if they were called from inside the class. Correct? – Aviv Cohn Mar 17 '14 at 08:16
  • @Prog Yes, they should run on whichever thread called them. **Remember** as a general rule of thumb, all Swing methods should generally be invoked on the EDT. So if you're running a worker thread, it really shouldn't touch any Swing-related code apart from `repaint()` and a handful of other methods. Beware! – Muel Mar 17 '14 at 08:22
  • How my game works, is I have a non-EDT thread to do calculations, update game objects, get user input from the keyboard, etc (most of these methods are inside my custom JPanel class). And from inside that thread, repaint() is called every update step, activating paintComponent() which is also inside the custom JPanel. Would you consider this safe? – Aviv Cohn Mar 17 '14 at 08:34
  • 1
    beware (@Prog also) http://stackoverflow.com/q/20905985/203657 - calling repaint from any thread other than the EDT might be calling for trouble – kleopatra Mar 17 '14 at 12:05
  • 1
    @Prog In principle it sounds safe, except that I'd expect the keyboard input to be obtained on the EDT, and then safely published to the non-EDT thread. From a design perspective, I'd only have methods in your JPanel class that can be invoked on the EDT. All other code I'd extract out to a controller (the C in MVC) class. I'd use the controller to be concerned with updating the model (your game objects) and view (your JPanel) in a thread-safe manner. – Muel Mar 18 '14 at 00:09