Start from a book!

Hi!
I am reading a book of java…

I try to run an example :

**

  • Write a description of class GamePanel here.
  • @author (your name)
  • @version (a version number or a date)
    */

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{

// instance variables - replace the example below with your own
private static final int PWIDTH = 500;
private static final int PHEIGHT = 400;

private Thread animator;
private volatile boolean running = false;
private volatile boolean gameOver =  false ;



/**
 * Constructor for objects of class GamePanel
 */

public GamePanel()
{
    // initialise instance variables
//SetBackground(Color.white);
SetPreferredSize(new Dimension(PWIDTH,PHEIGHT));

}

public void addNotify(){
super.addNotify();
startgame();
}

private void startGame(){
if (animator == null || !running){
animator = new Thread(this);
animator.start();
    }

}



public void stopGame(){running = false ;}



public void run(){
    
    running = true;

    while(running){
        gameUpdate();
        gameRender();
        repaint();

        try{
                Thread.sleep(20);
        }catch(InterruptedException ex){}
  
    }

System.exit(0);
}//end run


private void gameUpadate() {
    if (!gameOver){
// agiorna lo stato del gioco
                }

}

}// end of class

This need a Frame ora an applet which caontain it , I know… but why when I check error I found more problem ?
I cant compile it!

Thank to all !

And sorry if is a stupid error… :-[

What are the actual compile errors? From a quick look over you try and call gameUpdate but have a method called gameUpadate.

Also, booleans are automagically set to false on creation, and setting explicit values when declaring values is bad form (and can have nasty side effects with inheritance).

Also you have written setBackground and setPreferredSize capitalized (SetBackground and SetPreferredSize), even though they should start with a lowercase “s”. The method gameRender appears to be missing.

[quote](and can have nasty side effects with inheritance)
[/quote]
Could you give an example of the side effects? I haven’t thought about that myself.

There was a thread on GameDev.net a while back, but I can’t find it. Basically someone had a rather subtle bug because the ordering of base class static initialisation and constructors is non-obvious at times. Unfortunatly I can’t actually remember the specifics. :-[

Found it: http://www.gamedev.net/community/forums/topic.asp?topic_id=297309