I would revert to the use of the Java Docs recommended do/while loops.
They work well.  And you will have to figure out when to call your
rendering method.

I use MemoryImageSource and render with the do/while loops
in my newPixels() method.  I use paint() with the do/while
loops for expose events.  And I toggle setIgnoreRepaint()
as needed.

The mods to you've made to the do/while loops look unnecessary.
But our apps are different.

You are on the right track.  Don't forget about overriding update().

[EMAIL PROTECTED] wrote:
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]


===========================================================================
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