Buffered draw without flicker : Buffer Paint « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 2D Graphics GUI » Buffer Paint 




Buffered draw without flicker
Buffered draw without flicker


import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

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

public class BufferedDraw extends JPanel implements MouseListener,
    MouseMotionListener {
  Rectangle rect = new Rectangle(0010050);

  BufferedImage bi = new BufferedImage(55, BufferedImage.TYPE_INT_RGB);

  Graphics2D big;

  int last_x, last_y;

  boolean firstTime = true;

  Rectangle area;

  boolean pressOut = false;

  public BufferedDraw() {
    setBackground(Color.white);
    addMouseMotionListener(this);
    addMouseListener(this);
  }

  // Handles the event of the user pressing down the mouse button.
  public void mousePressed(MouseEvent e) {

    last_x = rect.x - e.getX();
    last_y = rect.y - e.getY();

    // Checks whether or not the cursor is inside of the rectangle while the
    // user is pressing themouse.
    if (rect.contains(e.getX(), e.getY())) {
      updateLocation(e);
    else {
      pressOut = true;
    }
  }

  // Handles the event of a user dragging the mouse while holding down the
  // mouse button.
  public void mouseDragged(MouseEvent e) {

    if (!pressOut) {
      updateLocation(e);
    }
  }

  // Handles the event of a user releasing the mouse button.
  public void mouseReleased(MouseEvent e) {
    if (rect.contains(e.getX(), e.getY())) {
      updateLocation(e);
    }
  }

  public void mouseMoved(MouseEvent e) {
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void updateLocation(MouseEvent e) {

    rect.setLocation(last_x + e.getX(), last_y + e.getY());
    repaint();
  }

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

  public void update(Graphics g) {
    Graphics2D g2 = (Graphics2Dg;

    if (firstTime) {
      Dimension dim = getSize();
      int w = dim.width;
      int h = dim.height;
      area = new Rectangle(dim);
      bi = (BufferedImagecreateImage(w, h);
      big = bi.createGraphics();
      rect.setLocation(w / 50, h / 25);
      big.setStroke(new BasicStroke(8.0f));
      firstTime = false;
    }

    big.setColor(Color.white);
    big.clearRect(00, area.width, area.height);

    big.setPaint(Color.red);
    big.draw(rect);
    big.setPaint(Color.blue);
    big.fill(rect);

    g2.drawImage(bi, 00this);
  }

  private boolean checkRect() {
    if (area == null) {
      return false;
    }
    if (area.contains(rect.x, rect.y, 10050)) {
      return true;
    }
    int new_x = rect.x;
    int new_y = rect.y;

    if ((rect.x + 100> area.width) {
      new_x = area.width - 99;
    }
    if (rect.x < 0) {
      new_x = -1;
    }
    if ((rect.y + 50> area.height) {
      new_y = area.height - 49;
    }
    if (rect.y < 0) {
      new_y = -1;
    }
    rect.setLocation(new_x, new_y);
    return false;
  }

  public static void main(String s[]) {

    JFrame f = new JFrame("BufferedShapeMover");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(new BufferedDraw()"Center");

    f.pack();
    f.setSize(new Dimension(550250));
    f.show();
  }

}


           
       














Related examples in the same category
1.Demos of a custom buffered image operationDemos of a custom buffered image operation
2.Smooth move using double bufferSmooth move using double buffer
3.Image offline renderingImage offline rendering
4.Data Buffer Grabber
5.Composite BufferedImage
6.RepaintManager.currentManager(null).setDoubleBufferingEnabled(false)
7.repaint just the affected part of the component
8.Create a multiple buffer strategy with the number of buffers given
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.