【问题标题】:Bufferstrategy not showing on java.awt.Frame缓冲区策略未显示在 java.awt.Frame 上
【发布时间】:2016-01-09 02:45:44
【问题描述】:
public void configure() {
    Frame frame = ctx.getFrame();
    frame.setTitle("AstroCycles | By: Carlos Aviles");
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setFocusable(true);
    frame.setSize(WIDTH, HEIGHT);
    frame.addKeyListener(ctx.getKeyEventDispatcher());
    frame.addWindowListener(ctx.getWindowEventDispatcher());
    frame.addMouseListener(ctx.getMouseEventDispatcher());
    frame.setVisible(true);
    frame.createBufferStrategy(3);
    frame.getBufferStrategy().getDrawGraphics().setColor(Color.RED);
    frame.getBufferStrategy().getDrawGraphics().fillRect(0, 0, 75, 75);
    frame.getBufferStrategy().getDrawGraphics().dispose();
    frame.getBufferStrategy().show();
}

框架正在显示,但具有请求坐标和大小的矩形(红色)未显示在框架上。我不知道为什么它不画。控制台没有抛出任何错误。 BufferStrategy 也不为空。

【问题讨论】:

  • getDrawGraphics 每次都会返回一个新缓冲区,因此您首先在一个缓冲区上 setColor,在另一个缓冲区上 fillRect 然后在另一个缓冲区上 show...
  • 我不知道。谢谢。

标签: java awt


【解决方案1】:

所以,有两件事跳出来,一,getBufferStrategy 将返回下一个要使用的缓冲区,因此您正在更改不同缓冲区的属性。

第二,在缓冲区上绘图和本地对等点再次绘制窗口之间存在竞争条件,覆盖缓冲区的内容。

这个例子基本上设置了一个无限更新循环来重绘缓冲区,你可以减慢延迟,你可以看到它从一种状态变为另一种状态

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);
    }

}

更好的解决方案是使用Canvas 并将其添加到框架中并使用它的BufferStrategy,这将防止您在框架的边框下进行绘画,并让您更好地了解您的实际可视空间会画画

【讨论】:

  • 我尝试使用画布(将它与它自己的类一起使用),但通用异常“组件必须有一个有效的对等点”不断发生。在将框架的可见性设置为 true 并添加 Canvas 后,我将创建画布。
  • 先添加Canvas,然后使窗口可见,然后尝试访问BufferStrategy,别忘了Canvas上的createBufferStrategy;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多