gpt4 book ai didi

Java swing双缓冲

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:31:56 25 4
gpt4 key购买 nike

我刚开始使用双缓冲,一切正常,直到我想在屏幕上添加一个 JScrollPane,以便稍后可以进行一些相机移动。除了 JScrollPane 的 ScrollBars 之外,一切都很好(我的 Sprite )。我希望他们能被展示出来!

但是,如果我调整窗口大小,滚动条会闪烁,所以我知道它们在那里!如果我足够快,我什至可以使用它们。他们为什么不出现在渲染中? :(

这是问题的 SSCCE:

public class BufferStrategyDemo extends JFrame
{
private BufferStrategy bufferStrategy;
private JPanel gameField;
private JScrollPane scroll;

public BufferStrategyDemo()
{

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setPreferredSize(new Dimension(800, 600));
this.pack();
this.createBufferStrategy(2);

this.gameField = new JPanel();
this.gameField.setPreferredSize(new Dimension( 1400, 600 ));

this.scroll = new JScrollPane( gameField );
this.add( scroll, BorderLayout.CENTER );

this.setVisible( true );
this.bufferStrategy = this.getBufferStrategy();

setupGameFieldContent();

new Renderer( this, scroll , bufferStrategy ).start();
}

// Add some contents to gameField that shows up fine
private void setupGameFieldContent()
{
// For ( all my sprites )
// gameField.add( sprite );

}

private class Renderer extends Thread
{
private BufferStrategy bufferStrategy;
private JFrame window;
private JScrollPane scroll;
private Image imageOfGameField;

public Renderer( JFrame window, JScrollPane scroll, BufferStrategy bufferStrategy )
{
this.bufferStrategy = bufferStrategy;
this.window = window;
this.scroll = scroll;
}

public void run()
{
while ( true )
{
Graphics g = null;
try
{
g = bufferStrategy.getDrawGraphics();
drawSprites(g);
}
finally { g.dispose(); }

bufferStrategy.show(); // Shows everything except the scrollbars..
Toolkit.getDefaultToolkit().sync();

try { Thread.sleep( 1000/60 ) ; }
catch(InterruptedException ie) {}
}
}

private void drawSprites( Graphics g )
{
Graphics2D g2d=(Graphics2D)g;

//Also tried using the JPanels (gameField) createImage with same result
imageOfGameField = this.scroll.createImage(800, 600 );
Graphics screen = (Graphics2D)imageOfGameField.getGraphics();

/**
* For all my sprites, draw on the image
for ( Sprite current : sprites )
screen.drawImage( current.getImage(), current.getX(), current.getY(), current.getWidth() , current.getHeight(), null);
*/

g2d.drawImage( imageOfGameField, 0, 0 , null );

}
}
}

最佳答案

绘图是在整个帧区域上执行的,而不是在 gameField 上执行的。实际上,临时 JScrollPane 出现的唯一原因是它在鼠标拖动处理程序的某处调用了 paintImmediately

首先想到的是将 JPanel 替换为 Canvas,如 I_Love_Thinking 所写,然后从该 Canvas 实例中获取 bufferStategy 并用它来渲染。但不幸的是,它没有按预期工作。轻量级 JScrollPane 无法正常驱动重量级 Canvas。 Canvas 拒绝在区域 (0, 0, viewport_width, viewport_height) 之外绘制内容。这是众所周知的bug它不会修复。混合使用重量级组件和轻量级组件是个坏主意。

用重量级替换JScrollPane ScrollPane似乎工作。几乎。在某些情况下,滚动条上有明显的渲染瑕疵。

至此,我决定不再撞墙,放弃。滚动 Pane 是许多问题的根源,不值得用于延迟滚动。现在是从另一个 View 滚动查看的时候了。 Canvas 不是游戏领域本身,而是我们用来观察游戏世界的窗口。这是 gamedev 中众所周知的渲染策略。 Canvas 的大小与可观察区域完全相同。当需要滚动时,我们只是用一些偏移来渲染世界。 offset 的值可能取自一些外部滚动条。

我建议接下来的更改:

  1. 扔掉JScrollPane
  2. 直接将 Canvas 添加到框架。
  3. 在 Canvas 下添加单个水平 JScrollBar
  4. 使用滚动条的值来移动渲染。

该示例基于您的代码。此实现不考虑框架的大小调整。在现实生活中,有些地方需要与视口(viewport)的大小同步(滚动 Pane 之前为我们所做的)。此外,示例违反了 Swing 线程规则并从渲染线程读取滚动条的值。

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JScrollBar;

public class BufferStrategyDemo extends JFrame {
private BufferStrategy bufferStrategy;
private Canvas gameField;
private JScrollBar scroll;

public BufferStrategyDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().setPreferredSize(new Dimension(800, 600));

gameField = new Canvas();
gameField.setIgnoreRepaint(true);
gameField.setPreferredSize(new Dimension(800, 580));
getContentPane().add(gameField, BorderLayout.CENTER);

scroll = new JScrollBar(JScrollBar.HORIZONTAL);
scroll.setPreferredSize(new Dimension(800, 20));
scroll.setMaximum(1400 - 800); // image width - viewport width
getContentPane().add(scroll, BorderLayout.SOUTH);

this.pack();

gameField.createBufferStrategy(2);
bufferStrategy = gameField.getBufferStrategy();

new Renderer().start();
}

private class Renderer extends Thread {
private BufferedImage imageOfGameField;

public Renderer() {
// NOTE: image size is fixed now, but better to bind image size to the size of viewport
imageOfGameField = new BufferedImage(1400, 580, BufferedImage.TYPE_INT_ARGB);
}

public void run() {
while (true) {
Graphics g = null;
try {
g = bufferStrategy.getDrawGraphics();
drawSprites(g);
} finally {
g.dispose();
}

bufferStrategy.show();
Toolkit.getDefaultToolkit().sync();

try {
Thread.sleep(1000 / 60);
} catch (InterruptedException ie) {
}
}
}

private void drawSprites(Graphics g) {
Graphics2D g2d = (Graphics2D) g;

Graphics g2d2 = imageOfGameField.createGraphics();
g2d2.setColor(Color.YELLOW); // clear background
g2d2.fillRect(0, 0, 1400, 580); // again, fixed width/height only for SSCCE
g2d2.setColor(Color.BLACK);

int shift = -scroll.getValue(); // here it is - get shift value

g2d2.fillRect(100 + shift, 100, 20, 20); // i am ugly black sprite
g2d2.fillRect(900 + shift, 100, 20, 20); // i am other black sprite
// located outside of default view

g2d.drawImage(imageOfGameField, 0, 0, null);

g2d2.dispose();
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
BufferStrategyDemo mf = new BufferStrategyDemo();
mf.setVisible(true);
}
});
}
}

another example ,主要基于 Java Games: Active Rendering 中的代码文章。它与 JScrollBar 的值和 Canvas 的大小正确同步。此外,它直接绘制到 Graphics,从 BufferStrategy 实例(而不是中间 BuffereImage 对象)获得。结果是这样的:

Active Rendering Demo

关于Java swing双缓冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9554731/

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