aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Threads and Painting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Threads and Painting" Watch "Threads and Painting" New topic
Author

Threads and Painting

Jake Cirino
Greenhorn

Joined: Jul 27, 2012
Posts: 17

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

Joined: Oct 24, 2010
Posts: 561
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Which you can read about in Concurrency in Swing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: Threads and Painting