Drawing to off-screen buffer

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);
        }
    }

im not sure why the colors wrong.
id get rid of myImage, and do all the drawing in the while{} straight into the bufferedStategey.getDrawGraphics().

well, this is just an example that I cooked up. I’m trying to pre-render some images to blit during the display process, and this was just a test to figure out how to do it.

There must be something simple that I’m missing here!

The reason (I think) you are getting a white rectangle is because you are calling getGraphics() twice. The first one returns a Graphics object and you set its draw color to red. Then you get another Graphics object, whose default colour is white which you actually draw to screen. To fix this try:


Graphics g = myImage.getGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 16, 16);

I hope that is correct and makes sense to you :slight_smile:

Stu

Yes, I believe you are correct. Thanks for replying!