RepaintManager.currentManager(null).setDoubleBufferingEnabled(false) : Buffer Paint « 2D Graphics GUI « Java






RepaintManager.currentManager(null).setDoubleBufferingEnabled(false)



import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.RepaintManager;

public class DragImage extends JComponent implements MouseMotionListener {
  static int imageWidth = 60, imageHeight = 60;
  int imageX, imageY;

  Image image;

  public DragImage(Image i) {
    image = i;
    addMouseMotionListener(this);
  }

  public void mouseDragged(MouseEvent e) {
    imageX = e.getX();
    imageY = e.getY();
    repaint();
  }

  public void mouseMoved(MouseEvent e) {
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.drawImage(image, imageX, imageY, this);
  }

  public static void main(String[] args) {
    String imageFile = "A.jpg";
    // Turn off double buffering
    RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);

    Image image = Toolkit.getDefaultToolkit().getImage(DragImage.class.getResource(imageFile));
    image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_DEFAULT);
    JFrame frame = new JFrame("DragImage");
    frame.getContentPane().add(new DragImage(image));
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

 








Related examples in the same category

1.Demos of a custom buffered image operationDemos of a custom buffered image operation
2.Buffered draw without flickerBuffered draw without flicker
3.Smooth move using double bufferSmooth move using double buffer
4.Image offline renderingImage offline rendering
5.Data Buffer Grabber
6.Composite BufferedImage
7.repaint just the affected part of the component
8.Create a multiple buffer strategy with the number of buffers given