File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 557
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.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and Painting
 
Similar Threads
Graphics Graphics Graphics - Where are my circles?
Help with Scrollable Image please...
Graphics Graphics Graphics - Where are my circles?
background color of a jframe
Applet background color