双缓冲绘制无闪烁_第1页
双缓冲绘制无闪烁_第2页
双缓冲绘制无闪烁_第3页
双缓冲绘制无闪烁_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、public class BufferedDraw extends JPanel implements MouseListener, MouseMotionListener Rectangle rect = new Rectangle(0, 0, 100, 50); BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); Graphics2D big; int last_x, last_y; boolean firstTime = true; Rectangle area; boolean pressOut

2、 = 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

3、 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) update

4、Location(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 mouseEn

5、tered(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 = (Graphics2D) g; if (firstTime) Dimension dim = getSize(); int w = dim.width; int h =

6、dim.height; area = new Rectangle(dim); bi = (BufferedImage) createImage(w, h); big = bi.createGraphics(); rect.setLocation(w / 2 - 50, h / 2 - 25); big.setStroke(new BasicStroke(8.0f); firstTime = false; big.setColor(Color.white); big.clearRect(0, 0, area.width, area.height); big.setPaint(Color.red)

7、; big.draw(rect); big.setPaint(Color.blue); big.fill(rect); g2.drawImage(bi, 0, 0, this); private boolean checkRect() if (area = null) return false; if (area.contains(rect.x, rect.y, 100, 50) return true; int new_x = rect.x; int new_y = rect.y; if (rect.x + 100) > area.width) new_x = area.width -

8、 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 Bord

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论