Results 1 to 3 of 3
  1. #1
    TobyLobster is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default BufferStrategy and white rows of pixels

    I'm trying to use a simple BufferStrategy to double buffer an Applet. It's supposed to just show a green rectangle as big as the Applet, but the top 5 rows of pixels are always coming out white.

    Do I need to use JFrames, Swing or other tech to make this work?

    Java Code:
    import java.applet.Applet;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Dimension;
    import java.awt.image.BufferStrategy;
    
    public class test extends Applet implements Runnable
    {
        private BufferStrategy bufferStrategy;
        private Canvas         canvas;
        private boolean        stopped = false; // True if applet destroyed
    
        public void init()
        {
            Thread t = new Thread(this);
            setIgnoreRepaint(true);
            t.start();
        }
    
        public void destroy()
        {
            stopped = true;
    
            // Allow Applet to destroy any resources
            super.destroy();
        }
    
        public void run()
        {
            canvas = new Canvas();
            canvas.setSize(new Dimension(getWidth(),getHeight()));
            add(canvas);
            canvas.createBufferStrategy(2);
            bufferStrategy = canvas.getBufferStrategy();
    
            //main loop
            while(!stopped)
            {
                // Draw green rectangle
                Graphics2D g2d = (Graphics2D)bufferStrategy.getDrawGraphics();
                g2d.setColor(Color.green);
                g2d.fillRect(0,0,getWidth(),getHeight());
                g2d.dispose();
    
                //Flip or show the back buffer
                if(!bufferStrategy.contentsLost())
                {
                    bufferStrategy.show();
                }
            }
        }
    }
    With simple HTML:
    Java Code:
    <html>
        <title>Test Page</title>
        <body bgcolor=#c0c0c0>
            <H2>Test Page</H2>
            <applet code=test.class width=360 height=234></applet>
        </body>
    </html>
    I get the same result when using appletviewer or using a browser to view the applet. I'm using javac 1.5.0_16 on MacOS X.

    Thanks for any advice.

  2. #2
    TobyLobster is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default JApplet version

    Answering my own question - I guess I have to use a JApplet then. JApplets have double buffering on by default, I think. This seems to work:

    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;
    
    public class test extends JApplet implements Runnable
    {
        private boolean        stopped = false; // True if applet destroyed
    
        public void paint(Graphics g)
        {
            if( (System.currentTimeMillis() % 1000) > 500 )
            {
                g.setColor(Color.green);
            }
            else
            {
                g.setColor(Color.blue);
            }
            g.fillRect(0,0,getWidth(),getHeight());
        }
    
        public void update(Graphics g)
        {
            paint(g);
        }
    
        public void start()
        {
            Thread t = new Thread(this);
            t.start();
        }
    
        public void stop()
        {
            stopped = true;
    
            // Allow JApplet to destroy any resources
            super.destroy();
        }
    
        public void run()
        {
            while(!stopped)
            {
                long startTime = System.currentTimeMillis(); 
    
                repaint();
    
                long stopTime = System.currentTimeMillis();
                long runTime  = stopTime - startTime;
                long waitTime = Math.max(1L, 100 - runTime);
      
                try{Thread.sleep(waitTime);}
                catch(InterruptedException e){}
            }
        }
    }
    Last edited by TobyLobster; 12-20-2008 at 11:46 AM.

  3. #3
    TobyLobster is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default Additional

    ... except that I needed to add a JPanel too, as the code above only double buffers correctly on Mac machines (ie. not on PCs). So much for Java's platform independence!

Similar Threads

  1. Modified Pixels
    By monkey04 in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 09:15 AM
  2. White Space Issue
    By sibythoma1984 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-22-2008, 01:05 PM
  3. Blurring pixels
    By tim in forum New To Java
    Replies: 0
    Last Post: 01-01-2008, 03:06 PM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM
  5. Deleting certain image pixels..
    By Brightside in forum New To Java
    Replies: 1
    Last Post: 05-22-2007, 10:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •