• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and Painting

 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, today for some practice, I whooped up a code that generates randomly colored circles (noise) on the screen.

This is the code I wrote:


However, while it's running through that code, because it's not a thread(I think?) it doesn't allow you to close the program by hitting the X in the corner, you have to close it from eclipse.

I (tried) to write a code using a thread, but no circles are painted on the screen then:



Can someone tell me why this doesn't work, and how to fix it?

(Also, just for future reference, because of it painting so many images on the screen, and them not being removed the amount of RAM being used by the program increases by 1Mb every couple of seconds. How would I remove these circles from the screen, would I have to use double buffering?)
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you significantly reduce the number of times you pass through the while loop - to a few hundred or maybe a 1000, you'll see more interesting results, and the GUI will respond to user input as it should once the while loop has completed. There's a lot that can be learned by messing around with a little program like the first one.

Put the second program away and come back to it after studying multi-threading (or concurrency) and graphics in Swing a bit more.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which you can read about in Concurrency in Swing.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic