【发布时间】:2012-03-15 12:39:41
【问题描述】:
这是我第一次尝试使用BufferStrategy,非常感谢一些提示。
1) 为什么在下面的代码中,getSize() 在您调整窗口大小之前返回尺寸为 0?如何立即检测窗口的大小?
2) 为什么getSize() 返回的东西不是窗口的完整尺寸? IE 为什么右下角有一条无黑条?
3) 调整窗口大小时有没有办法消除闪烁?
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BSTest extends JFrame {
BufferStrategy bs;
DrawPanel panel = new DrawPanel();
public BSTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,420);
setLocationRelativeTo(null);
setIgnoreRepaint(true);
setVisible(true);
createBufferStrategy(2);
bs = getBufferStrategy();
panel.setIgnoreRepaint(true);
add(panel);
panel.drawStuff();
}
public class DrawPanel extends JPanel {
public void drawStuff() {
while(true) {
try {
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
g.setColor(Color.BLACK);
System.out.println("W:"+getSize().width+", H:"+getSize().height);
g.fillRect(0,0,getSize().width,getSize().height);
bs.show();
g.dispose();
Thread.sleep(20);
} catch (Exception e) { System.exit(0); }
}
}
}
public static void main(String[] args) {
BSTest bst = new BSTest();
}
}
【问题讨论】:
标签: java swing doublebuffered