⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 magnifier.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class Magnifier extends Applet implements MouseMotionListener {
	Graphics g;

	Image offi;

	Image back;

	String picture_name;

	MediaTracker imageTracker;

	int boxX = 0, boxY = 0; //放大镜初始位置

	int boxW = 100, boxH = 100; //放大镜宽度、高度

	int width, height;

	public void init() {
		//初始化applet

		//加载图片
		g = getGraphics();
		picture_name = new String();
		picture_name = getParameter("picture_name");
		if (picture_name == null)
			picture_name = "beautiful.jpg";
		imageTracker = new MediaTracker(this);
		back = getImage(getCodeBase(), picture_name);
		//设置放大后的图象的大小
		offi = createImage(300, 200);
		Graphics offg = offi.getGraphics();
		offg.drawImage(back, 0, 0, this);
		//添加鼠标事件监听
		addMouseMotionListener(this);
	}

	public void mouseDragged(MouseEvent e) {
		//鼠标拖拽事件处理
	}

	public void mouseMoved(MouseEvent e) {
		//处理鼠标移动事件
		//通过鼠标位置设置放大镜的位置
		redrawGlass(boxX, boxY, e.getX(), e.getY());
		//设置放大镜的当前位置
		boxX = e.getX();
		boxY = e.getY();
		//若放大镜溢出applet则进行调整
		if (boxX > (width - boxW / 2))
			boxX = width - boxW / 2;
		if (boxY > (height - boxH / 2))
			boxY = height - boxH / 2;
		//输出放大镜
		drawGlass();
	}

	void drawGlass() {
		Graphics temp;
		//复制g的一个实例
		temp = g.create();
		//为temp限制一个矩形区域
		temp.clipRect(boxX, boxY, boxW, boxH);
		//输出放大后的图象
		temp.drawImage(back, -boxX, -boxY, width * 2, height * 2, null);
		//输出放大镜边框
		g.setColor(Color.white);
		g.drawRect(boxX, boxY, boxW - 1, boxH - 1);
	}

	void redrawGlass(int oldX, int oldY, int newX, int newY) {
		//清除已经画过的矩形框和放大的图象
		Graphics temp;
		temp = g.create();
		if (newX <= oldX && newY <= oldY) {
			temp.clipRect(newX, newY + boxH, boxW + oldX - newX, oldY - newY);
			temp.drawImage(offi, 0, 0, null);
			temp = g.create();
			temp.clipRect(newX + boxW, newY, oldX - newX, boxH + oldY - newY);
			temp.drawImage(offi, 0, 0, null);
		} else if (newX > oldX && newY <= oldY) {
			temp.clipRect(oldX, newY + boxH, boxW + newX - oldX, oldY - newY);
			temp.drawImage(offi, 0, 0, null);
			temp = g.create();
			temp.clipRect(oldX, newY, newX - oldX, boxH + oldY - newY);
			temp.drawImage(offi, 0, 0, null);
		} else if (newX > oldX && newY > oldY) {
			temp.clipRect(oldX, oldY, boxW + newX - oldX, newY - oldY);
			temp.drawImage(offi, 0, 0, null);
			temp = g.create();
			temp.clipRect(oldX, oldY, newX - oldX, boxH + newY - oldY);
			temp.drawImage(offi, 0, 0, null);
		} else {
			temp.clipRect(newX, oldY, boxW + oldX - newX, newY - oldY);
			temp.drawImage(offi, 0, 0, null);
			temp = g.create();
			temp.clipRect(newX + boxW, oldY, oldX - newX, boxH + newY - oldY);
			temp.drawImage(offi, 0, 0, null);
		}
	}

	public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
		if (flags == ALLBITS) {
			width = back.getWidth(this);
			height = back.getHeight(this);
			offi = createImage(width + boxW / 2, height + boxH / 2);
			Graphics offg = offi.getGraphics();
			offg.setColor(Color.lightGray);
			offg.fillRect(0, 0, width + boxW / 2, height + boxH / 2);
			offg.drawImage(back, 0, 0, this);
			repaint();
			return false;
		} else
			return true;
	}

	public void paint(Graphics g) {
		//输出背景图片
		g.drawImage(back, 0, 0, this);
		//画放大镜
		drawGlass();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -