📄 e602. double-buffering in full-screen mode.txt
字号:
Page-flipping, if supported, is the fastest way to copy a back buffer to the screen. This example demonstrates how to implement double-buffering using page flipping in full-screen mode.
// Determine if page flipping is supported
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferCapabilities bufCap = gc.getBufferCapabilities();
boolean page = bufCap.isPageFlipping();
if (page) {
// Page flipping is supported
} else {
// Page flipping is not supported
}
// Create a window for full-screen mode
Frame frame = new Frame(gd.getDefaultConfiguration());
Window win = new Window(frame);
// Configure the window so that a mouse click will exit full-screen mode
win.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
// Exit full-screen mode
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
gd.setFullScreenWindow(null);
}
});
try {
// Enter full-screen mode
gd.setFullScreenWindow(win);
win.requestFocus();
// Create the back buffer
int numBuffers = 2; // Includes front buffer
win.createBufferStrategy(numBuffers);
// Determine the state of a back buffer after it has been displayed on the screen.
// This information is used to optimize performance. For example, if your application
// needs to initialize a back buffer with a background color, there is
// no need to do so if the flip contents is BACKGROUND.
BufferStrategy strategy = win.getBufferStrategy();
bufCap = strategy.getCapabilities();
BufferCapabilities.FlipContents flipContents = bufCap.getFlipContents();
if (flipContents.equals(BufferCapabilities.FlipContents.UNDEFINED)) {
// The contents is unknown after a flip
} else if (flipContents.equals(BufferCapabilities.FlipContents.BACKGROUND)) {
// The contents cleared to the component's background color after a flip
} else if (flipContents.equals(BufferCapabilities.FlipContents.PRIOR)) {
// The contents is the contents of the front buffer just before the flip
} else if (flipContents.equals(BufferCapabilities.FlipContents.COPIED)) {
// The contents is identical to the contents just pushed to the
// front buffer after a flip
}
// Draw loop
while (true) {
// Get screen size
int screenWidth = win.getWidth();
int screenHeight = win.getHeight();
// Get graphics context for drawing to the window
Graphics g = strategy.getDrawGraphics();
if (!flipContents.equals(BufferCapabilities.FlipContents.BACKGROUND)) {
// Clear background
g.setColor(Color.white);
g.fillRect(0, 0, screenWidth, screenHeight);
}
// Draw shapes and images...
// Done drawing
g.dispose();
// Flip the back buffer to the screen
strategy.show();
}
} catch (Throwable e) {
// Process exception...
} finally {
gd.setFullScreenWindow(null);
}
Related Examples
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -