Can anyone tell me why this code puts a white rectangle on the screen as opposed to a red one? There is something simple that I’m missing.
Also, is my second call to getDefaultConfiguration necessary?
public FullScreenTest3(int numBuffers, GraphicsDevice device) {
try {
GraphicsConfiguration gc = device.getDefaultConfiguration();
mainFrame = new Frame(gc);
mainFrame.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
char keyChar = e.getKeyChar();
if (keyChar=='q' || keyChar=='Q') {
quitFlag = true;
}
} });
mainFrame.setUndecorated(true);
mainFrame.setIgnoreRepaint(true);
device.setFullScreenWindow(mainFrame);
if (device.isDisplayChangeSupported()) {
chooseBestDisplayMode(device);
}
gc = device.getDefaultConfiguration();
myImage = gc.createCompatibleImage(16, 16);
myImage.getGraphics().setColor(Color.red);
myImage.getGraphics().fillRect(0,0,16,16);
/*
** added this to avoid race condition
*/
Thread.sleep(1000);
mainFrame.createBufferStrategy(numBuffers);
BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
while (quitFlag == false) {
Graphics g = bufferStrategy.getDrawGraphics();
if (!bufferStrategy.contentsLost()) {
g.setColor(Color.black);
g.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
g.drawImage(myImage, 200, 200, null);
bufferStrategy.show();
g.dispose();
}
try {
Thread.sleep(lag);
} catch (InterruptedException e) {}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
device.setFullScreenWindow(null);
}
}