about the code structure

First, I must say that i'm newbie in java game. and i have a puzzle , i want to hear your opinions.

(1)The way I saw in java2d examples when draw shapes is always:

class MyClass extends JPanel { // as a canvas

...

public void paintComponent(Graphics g) {

// draw the shapes

}

}

(2)When it comes to game, I see this:

class MyClass extends JPanel implements Runnable { // as a canvas

...

public void run {

while (gameIsRunning == true) {

repaint();

// take a nap

}

}

public void paintComponent(Graphics g) {

// draw the background and the sprites

}

}

(3)But everything change, when i look at this book these days "Developing Game in Java". In this book, it doesn't use thread in order to draw something, even no the paint or paintComponent methods. It used the so called "active rendering", and so on....

I feel really puzzled, just don't understand the different between (2) and (3). Can anybody here tell me the different and which approach is better.

Thank you!

[1114 byte] By [cjrena] at [2007-10-2 18:18:44]
# 1

Preliminary: in the GUI toolkit(s) with Java, and I presume any GUI toolkit for any system, there is an existing thread running that takes care of GUI elements. This thread looks for user input and then invokes code as necessary.

Preliminary #2: In most games, there's an opponent that's played by the computer; the new thread runs the opposing player's actions. (Note that this isn't the case for all games. For example a Number-15 puzzle wouldn't need a new thread.)

In case 1, no new thread is created, because it's not necessary. The standard GUI thread can take care of things, including calling paintComponent. This is called "passive rendering".

continued...

paulcwa at 2007-7-13 19:39:09 > top of Java-index,Other Topics,Java Game Development...
# 2

In case 2, a new thread is created because it's necessary for the given game, and furthermore the thread is associated with that particular class. (Personally, I feel it makes for much cleaner code when the GUI element in question, in this case a JPanel, doesn't implement Runnable; I think it's better if it creates a new, possibly anonyous class to do so.) Having multiple threads, associated with different individual classes used in the game, makes sense for puzzle games or the like, that is, slower games in which most activity is driven by the user. These games act more like regular GUI applications. This is also a case of passive rendering.

continued...

paulcwa at 2007-7-13 19:39:09 > top of Java-index,Other Topics,Java Game Development...
# 3

In case 3, there actually is a new thread created somewhere to drive the game, most likely. But the code is structured in such a way that there's only one, and it's probably not associated with any single class.It's advantageous to do things this way when you have a lot of individual elements in the game acting and interacting frequently. This is the case for your typical action game. The reason this is advantageous is because there is overhead to using threads, and with a lot of interacting elements, individual element-specific threads would spend a lot of time synchronizing, which would end up being less efficient. So for action games you tend to have a single thread driving a "game loop" that updates state for all the individual parts and also drives rendering. When the rendering is driven by something other than the existing GUI thread, it's called "active rendering".

paulcwa at 2007-7-13 19:39:09 > top of Java-index,Other Topics,Java Game Development...
# 4

Thank you so much, paulcw. you give me a lot of information :-)

Actually, I'm doing a little game call "Tank Battle". In my game, there are two players and each has a tank, they can control their tank to shoot at each other.

Although the code can work at last, but I guess it must have mary problem which can be made more better, like flicker.

Anyway, the main code likes this:

public class BattleField extends JPanel implements Runnable, KeyListener {

// subclass JPanel to customize the paintComponent method.

// The JPanel likes the canvas, draw everything here.

public BattleField() {

gameStart(); // initialize the game

}

private void gameStart() {

this.setBackground(Color.BLACK);

// define the initialized location of two tanks

gameOn = true;

running = true;

pressEnter = false;

pressEsc = false;

t = new Thread(this);

t.start();

}

public void run() {

while (running == true) { // which means it's not in the state of game over

repaint(); //which will call the paintComponent function

try {

t.sleep(15);// 旝昩 // 20 ~ 15

} catch (InterruptedException e) {

}

}

if (pressEsc == true) { // in the state of game over

System.exit(0); // close the game, when user press "Esc"

} else if (pressEnter == true) {

gameStart();// restart the game, when user press "Enter"

}

}

public void paintComponent(Graphics g) {

super.paintComponent(g); // always have this.

Graphics2D g2d = (Graphics2D)g;

if (gameOn == true) {

// draw the background

// draw the tank

// draw bullet list of the tank1

// draw bullet list of the tank2

// draw the explosion when some one is hitted by bullet

} else { // gameOn == false, somebody is dead, so game over, show the resule

// the result of the game

}

} // end of method paintComponent

/* Handle the key pressed event from the user. */

public void keyPressed(KeyEvent e) {

// here the user can move the tank, which reset the location of his tank

}

public static void main(String[] args) {

BattleField battleField = new BattleField();

JFrame frame = new JFrame("BattleField2");

...

frame.getContentPane().add(battleField);

frame.addKeyListener(battleField);// This is important. Component..addKeyListener(...).

frame.setVisible(true);

}

} /// End

Thank you for your patience! you may find that I render everything in the paintComponet method, which is invoked by the repaint() in the run() method. I guess I haven't use active rendering in my code, yes?

I have look at other people's simple game codes, their code structure is almost the same as mine. But do you think this is not a good way for my code structure? Because I find in the book "developing game in java", the auther don't do it like that. Please feel free to tell me the problem.

Thanks in advance.

cjrena at 2007-7-13 19:39:09 > top of Java-index,Other Topics,Java Game Development...
# 5

Yeah, that's passive rendering. An action game like a tank battle will probably be more appropriate with active rendering, but if it works now, I say don't worry about that yet. Frankly I've tried both and haven't seen much of a difference.

It looks OK to me. You might want to subclass a different component than JPanel, which I suspect has some overhead that you're not using. But maybe not, I don't use Swing much. Actually you're not using Swing much here so maybe you'd want to use plain AWT.

You might want to use double buffering to prevent flicker.

paulcwa at 2007-7-13 19:39:09 > top of Java-index,Other Topics,Java Game Development...
# 6
many hints. Thank you again. And also i want to say that English is not my native language, i feel really happy when i find that you can guess what i mean :-)Java is good, and i also want to what are other people using Java to do?
cjrena at 2007-7-13 19:39:09 > top of Java-index,Other Topics,Java Game Development...