I have one class like this
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)
{
}
int index = Block.getRandomStartPosition();
new Block(frame, index);
new Block(frame, index+1);
}
}
}
I use this class in the main JFrame class and start it as:
private void initBlockSpawner()
{
spawner = new BlockSpawner(this);
new Thread(spawner).start();
}
I call this function initBlockSpawner () from the JFrame Constructor. The 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 the block fall at a certain speed. I tried to manually create new blocks in the JFrame constructor, and they work, they repaint and fall. But whenever I want to instantiate blocks from other threads, they seem to fall (i.e., His properties update every loop), but they are not drawn in the JFrame.
NetBeans, JFrame, :
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GtrisJFrame().setVisible(true);
}
});
}
Java Threads, awt- swing. -, , , , swing - ... Is ?
.
EDIT: , , toString Instantiated , [, 0,0,0x0], JFrame, [, 0,0,328x552] . 328x552 , , getPreferredSize()... , :
new Block(this, index).setPreferredSize(new Dimension(328, 552));
, - , [, 0,0,328x552]?
, , !
2:
, x: 0 y: 0, ? BlockSpawner run() - :
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)
{
}
}
}
! JFrame , Thread.sleep() , getSize() x: 0 y: 0; One Dispatcher Thread? - ?