Reputation: 1
Writing a little game here and I have ran into some issues. I can not for the life of me manage to display my buffered image.
I think I have everything in place but obviously something is wrong if the background is still black.
Game Class:
public class Game extends Canvas implements Runnable {
private boolean running = false;
private Thread thread;
public static int WIDTH, HEIGHT;
public BufferedImage background = null;
// object handler
Handler handler;
Random rand = new Random();
private void init(){
WIDTH = getWidth();
HEIGHT = getHeight();
BufferedImage background = new BufferedImage(0, 0, 0);
try{
background = ImageIO.read(new File("/Background.png"));
}catch(IOException e){
}
handler = new Handler();
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public void run(){
// Heat of the Game. Game Loop.
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames + " TICKS " + updates);
frames = 0;
updates = 0;
}
}
}
private void tick(){
handler.tick();
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(background, 0, 0, null);
g.dispose();
bs.show();
}
private void LoadImageBackground(BufferedImage image){
int h = image.getHeight();
int w = image.getWidth();
}
public static void main(String[] args) {
new Window(762,720, "BusyBee", new Game());
}
}
Window Class:
public class Window extends Game {
public Window(int w, int h, String title, Game game){
game.setPreferredSize(new Dimension(w,h));
game.setMaximumSize(new Dimension(w,h));
game.setMinimumSize(new Dimension(w,h));
JFrame frame = new JFrame(title);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setIconImage(background);
// starts the synchronized game start() method
game.start();
}
}
Upvotes: 0
Views: 234
Reputation: 694
I didn't have BufferedImageLoader, so I used the following code to load my image:
try {
background = ImageIO.read(new File("/Background.png"));
} catch (IOException e) {
}
Don't forget to draw your image in render!
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
// DRAW THE BACKGROUND!!
g.drawImage(background, 0, 0, null);
g.dispose();
bs.show();
}
Upvotes: 1