0

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...

user2556304
  • 159
  • 2
  • 15
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Jul 08 '13 at 19:07

1 Answers1

1

The code you have shouldn't even compile. JPanel has no public getBufferStrategy() method. So you should have gotten a compile error on this line:

BufferStrategy bs = this.getBufferStrategy();

In Swing it is more typical to use BufferStrategy at the level of the JFrame. I think it may be the case that only heavyweight components can use BufferStrategy.

Russell Zahniser
  • 16,188
  • 39
  • 30
  • +1 `BufferStrategy` is a way of abstracting the capabilities found on disparate host platforms. Absent contrary [profile](http://stackoverflow.com/q/2064427/230513) evidence, I'd rely on the strategies inherited by `JComponent`. – trashgod Jul 08 '13 at 23:24