1

So after looking at multiple ways to paint or draw things onto the canvas/screen, I tested out my own way that seems to work, no problem. I simply just g.clearRect(0, 0, width, height) on an update method that is put in my game loop so that the game can refresh every frame, and the BufferStrategy will only show after all the items are refreshed fully, so wouldn't this be an okay strategy?

Here is the idea of the method that I use that uses a canvas's BufferStrategy.

public void update() {

    //Update all the entities that are on the screen, like their x and y, etc.
    player.update();

    g.clearRect(0, 0, width, height);

    // Draw the things I want here, such as player, etc.
    g.drawImage(image, player.getX(), player.getY(), null);

    bs.show();

}

Is this method inefficient somehow? I struggled a lot to find a method of displaying objects and such on screen, but I stumbled across this way which seems very easy for me.

  • Basically, yes, you have to, you have no choice. You have to picture a `BufferStrategy` like a deck of cards, you pull one card off the top, draw on it, push that to the screen, put it to the bottom of the deck, pull the next card off, draw on it, so on and so forth. What happens when you cycle through all the cards, the first card you started with still has what you painted on it (but nothing else), so by the time you get to this card, it's out-of-date with what else has been painted previously, so you have (no real) choice, but to clear and completely repaint the current state – MadProgrammer Jun 28 '18 at 04:34
  • This is further complicated by the fact that the "pages" are not guaranteed to be in any order, so you might start with page 1, get page 2, get page 3, but page 2 still been pushed to the hardware, so the next page might be 1, it's kind of the point of the buffering system - it will deliver the pages to the hardware in the correct order, so don't worry about that. One thing you should consider is "what is changing". If you have static content that doesn't change (or doesn't change often), it would be faster to paint that too a BufferedImage and simply paint that onto the pages – MadProgrammer Jun 28 '18 at 05:48

0 Answers0