Bufferstrategy not showing on java.awt.Frame

后端 未结 1 758
一向
一向 2020-12-22 12:15
public void configure() {
    Frame frame = ctx.getFrame();
    frame.setTitle(\"AstroCycles | By: Carlos Aviles\");
    frame.setResizable(false);
    frame.setLoca         


        
相关标签:
1条回答
  • 2020-12-22 12:32

    So, two things jump out, one, getBufferStrategy will return the next buffer to be used, so you are changing properties on different buffers.

    Two, there is a race condition between you drawing on the buffer and the window been painted again by the native peer, overwriting your buffer's contents.

    This example basically sets up a infinite update loop which repaints the buffer, you can slow down the delay and you can see it change from one state to another

    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Test {
    
        public static void main(String[] args) {
            Frame frame = new Frame();
            frame.setTitle("AstroCycles | By: Carlos Aviles");
            frame.setLocationRelativeTo(null);
            frame.setFocusable(true);
            frame.setSize(100, 100);
            frame.setBackground(Color.BLUE);
            frame.setVisible(true);
            frame.createBufferStrategy(3);
    
            do {
                BufferStrategy bs = frame.getBufferStrategy();
                while (bs == null) {
                    System.out.println("buffer");
                    bs = frame.getBufferStrategy();
                }
                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
                        System.out.println("draw");
                        Graphics graphics = bs.getDrawGraphics();
    
                        // Render to graphics
                        // ...
                        graphics.setColor(Color.RED);
                        graphics.fillRect(0, 0, 100, 100);
                        // Dispose the graphics
                        graphics.dispose();
    
                        // Repeat the rendering if the drawing buffer contents
                        // were restored
                    } while (bs.contentsRestored());
    
                    System.out.println("show");
                    // Display the buffer
                    bs.show();
    
                    // Repeat the rendering if the drawing buffer was lost
                } while (bs.contentsLost());
                System.out.println("done");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            } while (true);
        }
    
    }
    

    A better solution would be to use a Canvas and add this to the frame and use it's BufferStrategy, this will prevent you from painting under the frame's borders and provide you with a better idea of the actual viewable space you can paint

    0 讨论(0)
提交回复
热议问题