Java 制作的一个放大镜程序

package com.mocake.jna;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Magnifier extends JFrame {
	/**
	 * 主容器
	 */
	private Container container = getContentPane();

	/**
	 * 放大镜x坐标
	 * 计算方式:setCoordinateX = absoluteCoordinateX 
	 * - relativeCoordinateXWhenMousePressed
	 */
	private int setCoordinateX;

	/**
	 * 放大镜y坐标
	 * 计算方式:setCoordinateY = absoluteCoordinateY 
	 * - relativeCoordinateYWhenMousePressed
	 */
	private int setCoordinateY;

	/**
	 * 鼠标绝对x坐标
	 */
	private int absoluteCoordinateX;

	/**
	 * 鼠标绝对y坐标
	 */
	private int absoluteCoordinateY;

	/**
	 * 鼠标按下时的相对x坐标
	 */
	private int relativeCoordinateXWhenMousePressed;

	/**
	 * 鼠标按下时的相对y坐标
	 */
	private int relativeCoordinateYWhenMousePressed;

	/**
	 * 标记鼠标是否按下。如果按下则为true,否则为false
	 */
	private boolean mousePressedNow;

	/**
	 * 放大镜尺寸
	 */
	private int magnifierSize = 100;

	/**
	 * 放大镜内容面板
	 */
	private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize);

	/**
	 * 构造函数,创建一个放大镜窗体
	 */
	public Magnifier() {
		setUndecorated(true); // 窗体边缘
		setResizable(false);
		container.add(magnifierPanel);
		addMouseListener(new MouseFunctions());
		addMouseMotionListener(new MouseMotionFunctions());
		updateSize(magnifierSize);
		this.setVisible(true);
	}

	/**
	 * 程序入口点
	 * 
	 * @param arg
	 *                启动参数,这里为空
	 */
	public static void main(String arg[]) {
		Magnifier magnifier = new Magnifier();
		magnifier.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * 更新窗体
	 * 
	 * @param magnifierSize
	 *                放大镜尺寸
	 */
	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;

	/**
	 * 带参数的构造函数
	 * @param magnifierSize
	 *         放大尺寸
	 */
	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;
	}

	/**
	 * 设置放大镜的位置
	 * @param locationX
	 *         x坐标
	 * @param locationY
	 *         y坐标
	 */
	public void setMagnifierLocation(int locationX, int locationY) {
		this.locationX = locationX;
		this.locationY = locationY;
		repaint(); // 注意重画控件
	}

	/**
	 * 设置放大镜的尺寸
	 * @param magnifierSize
	 *         放大镜尺寸
	 */
	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);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值