0

I am programming a game and at the moment I use canvas to render the graphic stuff on it I am using a game loop to render ever 16 ms the player,the Map and so on new. I wanted to insert buttons on it for some functions like opening a questlog but there is no way to use normal buttons on canvas as far as I got it. So I asked my programming teacher and he told me to use JPanel instead of canvas so I can use buttons and render my graphics on it. But when I try to render on a JPanel in my gameloop the game is flickering. I think this happens because a JPanel don't have bufferedstrategie so I wanted to ask if it's possible to do buffering on JPanel like on a canvas?

alovaros
  • 476
  • 4
  • 23
  • `JPanel` is already double buffered, for [example](http://stackoverflow.com/a/3256941/230513); possible duplicate of [this](http://stackoverflow.com/q/3256269/230513). – trashgod Jan 11 '17 at 21:05
  • Hmm so the problem seems to be my gameloop? :D in this example I have to use the paintComponent method so I won't get around this right? ;/ – alovaros Jan 11 '17 at 21:07
  • *"if it's possible to do buffering on JPanel like on a canvas?"* in a roundabout way, yes, but you'll have to do all the work yourself, managing the buffers, flipping them etc. Basically, you've hit the core differences between a heavy weight (`Canvas`) and light weight (`JPanel`) component. Heavy weight components have their own native peer and have a more direct relationship to the underlying OS/hardware, whereas light weight components share a native peer (with a underlying heavy weight component). – MadProgrammer Jan 11 '17 at 21:07
  • The painting process between a `BufferedStrategy` and `JPanel` is also different, the `BufferedStrategy` uses a direct painting process (you control it directly), whereas `JPanel`'s painting process is controlled by the repaint manager, so you have less control over the process – MadProgrammer Jan 11 '17 at 21:09
  • You could also have a look at http://stackoverflow.com/questions/12175174/paintcomponent-vs-paint-and-jpanel-vs-canvas-in-a-paintbrush-type-gui/12175819#12175819 and http://stackoverflow.com/questions/17737555/how-java-graphics-repaint-method-actually-works/17737642#17737642 which go into more detail about the subject and [Passive vs. Active Rendering](http://docs.oracle.com/javase/tutorial/extra/fullscreen/rendering.html) – MadProgrammer Jan 11 '17 at 21:10
  • Oke @MadProgrammer Thanks a lot! so all in all I think it's easier for me to use canvas instead of JPanel and just do a workaround with the buttons :'D thank u – alovaros Jan 11 '17 at 21:15
  • 1
    @alovaros Just understand that `Canvas` and `JPanel` are using two different painting approaches that are incompatible with each other, in the end, if you want to continue using `Canvas`, you might need to create your own "buttons" which you can render and control your self, through your painting loop – MadProgrammer Jan 11 '17 at 21:19

0 Answers0