下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GA/T 2333-2025法庭科学塑料检验拉曼光谱法
- 注册会计师审计中职业道德概念框架的运用步骤
- 拒绝校园欺凌严守法律底线班会讲稿
- 某陶瓷厂烧成温度控制细则
- 2026江苏扬州市消防救援局政府专职消防人员国上半年招聘59人备考题库及答案详解(考点梳理)
- 中信期货佛山分公司2026届校园招聘备考题库附参考答案详解(考试直接用)
- 做账实操-行政事业单位账务处理规范及实操指南
- 2026春季新疆克拉玛依市面向高校毕业生招聘事业单位人员120人备考题库含答案详解(满分必刷)
- 2026上半年广东广州市越秀区教育局招聘事业编制教师83人备考题库含答案详解ab卷
- 2026陕西西安交通大学医院招聘1人备考题库带答案详解(黄金题型)
- 2026年汽车销售店员工劳动合同三篇
- 5.1 拆盒子 课件 2025-2026学年三年级数学下册北师大版
- 2025急诊科护理指南
- 江苏省安全员c证考试题库及答案
- 四川省算力发展蓝皮书
- 软件供应链安全培训内容课件
- 2025年浙江省杭州市辅警协警笔试笔试真题(含答案)
- 抗菌药物使用分级管理流程操作指南
- 国家安全与保密教育题库及答案解析
- 塑料注塑机基础调试操作培训资料
- 2026年晋中职业技术学院单招职业适应性考试题库必考题
评论
0/150
提交评论