Graphics Context Error

Started by
0 comments, last by destructivArts 12 years ago
I cannot for the life of me, figure out why this code doesn't work.
I keep getting two errors that I defined in try catch statements: dbImage is null and Graphics Context Error: java.lang.NullPointerException
These appear every frame.
I know this is a lot of code, but I want to include anything that could affect it. I'm also leaving out a lot of functions that work with the running of the game not the rendering.

Anyways, the errors are coming from the BufferImage and RenderImage functions


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import com.sun.j3d.utils.timer.J3DTimer;
public class PlatformEngine extends JPanel implements Runnable{
//Window Variables
public static int PWIDTH = 520;
public static int PHEIGHT = 400;
public static PlatformEngine game = new PlatformEngine();

//Method Specific Variables
//Run()
public boolean running = false;
public Thread th = new Thread(this);
public int cycleTime = 15;
public int MAX_FRAME_SKIPS = 5;

//Player
public Player player = new Player();

//BufferImage
private Graphics dbg;
private Image dbImage = null;
//Constructor
//----------------------------------------------------------------------
public PlatformEngine(){
setPreferredSize(new Dimension(PWIDTH,PHEIGHT));
setFocusable(true);
requestFocus();

//Start game thread
th.start();
}

//Game Functions
//----------------------------------------------------------------------
public void run(){
//Updates, Buffers, and Renders the game each frame
//These variables are used to standardize the framerate
long beforeTime,afterTime,timeDiff,sleepTime;
long overSleepTime = 0L;
long excess = 0L;
int skips = 0;


beforeTime = J3DTimer.getValue();

running = true;
while(running){
UpdateGame();
BufferImage();
RenderImage();

afterTime = J3DTimer.getValue();
timeDiff = afterTime - beforeTime;
sleepTime = cycleTime - timeDiff;

try{
if(sleepTime>0)
th.sleep(sleepTime/1000000L);
else
excess -= sleepTime;

}catch(Exception e){}
while((excess > cycleTime) && (skips<MAX_FRAME_SKIPS)){
skips++;
UpdateGame(); //Update but don't render
excess-=cycleTime;
}
}
}

public void UpdateGame(){
//Updates the game
/* This involves:
- checking and handling the active status of entities
- checking collisions
*/
}

public void BufferImage(){
if(dbImage == null){
dbImage = createImage(PWIDTH,PHEIGHT);
if(dbImage == null){
System.out.println("dbImage is null");
return;
}else
dbg = dbImage.getGraphics();
}
//clear the background
dbg.setColor(Color.white);
dbg.fillRect(0,0,PWIDTH,PHEIGHT);
//Individual Paint Functions Here
/* These most likely need to check the state first
REMINDER: Implement State Machine before going into all that
*/


}

public void RenderImage(){
//Draws a pre rendered image of the game to the Panel
Graphics g;
g = this.getGraphics();


try{

if((g!=null)&&(dbImage!=null)){
g.drawImage(dbImage,0,0,this);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}catch(Exception e){
System.out.println("Graphics Context Error: " + e);
}
}
}


Any Help would be great,
Thanks
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out
Advertisement
Ok, nobody seems to be able to find a solution to this. I've asked at school too, and neither my teacher or my classmates can figure this out. Really, what I'm asking is: How would I create my own method to draw a picture, this picture being the next frame (double buffer) and then without using JPanel's paintComponent() method, how would I paint that to the JPanel.

The reason behind the last part is that the call to repaint() is only a request that can be handled whenever the processor has time. I want to ensure that when I tell the JPanel to repaint, that it repaints right then.

Any help is greatly appreciated,

Thanks in advance,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

This topic is closed to new replies.

Advertisement