NOTE: This thread is already at GameDev forum, but I think this problem should be of interest also in this forum. Thanks for your comprehension over this n00b.
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?
Myself, I don't see a solution to your problem given the evidence you've presented, and it's a good bet that your bug is somewhere deep in code that you've not posted here. Keep using println statements of a debugger to try to isolate the problem, such as what you've already done (and that data sounds suspicious that your blocks have a 0x0 size).
Sorry for double-open a discussion, I didn't wanted to Is there any way that any admin or myself delete the one at GameDev?
I change the run() method on my BlockSpawner (that extends Thread) class to something like this:
And it apparently worked! spawns the first pair of blocks and they show on the JFrame, then, just shows the log "SPAWN" but doesn't paints the cubes... The way I construct this BlockSpawner Thread is something like this:
at the JFrame class. The toString() method continued to behave as I mentioned before:
Block:mainPkg.Block[,0,0,328x552] for the first (painted) pair of blocks
Block:mainPkg.Block[,0,0,0x0] for the rest of the non-painted blocks
That Thread.sleep() is the bug's source... I placed it before instantiate the Blocks and now not even the first pair appears! Plus, i debbug the instantiated block with getSize(), and here are the results:
with pre Thread.sleep() instantiated blocks (only the first pair): size x:328 y:552
with post Thread.sleep() instantiated blocks (after the first loop): size x:0 y:0