Guest User

Untitled

a guest
Jun 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. SimulationThread extends Thread
  2.  
  3. public void run()
  4.  
  5. while (isRunning())
  6.  
  7. synchronized (this)
  8.  
  9. while (isPaused())
  10.  
  11. try this.wait()
  12.  
  13. doCalculations()
  14. doRendering() // more precisely invoke the repaint() method
  15.  
  16.  
  17.  
  18.  
  19. ******************************************************
  20.  
  21.  
  22. ............
  23.  
  24. public void onUnPauseButtonPress()
  25.  
  26. synchronized (Simulation.getSimulationThread())
  27.  
  28. Simulation.getSimulationThread().notify();
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. ******************************************************
  37.  
  38.  
  39. also be aware that the doCalculations() and the paintComponent() of the panel
  40. need to be synchronized (ideally with the panel object) because paintComponent()
  41. is actually called in the swing thread. you just call repaint() and then swing
  42. will call paintComponent in its own thread. if not synchronized the data could
  43. change half way through the rendering of a frame.
Add Comment
Please, Sign In to add comment