I've been trying to make a java game loop in canvas that runs render(Graphics g) and logic() but it isn't working.
I have already tried making a script that runs the two functions then calls itself again to create a loop.
Thanks in advance.
I tried this:
public class Canvas extends JPanel {
static GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
static int width = gd.getDisplayMode().getWidth();
static int height = gd.getDisplayMode().getHeight();
//Get the system time
long lastTime = System.nanoTime();
//Specify how many seconds there are in a minute as a double
//store as a double cause 60 sec in nanosec is big and store as final so it can't be changed
final double ticks = 60D;
//Set definition of how many ticks per 1000000000 ns or 1 sec
double ns = 1000000000 / ticks;
double delta = 0;
public static void main(String[] args) {
JFrame frame = new JFrame("Zombie Run");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Canvas());
frame.setSize(width, height);
frame.setVisible(true);
loop();
}
public static void loop() {
logic();
render(g);
loop();
}
public static void render(Graphics g) {
System.out.println("Running Render");
g.setColor(Color.BLUE);
g.fillRect(0, 0, 800, 500);
}
public static void logic() {
System.out.println("Logic");
}
}
The error I got from eclipse is:
Logic
Running Render
Exception in thread "main" java.lang.NullPointerException
at ZombieGame.Canvas.render(Canvas.java:41)
at ZombieGame.Canvas.loop(Canvas.java:36)
at ZombieGame.Canvas.main(Canvas.java:31)