RBL JAVA Tips
Main page
Home
[ Search | Home Page | Index Java tips ]


PrevRetour IndexNext
  • How to use double buffer to draw the applet without flicker

    tipsImg4.html : See source code or Download this
    Test the applet
  • Declarations for double buffering
          // for double buffering to prevent flicker 
          // memory image 
          Image offScreenImage; 
          // graphics context
          Graphics offScreen; 
  • Change : You must define a memory image and get this graphics context to draw the applet. after this you draw this memory image on the applet graphics context.
         // Called when the applet needs to be painted 
         // calls the flicker free updating system 
         public void paint (Graphics g) { 
              update(g); 
         } // end of paint 
    
         // Draw the applet without flicker 
         // Use synchronized 
         public synchronized void update(Graphics g) { 
              // get the size of the applet 
              int xW = size().width ; 
              int xH = size().height ; 
    
              if ((xW != aW) || (xH != aH) || (offScreen == null)) { 
                   // initialise the double buffering screen 
                   try { 
                        offScreenImage = createImage (xW, xH); 
                        offScreen = offScreenImage.getGraphics (); 
                   } catch (Exception e) { 
                        offScreen = null; 
                   } 
                   aW = xW ; 
                   aH = xH ; 
    
                   // get Image size 
                   iW = img.getWidth(this) ; 
                   iH = img.getHeight(this) ; 
              } 
    
              if ((offScreen!=null) && b) { 
                   paintApplet(offScreen); 
                   g.drawImage(offScreenImage,0,0,this); 
              }  else 
                   paintApplet(g); 
         } // end of update 
    
         // Draw applet 
         public void paintApplet(Graphics g) {  
                g.setColor(Color.white ) ; 
              g.fillRect(0, 0, aW, aH) ; 
              g.drawImage(img, xPos, yPos, this) ; 
                g.setColor(Color.red ) ; 
                g.drawString("double buffer : " + b, 10, yPos) ; 
         } 
  • Warning : the boolean "b" it's use to define state of double buffering.
  • .



Copyright © 1996..2003, BERTHOU. All right reserved.
Last change : 05 March 2003 18H20