SWing放大镜

0 篇文章 0 订阅
 
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
     );
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值