0

I'm trying to make a simple game engine in java, I'm currently working on rendering & displaying stuff, I made a display function to display the frames rendered by the camera in a JFrame

 if (cam != null)
      frame = cam.render();
    setVisible(true);
    ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
    ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
    BufferCapabilities bufCap =
      new BufferCapabilities(imgFrontBufCap, imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
    try {

      createBufferStrategy(2, bufCap);
    } catch (AWTException ex) {
      createBufferStrategy(2);
    }

    BufferStrategy bs = getBufferStrategy();
    Graphics g = bs.getDrawGraphics();
    if (frame != null)
      g.drawImage(frame, 0, 0, null);
    g.dispose();
    bs.show();

but It draws a frame one time that I got this exception & I can't find anything helpful in the internet so I asked a question here

Exception in thread "Display" java.lang.NullPointerException: Cannot invoke "java.awt.image.VolatileImage.getGraphics()" because "this.backBuffers[i]" is null
        at java.desktop/java.awt.Component$BltBufferStrategy.showSubRegion(Component.java:4508)
        at java.desktop/java.awt.Component$BltBufferStrategy.show(Component.java:4469)
        at com.physicsEngine.rendering.Displayer.display(Displayer.java:41)
        at com.physicsEngine.Game.display(Game.java:74)
        at com.physicsEngine.Game.run(Game.java:65)
        at com.physicsEngine.Game.run(Game.java:69)
        at com.physicsEngine.Game.run(Game.java:69)
        at com.physicsEngine.Game.run(Game.java:69)
        at com.physicsEngine.Game.run(Game.java:69)
        at com.physicsEngine.Game.run(Game.java:69)
        at com.physicsEngine.Game.run(Game.java:69)
  • Use a JFrame and a drawing JPanel. JPanel painting is already double-buffered. The Oracle tutorial, [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) will show you how. – Gilbert Le Blanc Jun 29 '21 at 01:11

0 Answers0