When the image information is large, using the above direct display method, may be shown in the previous part, showing the latter part, because the latter part has not been read from the file, so that the display is mottled phenomenon. In order to improve the display effect, many applications use image buffering technology, that is, the image is fully loaded into memory, in the buffer to draw the image or graphics, and then the buffer in the plot of the image or graphics in a single output on the screen. Buffering technology can not only solve the flicker problem, and because the image is created in the computer memory, the program can process the image pixel level, complete the complex image transformation and then display.
The example applet demonstrates an image buffering display technique. When the program is running, when the mouse is pressed in the image area, the image will appear with a border, and when the mouse is moved, the image also moves. After lifting the mouse, the border disappears. The program puts the image of the two states first into two buffers, and when the mouse is dragged, the image of the mouse-raised style is redrawn while the mouse is constantly being redrawn in the new position.
Import java.applet.*;
Import java.awt.*; Imprt Java.awt.image.
* ;
Import javax.swing.*;
Import java.event.*;
public class Example7_6 extends applet{Image mypicture; /*init () method, first defines an image object, assigns the return value of the CreateImage () method, and then creates the graphics object and assigns it a graphical environment.
Finally, let the graphics object invoke the DrawImage () method to display the image. Because the Graphics object OFFSCREENGC is not a screen object, the applet window will not have an image display/public void init () {mypicture = GetImage (GetCodeBase (), "Mypic.jpg"
);
Image offscreenimage = createimage (Size (). width, size (). height);
Graphics OFFSCREENGC = Offscreenimage.getgraphics ();
New Buffereddemo (mypicture); The fourth parameter of the/*drawimage () method is the implementation of the ImageObserver interface, in the init () method, where the parameter to the DrawImage () method is called this, so the applet is to define the Imageupdate () method.
Boolean imageupdate (image img, int infoflg, int x, int y, int w, int h) {if (INFOFLG = allbits) {//indicates that the image has been fully loaded into memory
Repaint ();
Return false;//prevents the thread from calling the Imageupdate () method again} else. }/* The procedure is executed when the applet calls the DrawImage () method, and the DrawImage () method creates a thread that invokes the Imageupdate () method, in the Imageupdate () method, to determine whether the image has been partially transferredSave. The created thread keeps calling the Imageupdate () method until the method returns false. Parameter INFOFLG enables the applet to know how the image is loaded into memory. When INFOFLG equals allbits, the image is fully loaded into memory. When the method finds that the image has been fully loaded into memory, imageloaded is true and calls the repaint () method to repaint the applet window. method returns false to prevent the thread from calling the Imageupdate () method again.
* * Class Buffereddemo extends jframe{public buffereddemo (Image img) {This.getcontentpane (). Add (New Picpanel (IMG));
SetTile ("Double Slow technology demonstration");
SetSize (300, 300);
SetVisible (TRUE);
} class Picpane extends JPanel implements MouseListener, mousemotionlistener{int x = 0, y = 0, dx = 0, cy = 0;
BufferedImage Bimg1, Bimg2;
Boolean upstate = true;
Public Picpanel (Image img) {this.setbackground (color.white);
This.addmouselistener (this);
This.addmousemotionlistener (this);
BIMG1 = new BufferedImage (the Img.getwidth (this), Img.getheight (this), BUFFEREDIMAGE.TYPE_INT_ARGB);
Bimg2 = new BufferedImage (the Img.getwidth (this), Img.getheight (this), BUFFEREDIMAGE.TYPE_INT_ARGB);
Graphics2D g2d1 = Bimg1.creategraphics ();
Graphics2D G2D2 = Bimg2.creategraphics (); G2d1.DrawImage (IMG, 0, 0, this);
G2d2.drawimage (IMG, 0, 0, this);
G2d2.drawrect (1, 1, img.getwidth (this)-3, Img.getheight (this)-3);
public void Paintcomponent (Graphics g) {super.paincomponent (g);
Graphics2D g2d = (graphics2d) g;
if (upState) g2d.drawimage (BIMG1, x, y, this);
else G2d.drawimage (bimg2.x, y, this); public void Mousepress (MouseEvent e) {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));
DX = E.GETX ()-X;
DY = e.gety ()-y;
Repain (); } public void mouseexited (MouseEvent e) {} public void mouseclicked (MouseEvent e) {} public void mouseentered (MOUS Eevent e) {} public void mousereleased (MouseEvent e) {this.setcursor (Cursor.getpredefinedcursor (cursor.default_cursor
));
UpState = true;
Repaint ();
} public void mousemoved (MouseEvent e) {} public void mousedragged (MouseEvent e) {if (!upstate) {x = E.getx ()-
dx
y = e.gety ()-dy;
Repaint ();
}
}
}
Program to create a buffer image, you need 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 (This.getwidth (), This.getheight ());
You can also use the following construction methods to build.
BufferedImage (int width,int heigh, int imagetype);
Where the parameter imagetype is the image type.
Using a buffer to display the image, you must first prepare the image in the buffer, and then display the image in the buffer in the interface. Graphic object graphics is required to display the image, which can be established by the following methods:
Graphics2D g2d = Bimge.creategraphics ();