java放大镜源程序#优质参考_第1页
java放大镜源程序#优质参考_第2页
全文预览已结束

下载本文档

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

文档简介

1、/*用java语言编写,在JCeator中运行成功 *这是一个放大镜的程序,运行后拖动屏幕左上角的方框,即可以看到放大效果 *放大镜的原理:光的折射*/import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Magnifier extends JFrame private Container container = getContentPane(); private int setCoordinateX; private int setCoordinateY; private int abso

2、luteCoordinateX; private int absoluteCoordinateY; private int relativeCoordinateXWhenMousePressed; private int relativeCoordinateYWhenMousePressed; private boolean mousePressedNow; private int magnifierSize = 100; private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize); public Magn

3、ifier() setUndecorated(true); setResizable(false); container.add(magnifierPanel); addMouseListener(new MouseFunctions(); addMouseMotionListener(new MouseMotionFunctions(); updateSize(magnifierSize); this.setVisible(true); public static void main(String arg) Magnifier magnifier = new Magnifier(); mag

4、nifier.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void updateSize(int magnifierSize) magnifierPanel.setMagnifierSize(magnifierSize + 100); setSize(magnifierSize + 100, magnifierSize + 100); validate(); private class MouseFunctions extends MouseAdapter public void mousePressed(MouseEvent

5、e) if (e.getClickCount() = 1) / 如果鼠标左键点了一下,说明按住了窗体 mousePressedNow = true; relativeCoordinateXWhenMousePressed = e.getX(); relativeCoordinateYWhenMousePressed = e.getY(); public void mouseReleased(MouseEvent e) mousePressedNow = false; private class MouseMotionFunctions extends MouseMotionAdapter pu

6、blic void mouseDragged(MouseEvent e) if (mousePressedNow = true) / 如果此时鼠标按下了,说明在拖拽窗体 absoluteCoordinateX = Magnifier.this.getLocationOnScreen().x + e.getX(); absoluteCoordinateY = Magnifier.this.getLocationOnScreen().y + e.getY(); setCoordinateX = absoluteCoordinateX - relativeCoordinateXWhenMousePr

7、essed; setCoordinateY = absoluteCoordinateY - relativeCoordinateYWhenMousePressed; magnifierPanel.setMagnifierLocation(setCoordinateX, setCoordinateY); setLocation(setCoordinateX, setCoordinateY); class MagnifierPanel extends JPanel private Image screenImage; private int magnifierSize; private int l

8、ocationX; private int locationY; private Robot robot; public MagnifierPanel(int magnifierSize) try robot = new Robot(); catch (AWTException e) screenImage = robot.createScreenCapture(new Rectangle(0, 0, Toolkit .getDefaultToolkit().getScreenSize().width, Toolkit .getDefaultToolkit().getScreenSize().

9、height); this.magnifierSize = magnifierSize; public void setMagnifierLocation(int locationX, int locationY) this.locationX = locationX; this.locationY = locationY; repaint(); public void setMagnifierSize(int magnifierSize) this.magnifierSize = magnifierSize; public void paintComponent(Graphics g) super.paintComponent(Graphics2D) g); g.drawImage(screenImage, 0, / 目标矩形的第一个角的x坐标 0, / 目标矩形的第一个角的y坐标 magnifierSize, / 目标矩形的第二个角的x坐标 magnifierSize, / 目标矩形的第二个角的y坐标 locationX + (magnifierSize / 4), / 源矩形的第一个角的x坐标 locationY +

温馨提示

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

评论

0/150

提交评论