- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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。
最佳答案
因此,有两件事会跳出来,一是,getBufferStrategy
将返回要使用的下一个缓冲区,因此您要更改不同缓冲区的属性。
第二,在缓冲区上绘制和 native 对等点再次绘制窗口之间存在竞争条件,从而覆盖缓冲区的内容。
这个示例基本上设置了一个无限更新循环,它会重新绘制缓冲区,您可以减慢延迟,并且可以看到它从一种状态更改为另一种状态
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
,这将阻止您在框架边框下绘画并为您提供更好地了解您可以绘制的实际可视空间
关于java - Bufferstrategy 未显示在 java.awt.Frame 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689176/
从Canvas可以看出类和 JFrame类,与BufferStrategy(Canvas 和JFrame)相关的方法不来自同一个父类。例如 JFrame 的 createBufferStrategy
经过几个小时的尝试解决这个问题后,我被难住了。我对 Java 有点陌生,可以使用一些帮助。 堆栈跟踪: java.lang.IllegalStateException: Component must
我尝试使用graphics2D和JFrame在Java中制作一个简单的GUI。我在 JFrame 上添加了背景颜色,在 initWindow() 方法中输入 this.setBackground(ne
我试图了解 BufferStrategy 是如何工作的。我制作了一个简单的应用程序,每 60 帧每秒一次又一次地绘制一些 Sprite 对象。我可以看到 Canvas 上的图像,但由于某种原因它们在闪
我正在尝试使用 JFrame 和 canvas 为游戏构建简单的 gui。 Window 是一个扩展 JFrame 类的类,我使用 fillRect 方法来填充黑色矩形。每次我运行程序时,框架窗口都不
首先,我知道以前曾有人问过类似的问题,但似乎没有答案可以解决我的问题。 我正在开发一个小游戏,由于某种原因,每当我尝试创建新的缓冲区策略时,java都会返回IllegalStateException。
我的桌面应用程序滞后。我认为 java.awt.image.BufferStrategy 中有问题。 private void render() { BufferStrategy bs
我是 java 的新手。我想做一个游戏。经过大量研究,我无法理解 bufferstrategy 是如何工作的。我知道基础知识..它创建了一个屏幕外图像,您可以稍后将其放入您的 Windows 对象中.
我用 BufferStrategy 等初始化一个扩展的 jFrame,在屏幕上得到一个漂亮的动画圆圈。我设置了一个关键监听器(在更新绘制线程之外),它告诉更新绘制线程在全屏独占模式之间切换,在更改完成
你好, 我尝试在 BufferStrategy 上使用其 Graphics 绘制图片。图片有透明背景,如果我在屏幕上绘制它,透明区域会变成黑色。 蓝色的东西是我想画的图像,但没有黑色部分(在原始图片中
当我试图弄清楚如何使用缓冲策略时,总体上只是改进我编写代码和清理东西的方式。当我运行以下代码时,在“createBufferStrategy(3)” 时出现错误 package Game1Te
所以我正在制作一个游戏,并且我的主游戏应用程序运行良好。问题是,当我尝试通过菜单(例如使用按钮 ActionEvent)启动游戏时,我的游戏似乎无法检测到提供给它的 KeyEvent。所以我决定制作一
我有一个 JFrame,我在其中添加了一个 JPanel。我正在做一些动画,所以我实现了 BufferStrategy 来进行渲染。我还有一个渲染循环,以使其在运行时保持渲染。 如果我像平常一样运行程
我是 Java 初学者,在扩展 JPanel 但不扩展 canvas 的类中创建 bufferstaregy 时遇到困难。有人可以在这里展示如何添加缓冲策略吗?我编写了非常简化的代码来说明我的问题。我
BufferStrategy 是 AWT 的一部分,通常与 Canvas 类一起使用,但它是否也可以与 Swing UI 结合使用而不会导致任何窗口显示问题,或者在这种情况下我应该更好地使用纯 AWT
我正在尝试使用 Java2D 制作一个简单的游戏以获得最大的兼容性。它在 Mac OS X Yosemite 上的 Java 8 下工作得很好,但当我在 Windows 7 下尝试相同的代码时,它就不
所以我最近一直在做一个小游戏,但遇到了一个奇怪的问题。虽然游戏在我不使用时运行得非常完美game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
Graphics g = bs.getDrawGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()
我正在尝试使用 BufferStrategy 和 Graphics2D 渲染图像。代码运行良好,但图像闪烁。在我用 Graphics2D 测试它之前,我只尝试了 Graphics,并且 Frame 疯
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 5 年前。 我一直在尝试制作 3d 游戏,但
我是一名优秀的程序员,十分优秀!