OK I have it working by overriding paint() to call the rendering method but 
this seems like a bit of wasted effort, at least on some occasions.  Anyway, 
it's working but I have noticed that almost every render has to happen twice 
because the strategy.contentsRestored() method returns true.

Is this normal?  The following is a cut-down version of the code which exhibits 
this problem.

[code]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class StrategyTest extends JFrame {

        private class MyPanel extends JPanel {

                public MyPanel() {
                        this.setIgnoreRepaint(true);
                        this.addComponentListener(new ComponentAdapter() {

                                @Override
                                public void componentResized(final 
ComponentEvent e) {
                                        StrategyTest.this.render();
                                }
                        });
                }
        }

        private final BufferStrategy strategy;

        private Graphics2D g2d;

        public StrategyTest() {
                this.setLayout(new BorderLayout());
                this.add(new MyPanel(), BorderLayout.CENTER);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.pack();
                this.setSize(1024, 768);
                this.setIgnoreRepaint(true);
                this.createBufferStrategy(2);
                this.strategy = this.getBufferStrategy();
        }

        public static void main(final String[] args) {
                new StrategyTest().setVisible(true);
        }

        @Override
        public void paint(final Graphics g) {
                this.render();
        }

        public void render() {
                if (this.strategy == null) {
                        return;
                }
                do {
                        do {
                                this.g2d = 
(Graphics2D)this.strategy.getDrawGraphics();
                                this.drawBox();
                                this.g2d.dispose();
                                if (!this.strategy.contentsRestored()) {
                                        break;
                                } else {
                                        System.out.println("### BUFFER CONTENTS 
RESTORED ###");
                                }
                        } while (true);
                        this.strategy.show();
                        if (!this.strategy.contentsLost()) {
                                break;
                        } else {
                                System.out.println("### BUFFER CONTENTS LOST 
###");
                        }
                } while (true);
        }

        @Override
        public void update(final Graphics g) {
                this.paint(g);
        }

        private void drawBox() {
                this.g2d.setColor(Color.RED);
                this.g2d.fillRect(20, 20, this.getWidth() - 40, 
this.getHeight() - 40);
        }
}
[/code]

-- 
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
[EMAIL PROTECTED]
[Replace the "SixFour" with numbers to email me]
[Message sent by forum member 'qu0ll' (qu0ll)]

http://forums.java.net/jive/thread.jspa?messageID=288417

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to