0
\$\begingroup\$

So, I'm making a game in Java, I'm using the BufferStrategy and Graphics class to render onto the screen.

But sometimes, when I run the game nothing is rendered onto the screen. But as soon as I resize the window, everything gets rendered fine.

At first I thought it had something to do with the BufferStrategy and graphics class, but when I printed those classes in the console after the initialization, they have the same values when the game is rendering fine and when the bug occurs.

I'm confused, is there a fix for this without altering a lot of code?

I'm creating my BufferStrategy as :

canvas.createBufferStrategy(3);

bs = canvas.getBufferStrategy();
g = bs.getDrawGraphics();

I'm not sure if the bug is occuring there though...

\$\endgroup\$
1
\$\begingroup\$

well im not sure due you lack information, like what are you doing in your update method but i will try to reply.

two things may happen here: 1.- bufferstrategy dont render anything until the memory is ready and an instance is created, in this case if createBufferStrategy is not ready will return null, what im doing below is to check again when its ready and if is, create BufferStrategy for the particular Canvas.

protected final Graphics getGraphicsBufferStrategy()
    {
        BufferStrategy bs = this.getBufferStrategy();
        if( bs == null )
        {
            this.createBufferStrategy( BUFFER_NUMBER );
            return null;
        }
         Graphics g = bs.getDrawGraphics();   
         return g;

    }//

2.-when you use bufferstrategy you dont render anything until you flush all the data to screen, in this case you have to call lastly BufferStrategy.show();

i leave oracle example of bufferstrategy that you can find in this link // Check the capabilities of the GraphicsConfiguration ...

 // Create our component
 Window w = new Window(gc);

 // Show our window
 w.setVisible(true);

 // Create a general double-buffering strategy
 w.createBufferStrategy(2);
 BufferStrategy strategy = w.getBufferStrategy();

 // Main loop
 while (!done) {
     // Prepare for rendering the next frame
     // ...

     // Render single frame
     do {
         // The following loop ensures that the contents of the drawing buffer
         // are consistent in case the underlying surface was recreated
         do {
             // Get a new graphics context every time through the loop
             // to make sure the strategy is validated
             Graphics graphics = strategy.getDrawGraphics();

             // Render to graphics
             // ...

             // Dispose the graphics
             graphics.dispose();

             // Repeat the rendering if the drawing buffer contents
             // were restored
         } while (strategy.contentsRestored());

         // Display the buffer
         strategy.show();

         // Repeat the rendering if the drawing buffer was lost
     } while (strategy.contentsLost());
 }

 // Dispose the window
 w.setVisible(false);
 w.dispose();
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Well, I came up with a cheap fix, which was basically resizing the window by 1 pixel and then back to original size within the code itself after all initialization is done.

But I still would like if someone let me know the reason as to why this is occurring.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.