title: Java paint program double buffering
body: <p>So I'm working on a paint program in school and now I have to double buffer it. After a week of searching and using tutorials online I am still unable to double buffer my program properly... I have written a double buffer function but then you can't paint anything... When i disable the double buffer you can paint fine but the screen flickers. Please help </p>
  
  <p>(Yes I am aware that some of my code is deprecated and I should fix that)</p>
  
  <p>Code:</p>
  
  <pre><code>import java.applet.*;
  import java.awt.*;
  
  public class PaintRun extends Applet
  {
  
      static int posx = 0;
      static int posy = 0;
  
      static int posxOld = 0;
      static int posyOld = 0;
  
      static int size = 20;
  
      static int toolNum = 0;
  
      static boolean ready = false;
  
      static Color col =  new Color(120,120,120);
  
      static Button clear = new Button(0,0,20,'C');
      static Button sizeInc = new Button(20,0,20,'+');
      static Button sizeDec = new Button(40,0,20,'-');
  
      static Tools tool = new Tools(posx,posy);
  
      public void paint(Graphics g)
      {
          drawButtons(g);
          detectButtons(g);
      }
  
      private static void detectButtons(Graphics g)
      {       
          if (clear.detect(posx,posy) == true)
          {
              g.setColor(Color.WHITE);
              g.fillRect(0, 0, 800000, 800000);
              drawButtons(g);
          }
          else if (sizeInc.detect(posx, posy))
          {
              size+=2;
              drawButtons(g);
          }
          else if (sizeDec.detect(posx, posy))
          {
              size-=2;
              if (size &lt;= 2)
                  size = 2;
              drawButtons(g);
          }
          else
          {
              if (ready == true)
              {
                  g.setColor(Color.BLACK);
                  g.drawLine(posxOld, posyOld, posx, posy);
                  drawButtons(g);
              }
          }
      }
  
      private static void drawButtons(Graphics g)
      {
          clear.setColor(col, Color.BLACK);
          sizeInc.setColor(col, Color.BLACK);
          sizeDec.setColor(col, Color.BLACK);
  
          clear.drawButton(g);
          sizeInc.drawButton(g);
          sizeDec.drawButton(g);
      }
  
      public boolean mouseDown(Event e, int x, int y)
      {
          posx = x;
          posy = y;
  
          posxOld = x;
          posyOld = y;
  
          ready = true;
  
          repaint();
          return true;
      }
  
      public boolean mouseDrag(Event e, int x, int y)
      {
          posxOld = posx;
          posyOld = posy;
  
          posx = x;
          posy = y;
  
          repaint();
          return true;
      }
  
      // Normal Update function
  
      public void update(Graphics g)
      {
          paint(g);
      }
  
      // Double buffered Update function
  
      /*public void update(Graphics g) 
      {
          Graphics offgc;
          Image offscreen = null;
          Dimension d = size();
  
          offscreen = createImage(d.width, d.height);
          offgc = offscreen.getGraphics();
  
          offgc.setColor(getBackground());
          offgc.fillRect(0, 0, d.width, d.height);
          offgc.setColor(getForeground());
  
          paint(offgc);
  
          g.drawImage(offscreen, 0, 0, this);
      }
      */
  }
  
  class Button extends Applet
  {
  
      int minX; // X coord where the button is drawn
      int minY; // Y coord where the button is drawn
      int maxX; // X size
      int maxY; // Y size
  
      char letter;
  
      Color back;
      Color line;
  
      // Constructor block
  
      public Button(char desc)
      {
          minX = 0;
          minY = 0;
  
          maxX = 40;
          maxY = 40;
  
          back = new Color(255,255,255);
          line = new Color(0,0,0);
  
          letter = desc;
      }
  
      public Button(int x, int y, char desc)
      {
          minX = x;
          minY = y;
  
          maxX = 40;
          maxY = 40;
  
          back = new Color(255,255,255);
          line = new Color(0,0,0);
  
          letter = desc;
      }
  
      public Button(int x, int y, int size, char desc)
      {
          minX = x;
          minY = y;
  
          maxX = size;
          maxY = size;
  
          back = new Color(255,255,255);
          line = new Color(0,0,0);
  
          letter = desc;
      }
  
      public Button(int x, int y, int sizeX, int sizeY, char desc)
      {
          minX = x;
          minY = y;
  
          maxX = sizeX;
          maxY = sizeY;
  
          back = new Color(255,255,255);
          line = new Color(0,0,0);
  
          letter = desc;
      }
  
      // Draws the button
  
      public void drawButton(Graphics g)
      {
          g.setColor(back);
          g.fillRect(minX, minY, maxX, maxY);
  
          g.setColor(line);
          g.drawRect(minX, minY, maxX, maxY);
          g.drawString(Character.toString(letter), (minX+maxX/2)-4, (minY+maxY/2)+4);
      }
  
      // Sets the button colors
  
      public void setColor(Color b, Color l)
      {
          back = b;
          line = l;
      }
  
      // Detects if those coordinates are in the button location or not
  
      public boolean detect(int x, int y)
      {
          boolean flag = false;
  
              if (x &gt; minX &amp;&amp; x &lt; (minX + maxX) &amp;&amp; y &gt; minY &amp;&amp; y &lt; (minY + maxY))
                  flag = true;
              else
                  flag = false;
  
          return flag;
      }
  }
  </code></pre>