Hi Java Gaming Com,
I am currently trying to make a game in java, but failed already with the basic.
Iam building an RTSG with nearly no animations for the begining and got most of my knowledge from tutorials like the space invader game and killer game programming.
I Thought of Having one Game routine, wich paints, when needed(not jet integrated) an board.
There Boards implement MouseListeners and a paint Method.
On Creation they register themself @the Game as Listener and remove themself when they are not any longer shown.
By now i have 2 different board, the GalaxyView with witch the Game starts and no problem occur, but when i Switch to the System View there are RandomNullpointers all during the paint method.
My Guess is, that the paint method of the SystemView is called even though the Object is not yet fully created Even though i dont really understand how this can happen.
Since stepping through the expressions with the debugger does prevent the bug from happening i guess it has something to do with Threading.
Comments & Hints and Advise are all appreciated and at this point let me apologize for my bad english ^^
I hope the code i post is enough.
public class Game extends Canvas {
/** The stragey that allows us to use accelerate page flipping */
private BufferStrategy strategy;
/** True if the game is currently "running", i.e. the game loop is looping */
private boolean gameRunning = true;
/** True if game logic needs to be applied this loop, normally as a result of a game event */
private boolean logicRequiredThisLoop = false;
/** The last time at which we recorded the frame rate */
private long lastFpsTime;
/** The current number of frames recorded */
private int fps;
/** The game window that we'll update with the frame count */
private JFrame container;
private Board board;
public Game() {
container = new JFrame("Space Exploration");
board= new GalaxyWrapper(this);
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(GRAPHICALCONSTANTS.width, GRAPHICALCONSTANTS.height));
panel.setLayout(null);
setBounds(0, 0, GRAPHICALCONSTANTS.width, GRAPHICALCONSTANTS.height);
panel.add(this);
setIgnoreRepaint(true);
container.pack();
container.setResizable(false);
container.setVisible(true);
requestFocus();
createBufferStrategy(2);
strategy = getBufferStrategy();
container.setDefaultCloseOperation(container.EXIT_ON_CLOSE);
}
public void gameLoop() {
long lastLoopTime = System.nanoTime();
while (gameRunning) {
long delta = (System.nanoTime()-lastLoopTime);
lastLoopTime = System.nanoTime();
lastFpsTime += delta;
fps++;
if (lastFpsTime >= 1e9) {
container.setTitle("Space Exploration (FPS: " + fps + ")");
lastFpsTime = 0;
fps = 0;
}
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
board.paint(g, this);
g.dispose();
strategy.show();
redraw=false;
try {
Thread.sleep(Math.max(1,10-(System.nanoTime()-lastLoopTime)/1000000));
} catch (InterruptedException e) {
}
}
}
public void setRedraw(Boolean redraw) {
this.redraw = redraw;
}
public Board getBoard() {
return board;
}
public void setBoard(Board board) {
this.board = board;
}
}
public interface Drawable {
public void paint(Graphics2D g,ImageObserver im);
}
public abstract class Board extends MouseAdapter implements Drawable{
private Game game;
public Board(Game game){
this.game=game;
game.setBoard(this);
game.addMouseListener(this);
game.setRedraw(true);
}
public void removeMouseListener(){
game.removeMouseListener(this);
}
public Game getGame(){
return game;
}
}