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]

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 >
