File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Instantiated components from within a thread aren’t repainting into a JFrame in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Game Development
Reply locked New topic
Author

Instantiated components from within a thread aren’t repainting into a JFrame in Java

Rigoberto Vides
Greenhorn

Joined: Feb 19, 2010
Posts: 4
Hi guys, I'm making this tetris variation, a piece should be spawned every 2 secs. I try to construct this piece (Block) from within a thread like this:


I'm using swingUtilities.invokeLater to send this thread to the swing event dispatcher, everything looks to work normal (i.e. the blocks instantiate each timeToSpawn) milisecs... but there is one BIG problem: they don't show on the Jframe! On the other hand whenever I just instantiate 1 or n blocks from within the same JFrame class like this for example:



They work perfectly fine and paint on the JFrame. What a mess.. I've been like crazy figuring out what's happening! My Block class extends Component and implements Runnable, it's run method looks like this:


It's a thread to update it's position each 450 milisecs... could this be the problem? although as I mentioned before, I just test this code and it worked, the problem is when I instantiate an object of this class within another thread.

As final and extra info. whenever I check the method toString on the Instantiated cubes from threads, they give me this [,0,0,0x0], but when I instantiate them within the same JFrame class they give me this result [,0,0,328x552] and they appear on the frame. this 328x552 value it's the same as the component's Dimension returned by getPreferredSize()... I tried to force them to that dimension by instantiate them like this:



But it didn't work, anyone knows what this [,0,0,328x552] value could mean?

Thanks in advance Everyone!


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiated components from within a thread aren’t repainting into a JFrame in Java