Java Swing Double buffering technology solves the problem of image flicker __java

Source: Internet
Author: User
Tags gety
Content reference to: about double buffering

When we watch TV, the screen we see is called the OSD layer, which means we can only see the images on the OSD layer. Now, I need to create a virtual, invisible, OSD layer that can be painted above (for example, Dot, line), which I call offscreen (back buffer). This offscreen exists in memory, we draw on it, the offscreen above can be shown on the OSD layer, a function that creates this offscreen, returns the handle of the offscreen (integer pointer), width, height, Pointer to the new offscreen data buffer, which is a offscreen data buffer created outside the function, the size of the offscreen height * Width * The size of each pixel data. Flicker is a common problem in graphics programming. A graphical operation that requires multiple complex drawing operations causes the rendered image to flicker or have an otherwise unacceptable appearance. Double buffering is used to solve these problems. Dual buffering uses a memory buffer to resolve flicker problems caused by multiple rendering operations. When double buffering is enabled, all drawing operations are first rendered to the memory buffer, not to the drawing surface on the screen. When all the drawing operations are complete, the memory buffers are copied directly to the drawing surface to which they are associated. Because only one graphic operation is performed on the screen, the image flicker caused by the complex drawing operation is eliminated.

In Java Swing, the principle of drawing is as follows:

Update ()-> paint (Graphics g)-> paintcomponent (Graphics g)

They are called gradually, and Java swing has a double buffer built into the paint (Graphics g).

If our image is drawn on the JPanel, we can inherit the JPanel and then make a copy of the paintcomponent (Graphics g) and update methods. We can draw the image on the paintcomponent, and then call the Paint method to invoke the Paintcomponent method to draw the image.

Double Buffering Implementation:

The update () method in the JPanel class creates a new image cache space, then takes the brush over it and draws the object to be drawn with the paint method. Once again, the image is displayed on the JPanel control.

I am here to use the mouse to draw lines to eliminate flashing examples to illustrate the use of double buffering.

Import Java.awt.Canvas;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseMotionAdapter;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Java.awt.image.BufferedImage;
Import Javax.swing.JFrame;
 public class Drawtrail {bufferedimage image = new BufferedImage (+, BUFFEREDIMAGE.TYPE_INT_RGB);
 Graphics g = image.getgraphics ();
 Private Drawcanvas canvas = new Drawcanvas ();
 private int PreX =-1;
    private int prey =-1;  Private Image offscreenimage;
  Graphics cache public void init () {g.fillrect (0, 0, 1600, 1000);
  JFrame frame = new JFrame ("Test draw the mouse trajectory");
  
  Frame.setsize (600, 600);
  Frame.add (canvas);
  Frame.setlayout (NULL);
  Canvas.setbounds (0, 0, 500, 500);
  
  Frame.setvisible (TRUE);
    Canvas.addmousemotionlistener (New Mousemotionadapter () {@Override public void mousedragged (MouseEvent e) { if (PreX > 0&& Prey > 0) {g.setcolor (color.black);
    G.drawline (PreX, Prey, E.getx (), e.gety ());
    } PreX = E.getx ();
    Prey = E.gety ();
   Canvas.repaint ();
  
  }
  });
            Frame.addwindowlistener (New Windowadapter ()//Add window close processing function {public void windowclosing (WindowEvent e)
            {system.exit (0);
 }});
  public static void Main (string[] args) {drawtrail dc = new Drawtrail ();
  
 Dc.init ();
  Class Drawcanvas extends Canvas {private static final long serialversionuid = 1L;
   @Override public void Paint (Graphics g) {graphics2d g2 = (graphics2d) g;
  G2.drawimage (image, 0, 0, NULL); @Override public void Update (Graphics g) {if (offscreenimage = = null) Offscreenimage = Thi     S.createimage (500, 500);  Creates a new image cache space, where the image size is 800*600 Graphics gimage = Offscreenimage.getgraphics ();                            Bring it to the Gimage, save the Paint (gimage);       Draw the things you want to draw to the image cache space to G.drawimage (offscreenimage, 0, 0, NULL);
 And then show them all at once}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.