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

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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » Buffer PaintScreenshots 
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. Smooth move using double bufferSmooth move using double buffer
2. Image offline renderingImage offline rendering
ww__w_._j___av_a_2__s.___c_o__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.