BufferStrategy中没有显示在java.awt.Frame中 [英] Bufferstrategy not showing on java.awt.Frame

查看:157
本文介绍了BufferStrategy中没有显示在java.awt.Frame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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中也是不为null。

The frame is showing, but the rectangle (red) with the requested coordinates and size are not showing on the frame. I have no idea why it's not drawing. The console is not throwing any errors. The BufferStrategy is also not null.

推荐答案

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

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

}

有一个更好的解决办法是使用画布键,将其添加到框架和使用它的 BufferStrategy使用,这将美元,绘画p $ pvent你下框的边框并为您提供实际的可视空间,更好的主意,你可以画

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

这篇关于BufferStrategy中没有显示在java.awt.Frame中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆