Java know how much (101) image buffering Technology

Source: Internet
Author: User
Tags gety

When the image information is large, using the above direct display method, may be shown in the previous section, after the display of the next part, because the latter part has not been read from the file, so that the display is mottled phenomenon. To improve the display, many applications use image buffering, which is to load the image into memory, draw an image or graphic in the buffer, and then output the image or graphic drawn in the buffer once on the screen. Buffer technology can not only solve the flicker problem, and because in the computer memory to create images, the program can do pixel-level image processing, complete the complex image transformation and then display.

"Example 12-6" applet demonstrates the image buffer display technology. When the program is running, when the mouse is pressed within the image area, the image will appear with a border, and when the mouse is moved, the image moves with it. When you lift the mouse, the border disappears. The program puts images of two states into two buffers first, and when the mouse drags, repeatedly redraws the mouse-pressed image in the new position when the mouse is lifted, redrawing the image of the mouse to lift the style.

1 when the mouse is lifted, redraw the image of the mouse lift style. 2 Importjava.applet.*;3 Importjava.awt.*;4Imprt Java.awt.image. * ;5 Importjavax.swing.*;6 Importjava.event.*;7  Public classExample7_6extendsapplet{8 Image mypicture;9     /*init () method, you first define an image object and give the return value of the CreateImage () method, then create the Graphics object and give it a graphical environment. Finally, let the graphics object call the DrawImage () method to display the image. Ten because the Graphics object here OFFSCREENGC is a non-screen object, the applet window will not have an image display*/ One      Public voidinit () { AMypicture = GetImage (GetCodeBase (), "Mypic.jpg"); -Image Offscreenimage =createimage (Size (). width, size (). height); -Graphics OFFSCREENGC =offscreenimage.getgraphics (); the         NewBuffereddemo (mypicture); -     } -     /*the fourth parameter of the DrawImage () method is to implement the ImageObserver interface, in the init () method, the parameter that calls the DrawImage () method is this, so the applet defines the Imageupdate () method*/ -      Public BooleanImageupdate (Image img,intINFOFLG,intXintYintWinth) { +         if(INFOFLG = allbits) {//indicates that the image is all loaded into memory - repaint (); +             return false;//prevent threads from calling the Imageupdate () method again A         } at         Else -             return true; -     } - } - /*the process of executing the program is that when the applet calls the DrawImage () method, the DrawImage () method creates a thread that calls the Imageupdate () method, and in the Imageupdate () method, measures whether the image has been partially transferred into memory. The created thread constantly calls the Imageupdate () method until the method returns false. The parameter INFOFLG allows the applet to know how the image is loaded into memory. When Infoflg equals Allbits, it indicates that the image is all loaded into memory. When the method finds that the image has all been loaded into memory, the imageloaded is true and the repaint () method is called to redraw the applet window. The method returns false to prevent the thread from calling the Imageupdate () method again. */ - classBuffereddemoextendsjframe{ in      Publicbuffereddemo (Image img) { -          This. Getcontentpane (). Add (NewPicpanel (IMG)); toSetTile ("Dual Slow Technology demo"); +SetSize (300, 300); -SetVisible (true); the     } * } $ classPicpaneextendsJPanelImplementsMouseListener, mousemotionlistener{Panax Notoginseng     intx = 0, y = 0, dx = 0, cy = 0; - bufferedimage bimg1, Bimg2; the     BooleanUpstate =true; +      Publicpicpanel (Image img) { A          This. SetBackground (color.white); the          This. Addmouselistener ( This); +          This. Addmousemotionlistener ( This); -BIMG1 =NewBufferedImage (Img.getwidth ( This), Img.getheight ( This), $ Bufferedimage.type_int_argb); $Bimg2 =NewBufferedImage (Img.getwidth ( This), Img.getheight ( This), - Bufferedimage.type_int_argb); -Graphics2D g2d1 =bimg1.creategraphics (); theGraphics2D G2D2 =bimg2.creategraphics (); -G2d1.drawimage (IMG, 0, 0, This);WuyiG2d2.drawimage (IMG, 0, 0, This); theG2d2.drawrect (1, 1, img.getwidth ( This)-3, Img.getheight ( This)-3); -     } Wu      Public voidpaintcomponent (Graphics g) { -         Super. Paincomponent (g); AboutGraphics2D g2d =(graphics2d) G; $         if(upState) -G2d.drawimage (BIMG1, x, Y, This); -         Else -G2d.drawimage (bimg2.x, Y, This); A     } +      Public voidmousepress (MouseEvent e) { the         if(E.getx () >= x && e.getx () < x + Bimg1.getwidth ( This) && e.gety () >= y&& e.gety () < Y + bimg1.getheight ( This)){ -Upstate =false; $ setcursor (Cursor.getpredefinedcursor (coursor.hand_cursor)); theDX = E.getx ()-x; theDY = e.gety ()-y; the Repain (); the         } -     } in      Public voidmouseexited (MouseEvent e) {} the      Public voidmouseclicked (MouseEvent e) {} the      Public voidmouseentered (MouseEvent e) {} About      Public voidmousereleased (MouseEvent e) { the          This. SetCursor (Cursor.getpredefinedcursor (cursor.default_cursor)); theUpState =true; the repaint (); +     } -      Public voidmousemoved (MouseEvent e) {} the      Public voidmousedragged (MouseEvent e) {Bayi         if(!upState) { thex = E.getx ()-DX; they = e.gety ()-dy; - repaint (); -         } the     } the}

To create a buffer image, the program needs to introduce the BufferedImage class in the Java.awt.image package. To create a buffer graph, you can call the CreateImage () method, which returns an Image object and then converts it to a BufferedImage object. For example, code:

   BufferedImage bimage = (bufferedimage) this. CreateImage (the. getwidth (),this. GetHeight ( ));

The following construction methods can also be used to build.

    BufferedImage (int width,intint imageType);

Where the parameter imagetype is the image type.

To display an image using a buffer, you first prepare the image in the buffer and then display the image in the buffer on the interface. Graphic object graphics are required for displaying images, which can be established by the following methods:

    Graphics2D g2d = Bimge.creategraphics ();

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing BasicsJava know how much (96) set the font and color of the drawingJava know how much (97) Drawing Mode OverviewHow much Java knows (98) the drawing method of the Graphics classJava know how much (graphics2d) the drawing method of the classJava know how much (100) Image processing Basics

Java know how much (101) image buffering Technology

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.