这是我第一次尝试使用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();
}
}