Note that there are some explanatory texts on larger screens.

plurals
  1. POBufferedStrategy in JPanel?
    primarykey
    data
    text
    <p>I am trying to convert a game using Canvasto using JFrame/JPanel.. Previously I had Game object Extend Canvas and implement Runnable</p> <p>and then created a JFrame and passed it the canvas...</p> <p>What it was before: (works)</p> <pre><code> 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(); } } </code></pre> <p>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..</p> <pre><code> 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) </code></pre> <p>Confused...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload