Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default Double buffer help needed

    My image keeps flashing when I try to move it, so I wan't to double buffer to eliminate that. I have read about a dozen tutorials I don't really understand it. I know the image is drawn off screen and brought back on, I just don't really understand how it's coded. So it would be great if someone could post an example code or explain it with bits of code. Thanks.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Swing is double buffered by default so it shouldn't be a problem.

  3. #3
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I don't think I'm using swing, I'm doing something like g.drawline(150,150,0,150);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    To see if you are using Swing vs AWT:
    Do any of your classes names begin with the letter J?
    Are any of your classes in the package javax.swing?

    g.drawLine doesn't show what class or package is being used.

    What does Google return? I get lots of examples with java double buffering example
    Last edited by Norm; 08-02-2010 at 07:59 PM.

  5. #5
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Well I get alot of double buffering examples on google too, just I don't really understand any of them in the first page or two. I believe I'm using AWT because in the beginning of my program I import java.awt. None of my classes begin with a J either. Thanks.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    class DoubleBufferedCanvas extends Canvas {

    public void update(Graphics g) {
    Graphics offgc;
    Image offscreen = null;
    Dimension d = size();

    // create the offscreen buffer and associated Graphics
    offscreen = createImage(d.width, d.height);
    offgc = offscreen.getGraphics();
    // clear the exposed area
    offgc.setColor(getBackground());
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground());
    // do normal redraw
    paint(offgc);
    // transfer offscreen to window
    g.drawImage(offscreen, 0, 0, this);
    }
    }

  7. #7
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    So would I just keep my stuff have already drawn without double buffering where it is?

  8. #8
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I tried it without moving my stuff around and putting that in and it still flickers. I don't think update is being called.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I don't think update is being called.
    Add println() statements to methods to allow them to tell you that they are being called.

  10. #10
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I don't have a compiler that accepts system.out.println. It won't tell me if method is working using the system.out.println.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What environment are you working in?
    If you get any errors, you need to post the full text here if you expect anyone to help you.

    If the statement: System.out.println("I am in methodX");
    is the first statement in methodX, then you'll know if the method is being called because you will see: 'I am in methodX' printed on the console.

  12. #12
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I put it like this:

    Java Code:
    public void update(Graphics g) { 
    System.out.println();
    Graphics offgc;
    Image offscreen = null;
    Dimension d = size();
    
    // create the offscreen buffer and associated Graphics
    offscreen = createImage(d.width, d.height);
    offgc = offscreen.getGraphics();
    // clear the exposed area
    offgc.setColor(getBackground());
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground());
    // do normal redraw
    paint(offgc);
    // transfer offscreen to window
    g.drawImage(offscreen, 0, 0, this);
    but I have no errors and the consol won't say anything.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Change this:
    System.out.println(); // print nothing
    to this:
    System.out.println("in update"); // print something

  14. #14
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I did that now and it still doesn't print anything.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    then you'll know if the method is being called because you will see:
    The method is NOT being called.

  16. #16
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    So then what do I do?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Do about what?

    I posted an example of double buffering.
    Somewhere in your code you build an image by drawing it bit by bit. You said that process was causing your image to flicker. To eliminate flicker, you use the double buffering technique: create an image in memory, draw on that image and THEN draw that image in one statement to where it is visible to the user.

  18. #18
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Can you tell me how I would use it in this code because it is really confusing me.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
       int sec = 45;
       int x = 1;
       int w = 120;
       int h = 150;
    
       public void init() {
    
          x = 1;
          setBackground( Color.black );
    
       }
    
       public void start() {
    
    
    	if (sec == 45) {
    	w = 120;
    	h = 150;
          	try 
       	{
        	Thread.sleep(1000);
        	} 
         	catch(InterruptedException e)
    	{
            e.printStackTrace();
            }
    	sec++;
          }
    	if (sec == 44) {
    	w = 119;
    	h = 147;
          	try 
       	{
        	Thread.sleep(1000);
        	} 
         	catch(InterruptedException e)
    	{
            e.printStackTrace();
            }
    	sec++;
          }
       }
    
       public void paint( Graphics g ) {
    	while (x > 0) {
    	g.setColor(Color.black);
    	g.fillRect(0, 0, 300, 300);
    	g.setColor(new Color(48, 203, 255));
          	g.drawLine(150, 150, w, h);
    	}
       }
    public void update(Graphics g) { 
    System.out.println("in update"); ;
    Graphics offgc;
    Image offscreen = null;
    Dimension d = size();
    
    // create the offscreen buffer and associated Graphics
    offscreen = createImage(d.width, d.height);
    offgc = offscreen.getGraphics();
    // clear the exposed area
    offgc.setColor(getBackground());
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground());
    // do normal redraw
    paint(offgc);
    // transfer offscreen to window
    g.drawImage(offscreen, 0, 0, this);
    }
    }

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    There are many unexplained statements in your code.

    Why do this:Thread.sleep(1000);
    What is the variable sec used for?

    Why are h and w set to those values?

    There is an endless loop in paint()

    I have no idea what your code is supposed to do.

  20. #20
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Its supposed to be a timer, at least the first two seconds of one. Thread.sleep(1000) makes a 1 second wait. Sec is used to determine where the second hand would be. h and w are set to those values to represent positions. The endless loop is supposed to make it so the clock keeps running. It's supposed to be an anolog clock.

Page 1 of 2 12 LastLast

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  2. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  3. Need Help: Double Buffer
    By rukawa527 in forum AWT / Swing
    Replies: 1
    Last Post: 02-09-2009, 12:39 AM
  4. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  5. Can't DOUBLE BUFFER images
    By hunterbdb in forum Java 2D
    Replies: 7
    Last Post: 11-14-2008, 08:53 PM

Posting Permissions

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