缓冲绘制无闪烁 : 缓冲区绘制 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 缓冲区绘制屏幕截图 
缓冲绘制无闪烁
缓冲绘制无闪烁


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. 演示自定义缓冲图像操作演示自定义缓冲图像操作
2. 平滑移动使用双缓冲平滑移动使用双缓冲
3. 离线渲染图像离线渲染图像
4. 数据缓冲区格获取
5. 综合BufferedImage
6. RepaintManager.currentManager(null).setDoubleBufferingEnabled(false)
7. 重绘只是受影响的部分组成
8. 建立多重缓冲策略
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.