gpt4 book ai didi

java - java中使用BufferStrategy时如何避免出现黑线

转载 作者:行者123 更新时间:2023-12-02 11:59:38 26 4
gpt4 key购买 nike

遵循 2015 年 11 月发布的指南。此时我已经逐字复制了他的代码,但它仍然对我不起作用。有什么东西被弃用了吗?

我有 3 个缓冲区(称它们为 1,2 和 3)。当 2 和 3 被绘制到屏幕上时,它们在屏幕的顶部和左侧有黑线。相同的代码可以在两个缓冲区中正常工作。

错误片段:https://gfycat.com/gifs/detail/GraveCompetentArmyworm

package field;

import javax.swing.JFrame;

import java.awt.*;
import java.awt.image.BufferStrategy;

public class Main extends JFrame{

private Canvas canvas=new Canvas();

public Main() {
setDefaultCloseOperation(EXIT_ON_CLOSE);

setBounds(0,0,1000,1000);

setLocationRelativeTo(null);

add(canvas);
setVisible(true);

canvas.createBufferStrategy(3);
BufferStrategy buffert = canvas.getBufferStrategy();

int p=0;
int ap=0;
while(p<1000) {
if (ap==100){
p++;
ap=0;
}
ap++;
buffert=canvas.getBufferStrategy();
Graphics g = buffert.getDrawGraphics();
super.paint(g);
g.setColor(Color.GREEN);

g.fillOval(p+100, 200, 50, 50);

buffert.show();
}
}

// public void paint(Graphics graphics) {
// super.paint(graphics);
// graphics.setColor(Color.RED);
// graphics.fillOval(100, 100, 100, 100);
//
// }



public static void main(String[] args){


new Main();




}

}

最佳答案

您需要阅读the JavaDocs for BufferStrategy Full-Screen Exclusive Mode API ,其中有许多重要的教程和示例BufferStrategy

一个BufferStrategy是一种执行“翻页”的手段,它独立于常规绘画系统。这为您提供了对绘画过程的“主动”控制。每个缓冲区都会在屏幕外更新,并在准备好时推送到屏幕上。

这通常不涉及组件自己的绘画系统,目的是避免它。

这意味着您不应该调用 super.paint(g)关于JFramecanvas.paint 。事实上,作为一般规则,您永远不应该调用 paint手动。

每次您想要更新缓冲区时,您都需要“准备”它。这通常意味着用一些基色填充它

因此,根据 JavaDocs 中的示例,您可以执行以下操作...

// 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

// Determine the current width and height of the
// output
int width = ...;
int height = ...l
// to make sure the strategy is validated
Graphics graphics = strategy.getDrawGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
// 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();

现在,就我个人而言,我更愿意使用 Canvas作为基础,因为它提供了更可重用的解决方案,并且更容易确定尺寸

关于java - java中使用BufferStrategy时如何避免出现黑线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47361659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com