import java.awt.*; import java.awt.image.*; import java.awt.event.*; public class BufferStrategyTest2 { private GraphicsDevice device; private Frame frame; private static Color[] colors = new Color[] { Color.red, Color.blue, Color.green, Color.yellow, Color.cyan, Color.pink, Color.magenta, Color.orange, Color.gray, Color.lightGray, Color.darkGray, Color.white, Color.black}; private static DisplayMode[] DISPLAY_MODES = new DisplayMode[] { new DisplayMode(640, 480, 32, DisplayMode.REFRESH_RATE_UNKNOWN), new DisplayMode(640, 480, 24, DisplayMode.REFRESH_RATE_UNKNOWN), new DisplayMode(640, 480, 16, DisplayMode.REFRESH_RATE_UNKNOWN), new DisplayMode(640, 480, 8, DisplayMode.REFRESH_RATE_UNKNOWN) }; public BufferStrategyTest2(){ frame = initFullScreen(); } public void draw(int bufferSize){ Rectangle bounds = frame.getBounds(); int width = bounds.width; int height = bounds.height; try{ frame.createBufferStrategy(bufferSize); BufferStrategy bufferStrategy = frame.getBufferStrategy(); for(int j = 0 ; j < 10 ; j++){ for (int i = 0 ; i < bufferSize ; i++) { Graphics g = bufferStrategy.getDrawGraphics(); if(!bufferStrategy.contentsLost()){ g.setColor(colors[i]); g.fillRect(0, 0, width, height); g.setFont(new Font("Helvetica", Font.PLAIN, 64)); g.setColor(Color.black); g.drawString("Buffer No." + i, 20, 220); bufferStrategy.show(); g.dispose(); } try { Thread.sleep((int)200L); } catch (InterruptedException ex) {} } } }catch (IllegalStateException ex){ ex.printStackTrace(); } device.setFullScreenWindow(null); System.exit(0); } private Frame initFullScreen(){ Frame frame = null; try{ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); device = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = device.getDefaultConfiguration(); frame = new Frame(gc); frame.setIgnoreRepaint(true); frame.setUndecorated(true); device.setFullScreenWindow(frame); if (device.isDisplayChangeSupported()) { selectDisplayMode(device); } } catch (Exception e){ e.printStackTrace(); device.setFullScreenWindow(null); System.exit(0); } return frame; } private DisplayMode getBestDisplayMode(GraphicsDevice device) { for (int i = 0 ; i < DISPLAY_MODES.length ; i++) { DisplayMode[] modes = device.getDisplayModes(); for (int j = 0 ; j < modes.length ; j++) { if (modes[j].getWidth() == DISPLAY_MODES[i].getWidth() && modes[j].getHeight() == DISPLAY_MODES[i].getHeight() && modes[j].getBitDepth() == DISPLAY_MODES[i].getBitDepth() ) { return DISPLAY_MODES[i]; } } } return null; } private void selectDisplayMode(GraphicsDevice device) { if (device.isDisplayChangeSupported()) { DisplayMode best = getBestDisplayMode(device); if (best != null) { device.setDisplayMode(best); } } } public static void main(String[] args){ try{ new BufferStrategyTest2().draw(Integer.parseInt(args[0])); }catch(NumberFormatException ex){ ex.printStackTrace(); } } }