I am trying to convert a game using Canvasto using JFrame/JPanel.. Previously I had Game object Extend Canvas and implement Runnable
and then created a JFrame and passed it the canvas...
What it was before: (works)
public class Game extends Canvas implements Runnable{
private void render() {
// TODO Auto-generated method stub
BufferStrategy bs = this.getBufferStrategy();
if(bs==null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//////////////////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.drawImage(background,0,0,null);
p.render(g);
c.render(g);
//g.drawImage(player,100,100,this);
//////////////////////////////
g.dispose();
bs.show();
}
public static void main (String args[]){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
But I would like to instead do the graphics on a JPanel, instead of Canvas, and pass this to the JFrame.. but it doesn't work..
public class Game extends JPanel implements Runnable{
private void render() {
// TODO Auto-generated method stub
BufferStrategy bs = this.getBufferStrategy(); // error here
if(bs==null){
createBufferStrategy(3); //error here
return;
}
public static void main (String[] args){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
Exception in thread "Thread-1" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
Confused...