0

So I am creating a 2D game in Java Swing and awt. I want to use graphics object as least as i can, basically creating an image buffer and writing to it and sending it to the canvas to get rendered. I have limited performance because i am on a raspberry pi, and i am using java and its libraries because most game libraries are not for Raspberry Pi and Java makes it easy for me. Thanks for any answers.

1 Answers1

0

Before you start running off re-inventing the wheel (and probably the whole car), you should by investigating something BufferStrategy, which provides direct drawing capabilities to a "paged flipping" algorithm.

BufferStrategy is limited, as you can't mix it with Swing components, as Swing uses a different rendering approach.

You "could" also use a couple of BufferedImages, which act as pages, and manually swap them, which is essentially what BufferStrategy does any way, but if you're using Swing, Swing is already double buffered, so you might be just spinning wheels for the want to look cool.

I would "suggest" starting out with something simple and seeing where it takes you, don't try to optimise the solution until you actually start having problems.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Which method do you recommend? – DinamicDigital Nov 29 '18 at 21:30
  • @DinamicDigital That depends, how much work do you want to go to? I'd start with simply using Swing out of the box, while it uses a passive rending engine, it's the simplest place to start, and you can make use of a `BufferedImage` as the primary backing buffer if you wanted to and simply paint it directly onto something like a `JPanel`. If you find that that gives you issues, try looking at using a `BufferStrategy`. – MadProgrammer Nov 29 '18 at 21:43
  • @DinamicDigital But there is a lot more going on then just the rendering, good resource management is also important - Have a look at [this example](https://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184) for some more details – MadProgrammer Nov 29 '18 at 21:43