2

I found this code of double buffering on internet but it has no explaination. I am a little confused in this code.

  • Why is the Image "i" used? What is its use if it is to be used once?

  • Why are we assigning changing color to Foreground color,when we already have set color?

  • What is g.drawImage() method doing?

Here is the code:

public void update(Graphics g)
{
    if(i==null)
    {
        i=createImage(getWidth(), getHeight());
        graph=i.getGraphics();
    }

    graph.setColor(getBackground());
    graph.fillRect(0, 0, getWidth(),getHeight());
    graph.setColor(getForeground());

    paint(graph);

    g.drawImage(i,0,0,this);
  }

Regards

Alfred
  • 1,543
  • 7
  • 33
  • 45
  • 1
    Your code seems to be very incomplete making it difficult to tease out the logic, including where the g comes from (possibly passed from the JVM into a `paint(...)` or `paintComponent(...)` method? – Hovercraft Full Of Eels Nov 23 '12 at 17:14
  • 1
    and Swing JComponents are doublebuffered by default – mKorbel Nov 23 '12 at 17:16
  • OK, that edit gives more information but also generates more questions. One would rarely if ever override the `update(Graphics g)` method in a Swing application, and usually this would be done for AWT applications only. Your tag suggests that this question is about Swing -- please clarify. – Hovercraft Full Of Eels Nov 23 '12 at 17:16
  • that was a mistake. I apologise for that mistake. I am using it in applet – Alfred Nov 23 '12 at 17:17
  • Then this tutorial is likely way out of date. I would use Swing and not AWT since as mKorbel notes, drawing in Swing is done via double buffering by default. – Hovercraft Full Of Eels Nov 23 '12 at 17:18
  • But when I use JApplet, the moving objects leave their mark behind. And every thing messes up – Alfred Nov 23 '12 at 17:24
  • @Alfred: that has ***nothing*** to do with double buffering. You're asking the wrong question. Possibly you're not calling a super paint or paintComponent method. I suggest that you ask a new question with all these important details. Have you gone through the basic painting with Swing tutorials? Are you drawing in the `paintComponent(...)` method of a JPanel? Are you calling the super method on the first line of this method? – Hovercraft Full Of Eels Nov 23 '12 at 17:27

2 Answers2

6

The basic idea of Double Buffering is to create the image off screen then display it all at once.

Double Buffering

From the java tutorials found here

The code you have there first creates an image on first way through to be your "Back Buffer" with this bit, i is likely a field such as

 private Image i;
 private Graphics graph;

 if(i==null)
{
    i=createImage(getWidth(), getHeight());
    graph=i.getGraphics();
}

Then Paints the background color onto the image with this

graph.setColor(getBackground());
graph.fillRect(0, 0, getWidth(),getHeight());

Then sets the front ready for drawing.

graph.setColor(getForeground());
paint(graph); /draws

Finally drawing the back Buffer over to the primary surface.

g.drawImage(i,0,0,this);
AbstractChaos
  • 4,211
  • 1
  • 19
  • 28
  • One question. I gets updated only once that is in the beginning. After that, it doesn't gets updated. Then why doesn't the `g.drawImage()` method prints new graphics always – Alfred Nov 24 '12 at 10:52
  • @Alfred graph is a reference to `i`'s graphics class any change to that affects the image. – AbstractChaos Nov 26 '12 at 10:27
  • how graph became reference to image class? – Alfred Nov 26 '12 at 13:30
  • @Alfred not image, Graphic class the one that will actually has the methods to draw the image also has a reference (by virtue of being retrieved from the image itself), back to the image on which it is meant to paint on. – AbstractChaos Nov 26 '12 at 13:36
  • Ok so you are directing me to the reference created in the i==null condition. Do you mean the reference created there by returning graphics object from image class using i.getGraphics() – Alfred Nov 27 '12 at 14:03
  • thanks alot. :) please visit my other question here: http://stackoverflow.com/questions/13586163/putting-java-applet-on-the-web – Alfred Nov 27 '12 at 14:26
2

The graphics operations are all performed on a Graphics obtained from i, which is a bitmap in memory.

When they're finished, the bitmap is drawn onto the "real" (screen) Graphics object g. So the user will never see half-finished drawing, which eliminates flicker.

The field i is allocated the first time and then reused, so it is not only used once.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284