Java AWT Graphics double buffer

Description

Java AWT Graphics double buffer


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Demo extends JPanel {
   int gap = 3;//ww w .  j av  a 2s  . co m
   int mx, my;
   boolean flicker = true;
   Image buffer = null;
   int w, h;

   public Demo() {
      Dimension d = getSize();
      w = d.width;
      h = d.height;
      buffer = createImage(w, h);
      addMouseMotionListener(new MouseMotionAdapter() {
         public void mouseDragged(MouseEvent me) {
            mx = me.getX();
            my = me.getY();
            flicker = false;
            repaint();
         }

         public void mouseMoved(MouseEvent me) {
            mx = me.getX();
            my = me.getY();
            flicker = true;
            repaint();
         }
      });
   }

   public void paint(Graphics g) {
      Graphics screengc = null;

      if (!flicker) {
         screengc = g;
         g = buffer.getGraphics();
      }

      g.setColor(Color.blue);
      g.fillRect(0, 0, w, h);

      g.setColor(Color.red);
      for (int i = 0; i < w; i += gap)
         g.drawLine(i, 0, w - i, h);
      for (int i = 0; i < h; i += gap)
         g.drawLine(0, i, w, h - i);

      g.setColor(Color.black);
      g.drawString("Press mouse button to double buffer", 10, h / 2);

      g.setColor(Color.yellow);
      g.fillOval(mx - gap, my - gap, gap * 2 + 1, gap * 2 + 1);

      if (!flicker) {
         screengc.drawImage(buffer, 0, 0, null);
      }
   }

   public void update(Graphics g) {
      paint(g);
   }

}

public class Main {
   public static void main(String[] args) {
      Demo panel = new Demo();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related