An example illustrates the use of image buffering to parse Java

  • 2020-04-01 04:15:02
  • OfStack

When the image information is large, using the method of direct display, may be the front part of the display, display the back part, because the latter part has not been read from the file, so the display appears mottled phenomenon. To improve the display, many applications use image buffering, which involves loading the image into memory in its entirety, drawing an image or graph in the buffer, and then printing the drawn image or graph from the buffer on the screen at once. Buffer technology can not only solve the flicker problem, but also because the image is created in the computer memory, the program can process the image at the pixel level, complete the complex image transformation before the display.

Applets demonstrate the technique of image buffering. When the program runs, when the mouse is pressed in the image area, the image will appear border, when the mouse is held, the image will also move. After lifting the mouse, the border disappears. The program will be two states of the image first into two buffers, when the mouse drag, constantly in the new position to redraw the mouse down style of image mouse up, redraw the mouse up style of image.


import java.applet.*;
import java.awt.*;
imprt java.awt.image. * ;
import javax.swing.*;
import java.event.*;
public class Example7_6 extends Applet{
  Image myPicture;
  
  public void init(){
    myPicture = getImage(getCodeBase(), "myPic.JPG");
    Image offScreenImage = createImage(size().width, size().height);
    Graphics offScreenGc = offScreenImage.getGraphics();
    new BufferedDemo(myPicture);
  }
  
  public boolean imageUpdate(Image img, int infoFlg, int x, int y, int w, int h){
    if (infoFlg = ALLBITS){ //Indicates that all images have been loaded into memory
      repaint();
      return false;//Prevents the thread from calling the imageUpdate() method again
    }
    else
      return true;
  }
}

class BufferedDemo extends JFrame{
  public BufferedDemo(Image img){
    this.getContentPane().add(new PicPanel(img));
    setTile(" Demonstration of dual cache technology ");
    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(img.getWidth(this), img.getHeight(this),
    BufferedImage.TYPE_INT_ARGB);
    bimg2 = new BufferedImage(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(MouseEvent 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();
    }
  }
}

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


  BufferedImage bimage = (BufferedImage)this.createImage(this.getWidth(),this.getHeight());


It can also be built using the following constructor.


  BufferedImage(int width,int heigh, int imageType);


Where the parameter imageType is the imageType.

To use the buffer to display the image, you need to prepare the image in the buffer, and then the image in the buffer to display on the interface. Display image requires Graphics, which can be created by:


  Graphics2D g2d = bimge.createGraphics();


Related articles: