SWing放大镜
来源:互联网 发布:比目鱼软件下载 编辑:程序博客网 时间:2024/04/28 10:46
package Magnifier;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 absoluteCoordinateX; private int absoluteCoordinateY; private int relativeCoordinateXWhenMousePressed; private int relativeCoordinateYWhenMousePressed; //标记鼠标是否按下。如果按下则为true,否则为false private boolean mousePressedNow; // 放大镜尺寸 private int magnifierSize = 200; //放大镜内容面板 private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize); //这个窗体就是放大镜 你可以自己更改这个窗体.. public Magnifier() { setUndecorated(true); // 这个就是窗口的边缘 false的话就失效果了 setResizable(false); container.add(magnifierPanel); addMouseListener(new MouseFunctions()); addMouseMotionListener(new MouseMotionFunctions()); updateSize(magnifierSize); this.setVisible(true); } public static void main(String arg[]) { // JFrame Magnifier magnifier = new Magnifier(); magnifier.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 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 { public void mouseDragged(MouseEvent e) { if (mousePressedNow == true) {// 如果此时鼠标按下了,说明在拖拽窗体 absoluteCoordinateX = Magnifier.this .getLocationOnScreen().x + e.getX(); absoluteCoordinateY = Magnifier.this .getLocationOnScreen().y + e.getY(); setCoordinateX = absoluteCoordinateX - relativeCoordinateXWhenMousePressed; setCoordinateY = absoluteCoordinateY - relativeCoordinateYWhenMousePressed; magnifierPanel.setMagnifierLocation(setCoordinateX, setCoordinateY); setLocation(setCoordinateX, setCoordinateY); } } }}class MagnifierPanel extends JPanel{ private Image screenImage; private int magnifierSize; private int locationX; 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().height)); this.magnifierSize = magnifierSize; } public void setMagnifierLocation(int locationX, int locationY) { //X坐标 this.locationX = locationX; //Y坐标 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 + (magnifierSize / 4), // 源矩形的第一个角的y坐标 locationX + (magnifierSize / 4 * 3), // 源矩形的第二个角的x坐标 locationY + (magnifierSize / 4 * 3), // 源矩形的第二个角的y坐标 this ); }}