Hi, I have a single class like this one
public class BlockSpawner implements Runnable{
public static long timeToSpawn;
private GtrisJFrame frame;
public BlockSpawner(GtrisJFrame frame)
{
this.frame = frame;
timeToSpawn = 2000;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(timeToSpawn);
}
catch(InterruptedException e)
{
//Unhandled exception
}
//After awake, instanciate 2 blocks
//get the position of the first one
int index = Block.getRandomStartPosition();
new Block(frame, index);
new Block(frame, index+1);
}
}
}
I instance this class in the JFrame main class, and start it's thread like this:
private void initBlockSpawner()
{
spawner = new BlockSpawner(this);
new Thread(spawner).start();
}
I call this initBlockSpawner() function from within the JFrame Constructor. Block Class is really a little big, but in a nutshell, it implements runnable, and calls its run() method at the end of its constructor. The run() method only makes a block to fall at certain speed. I've been tried to manually instantiate new blocks in the JFrame constructor and they work, they repaint and fall. But whenever I want to instantiate Blocks from other threads, they appear to fall (i.e. its properties update each loop), but they don't paint in the JFrame.
As additional information, I am using NetBeans, and since the application entry point is on the JFrame class, the main method looks like this:
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GtrisJFrame().setVisible(true);
}
});
}
I don't have that much experience with Java Threads, awt events and swing components. But something I read here makes me think that my problem is that only one thread has the control over the swing components, or something... Is there any way to solve my problem?
Thanks in advance.
EDIT: Additional 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:
new Block(this, index).setPreferredSize(new Dimension(328, 552));
But it didn't work, anyone knows what this [,0,0,328x552] value could mean?
Thanks everyone, I think we're almost there!
EDIT 2: I realized that the Size of the component is x:0 y:0, why is this? I change the BlockSpawner's run() method to something like this:
public void run()
{
while(true)
{
System.out.println("SPAWN");
int index = Block.getRandomStartPosition();
new Thread(new Block(frame, index)).start();
new Thread(new Block(frame, index+1)).start();
try
{
Thread.sleep(timeToSpawn);
}
catch(InterruptedException e)
{
//Unhandled exception
}
}
}
On the first run, everything goes ok! even the pair of Blocks paint on the JFrame and fall down correctly, but after the Thread.sleep(), the rest of them just get instantiated, but their getSize() method gives me x:0 y:0; Is this still somehow related to the One Dispatcher Thread issue? or it's something different now?