Results 1 to 20 of 23
Thread: Double buffer help needed
- 08-02-2010, 05:40 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
- 08-02-2010, 05:54 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Swing is double buffered by default so it shouldn't be a problem.
- 08-02-2010, 07:35 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
I don't think I'm using swing, I'm doing something like g.drawline(150,150,0,150);
- 08-02-2010, 07:55 PM #4
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 exampleLast edited by Norm; 08-02-2010 at 07:59 PM.
- 08-02-2010, 08:07 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
- 08-02-2010, 08:15 PM #6
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);
}
}
- 08-02-2010, 08:18 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
So would I just keep my stuff have already drawn without double buffering where it is?
- 08-02-2010, 08:21 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
I tried it without moving my stuff around and putting that in and it still flickers. I don't think update is being called.
- 08-02-2010, 08:25 PM #9
Add println() statements to methods to allow them to tell you that they are being called.I don't think update is being called.
- 08-02-2010, 08:47 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
- 08-02-2010, 08:52 PM #11
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.
- 08-02-2010, 08:58 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
I put it like this:
but I have no errors and the consol won't say anything.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);
- 08-02-2010, 09:03 PM #13
Change this:
System.out.println(); // print nothing
to this:
System.out.println("in update"); // print something
- 08-02-2010, 09:10 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
I did that now and it still doesn't print anything.
- 08-02-2010, 09:24 PM #15
The method is NOT being called.then you'll know if the method is being called because you will see:
- 08-02-2010, 09:24 PM #16
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
So then what do I do?
- 08-02-2010, 09:33 PM #17
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.
- 08-02-2010, 09:38 PM #18
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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); } }
- 08-02-2010, 10:04 PM #19
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.
- 08-02-2010, 10:12 PM #20
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
Similar Threads
-
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Need Help: Double Buffer
By rukawa527 in forum AWT / SwingReplies: 1Last Post: 02-09-2009, 12:39 AM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM -
Can't DOUBLE BUFFER images
By hunterbdb in forum Java 2DReplies: 7Last Post: 11-14-2008, 08:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks