Exception in thread “main” java.lang.NullPointerException
at Grafico.Game.(Game.java:28)
at Grafico.Game.main(Game.java:81)
package Grafico;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public static JFrame frame;
private final int WIDTH = 240;
private final int HEGHIT = 160;
private final int SCALE = 3;
private boolean isRunner = true;
private BufferedImage image;
private Spritesheet sheet;
private BufferedImage player;
public Game() {
player = sheet.getSprite(3, 3, 31, 31);
sheet = new Spritesheet("/Player.png");
this.setPreferredSize(new Dimension(WIDTH*SCALE,HEGHIT*SCALE));
initFrame();
image = new BufferedImage(160,120,BufferedImage.TYPE_INT_RGB);
}
public void initFrame(){
frame = new JFrame("Meu Jogo");
frame.add(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public synchronized void start() {
Thread thread = new Thread(this);
isRunner = true;
thread.start();
}
public synchronized void stop() {
isRunner=false;
Thread thread = new Thread(this);
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(222,115,14));
g.fillRect(0, 0, WIDTH, HEGHIT);
g.drawImage(player,31,31, null);
g.dispose();
g = bs.getDrawGraphics();
g.drawImage(image, 0 , 0, WIDTH*SCALE, HEGHIT*SCALE, null );
bs.show();
}
public static void main(String [] args) {
Game game = new Game();
game.start();
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int frames = 0;
double time = System.currentTimeMillis();
while(isRunner) {
long now = System.nanoTime();
delta += (now - lastTime)/ns;
lastTime = now ;
if(delta>=1) {
tick();
render();
frames++;
delta--;
}
if(System.currentTimeMillis() - time >= 1000) {
System.out.println("Fps"+frames);
frames=0;
time +=1000;
}
}
stop();
}
}