No frame refreshing when buffered window is called/created

Hi,

i'm programming a java-based game. It uses a JFrame that calls an "accelerated window" ( a buffered window with a Graphics2D Object ) to draw a basketball simulation. The problem is that when the accelerated window is running ( loop ) the refreshing and the listeners of the main window doesn't work properly (note that accelerated window is called by a Game class that extends Thread... ). Why? Thanks

// Class Game is called by a main JFrame...

publicclass Gameextends Thread{

.

.

.

public Game( ... ){

...

court =new Court2D();

court.setVisible(true);

court.draw();

...

}

...

}

/* ************************************************* */

publicclass Court2Dextends Canvas{

...

public Court2D(){

container =new JFrame("Court");

container.addWindowListener(new WindowAdapter(){

publicvoid windowClosing( WindowEvent e ){

container.dispose();

}

});

JPanel panel = (JPanel) container.getContentPane();

panel.setPreferredSize(new Dimension( WIDTH, HEIGHT));

panel.setLayout(null);

setBounds(0,0, WIDTH, HEIGHT);

panel.add(this);

setIgnoreRepaint(true);

...

container.setResizable(false);

container.setVisible(true);

requestFocus();

createBufferStrategy(2);

strategy = getBufferStrategy();

draw();

}

publicvoid draw(){

Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

...//draw something...

g.dispose();

strategy.show();

}

...

}

picture: http://backspace.altervista.org/JBM/problem.jpg

[2918 byte] By [backspacea] at [2007-10-2 20:54:02]
# 1
ok now I know that the problem is the loop that implements the "IA" of basketball players. The Game object is a Thread so why it blocks other threads ( include WindowListeners ) ?
backspacea at 2007-7-13 23:38:28 > top of Java-index,Other Topics,Java Game Development...
# 2
Unless you haven't shown a vital piece of your code, although Game extends Thread it isn't actually running as a thread - where is your run()method?
TimRyanNZa at 2007-7-13 23:38:28 > top of Java-index,Other Topics,Java Game Development...
# 3

Ops I'm sorry. There's a run() method as you can see as follows:

public class Game extends Thread {

...

public Game(...){

//initiate variables...

}

public void run() { // Called by the main window

if (quarter == 0)

jumpBall();

while ( ( quarter <= 4 ) && !(timeout) ) { // game loop

while ( ( timer < DURATION ) && !(timeout) ) { // quarter loop

step(); //Move

checkfouls();

timer = timer + 0.1;

try {

Thread.sleep( GAMESPEED );

} catch (Exception e) {}

}

minutes = (int) timer / 60;

seconds = (int) timer % 60;

quarter++;

timer = 0;

restartAction();

if ( (quarter > 4) && ( statistics.stats[USER].score == statistics.stats[CPU].score ) )

ot = true;

else

ot = false;

}

}

// other methods

}

Ok, this Thread never stops and the main window never responds to any type of command ( Listeners included ). Is this strange? This Thread sleeps for a second ( or more ) every loop step but nothing happens in the main window...

backspacea at 2007-7-13 23:38:28 > top of Java-index,Other Topics,Java Game Development...
# 4
ok problem solved! I have called the game.run() method instead game.start()... excuse me and thanks for the help
backspacea at 2007-7-13 23:38:28 > top of Java-index,Other Topics,Java Game Development...
# 5

Well you have to call start() if you want it to run as a separate thread. If you call the run() method directly then the method will run in the same thread as the caller.

I can't really tell what's happening from the code you've shown, but perhaps it doesn't matter if it works the way you want, now.

TimRyanNZa at 2007-7-13 23:38:28 > top of Java-index,Other Topics,Java Game Development...
# 6
yes thank you now I know what happens... :D
backspacea at 2007-7-13 23:38:28 > top of Java-index,Other Topics,Java Game Development...