minha imagem não está sendo renderizada. no erro diz q não to invocando o bufferedImage, mas estou eu acho kkk.
erro:
Exception in thread “main” java.lang.NullPointerException: Cannot invoke “java.awt.image.BufferedImage.getSubimage(int, int, int, int)” because “this.sprite” is null
at PackGraficos.SpriteSheet.getSprite(SpriteSheet.java:25)
at br.Game.(Game.java:36)
at br.Game.main(Game.java:69)
package br;
public class Game extends Canvas implements Runnable {
public JFrame frame;
public int width = 800;
public int height = 600;
public Thread thread;
public boolean isRunning = false;
public int fps = 0;
public BufferedImage image;
Player1 player1;
public static List<Entiti> entidades;
public static SpriteSheet spriteSheet;
public Game() {
initFrame();
image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
entidades= new ArrayList<Entiti>();
spriteSheet = new SpriteSheet("/deserto.jpg");
player1 = new Player1(0,0,64,64,spriteSheet.getSprite(0, 0, 64, 64));
entidades.add(player1);
}
public void initFrame() {
frame = new JFrame("Jogo de BRUNO");
frame.setPreferredSize(new Dimension(width, height));
frame.add(this);
frame.pack(); // para que o canvas tenha o mesmo tamanho da aplicaçao.
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public synchronized void start() {
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void stop() {
try {
thread.join();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
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(1,5,3));
g.fillRect(0, 0, width, height);
g=bs.getDrawGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.setFont(new Font("Arial",Font.BOLD,20));
g.setColor(Color.WHITE);
g.drawString("FPS "+fps, 710, 550);
bs.show();
}
@Override
public void run() {
long lestTime = System.nanoTime();
double amountOFTick = 60;
double ns = 1000000000 / amountOFTick;
double delta = 0;
int frames = 0;
double time = System.currentTimeMillis();
//
while (isRunning) {
long now = System.nanoTime();
delta += (now - lestTime) / ns;
lestTime = now;
if (delta >= 1) {
tick();
render();
frames++;
delta--;
}
if (System.currentTimeMillis() - time >= 1000) {
System.out.println("FPS: " + frames);
fps=frames;
frames = 0;
time += 1000;
}
}
stop();
}
}
///////////////////
package PackGraficos;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
public BufferedImage sprite;
public SpriteSheet(String path){
try {
sprite = ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getSprite(int x, int y, int width,int height) {
return sprite.getSubimage(x, y, width, height);
}
}
//////////////
package PackEntidades;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Entiti {
protected double x;
protected double y;
protected int widtht;
protected int height;
protected BufferedImage sprite;
public Entiti(int x, int y, int width, int height, BufferedImage sprite) {
this.x=x;
this.y=y;
this.widtht=width;
this.height=height;
this.sprite=sprite;
}
public void tick() {
}
public void render(Graphics g) {
g.drawImage(sprite,getX(), getY(), null);
}
public void setX(int newX) {
this.x = newX;
}
public void setY(int newY) {
this.y = newY;
}
public int getY() {
return (int)this.y;
}
public int getX() {
return (int)this.x;
}
public int getWidth() {
return widtht;
}
public void setWidth(int width) {
this.widtht = width;
}
public int getHeigh() {
return height;
}
public void setHeigh(int height) {
this.height = height;
}
}
//////////////////
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import br.Game;
public class Player1 extends Entiti{
private BufferedImage cplayer1;
public Player1(int x, int y, int width, int height, BufferedImage sprite) {
super(x, y, width, height, sprite);
// como o spritesheet e ststico (pertence de fato a classe) pode ser invocado.
cplayer1=Game.spriteSheet.getSprite(0, 0, 90, 150);
}
public void render(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(20, 20, 100, 100);
}
}