package Cut;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
/**
* 截屏窗体 用来监听鼠标移动 绘制图案
*
* @author zzc
*
*/
public class DrawListener extends MouseAdapter implements MouseMotionListener {
// 画笔类型 颜色
int type;
Color color;
MyPanel panel;
BufferedImage image;
Graphics2D g;
int x0, y0;
// 屏幕中点坐标
int centerX, centerY;
// 屏幕大小
int screenX, screenY;
// 每次缩放的大小
int cutWidth, cutHeight;
// 缩放程度
int degree = 0;
public DrawListener(MyPanel panel, int type, Color color) {
this.g = (Graphics2D) panel.getGraphics();
this.type = type;
this.color = color;
this.panel = panel;
image = panel.icon;
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
centerX = screensize.width / 2;
centerY = screensize.height / 2;
screenX = screensize.width;
screenY = screensize.height;
cutWidth = screensize.width;
cutHeight = screensize.height;
initdraw();
}
// 初始化画笔
private void initdraw() {
int size = 2;
if (type == 1)
size = 2;
else if (type == 2)
size = 5;
else if (type == 3) {
size = 17;
}
g.setStroke(new BasicStroke(size)); // 边界宽度为5
g.setColor(color);
}
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (type == 3) {
// double L2 = Math.pow(y - y0, 2) + Math.pow(x - x0, 2);
// double L = Math.sqrt(L2);
// double sinL = Math.abs(y - y0) / L;
// double cosL = Math.abs(x - x0) / L;
// int xL = (int) (20 * sinL);
// int yL = (int) (20 * cosL);
g.setColor(new Color(color.getRed(), color.getGreen(), color
.getBlue(), 20));
}
if (!e.isControlDown()) {
g.drawLine(x0, y0, x, y);
} else {
// System.out.println(centerX + " " + centerY);
centerX += x0 - x;
centerY += y0 - y;
limitCenterXY();
ImageIcon icon = cutBmp();
Image Image = icon.getImage();
BufferedImage bImage = toBufferedImage(Image);
panel.setIcon(bImage);
}
x0 = x;
y0 = y;
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
x0 = e.getX();
y0 = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseReleased(e);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
// TODO Auto-generated method stub
super.mouseWheelMoved(e);
int t = e.getWheelRotation();
// System.out.println(t);
ImageIcon icon;
if (t < 0) {
// 滚轮向上滑动 放大 中心坐标不变
degree++;
} else {
// 滑轮向下划 缩小
degree--;
if (degree < 0)
degree = 0;
boolean a = limitCenterX();
boolean b = limitCenterY();
// 如果当截取区域未到边界 中心坐标不变
// 如果当截取区域已到边界
// 改变中心位置 防止扩大截取长宽后超出范围
if (a | b) {
updateCenterXY(a,b);
}
}
icon = cutBmp();
Image Image = icon.getImage();
BufferedImage bImage = toBufferedImage(Image);
panel.setIcon(bImage);
}
// 图像放大
public ImageIcon cutBmp() {
// 设置要截取宽高
setCutSize();
// 限制中心点坐标
limitCenterXY();
System.out.println("中点坐标:" + centerX + " " + centerY + "起点坐标:"
+ (centerX - cutWidth / 2) + " " + (centerY - cutHeight / 2)
+ " 长宽:" + cutWidth + " " + cutHeight);
BufferedImage subimage = image.getSubimage(centerX - cutWidth / 2,
centerY - cutHeight / 2,// 开始截取点的坐标
cutWidth, cutHeight);// 截取地图的大小
return new ImageIcon(subimage.getScaledInstance(screenX, screenY,
BufferedImage.SCALE_DEFAULT));// 返回当前显示的地图
}
// 设置要截取宽高
public void setCutSize() {
float f = (float) (degree / 10.0) + 1;
// System.out.println(f);
// 要截取的长度 当degree越大截取的长宽越小 即放大 当degree越小截取的长宽越大 即缩小
cutWidth = (int) (screenX / f);
cutHeight = (int) (screenY / f);
}
// 更新中心点坐标 r用来判断改变x 还是 y
public void updateCenterXY(boolean a, boolean b) {
// 得到为改变钱的截取长宽
int oldcutwidth = cutWidth;
int oldcutheight = cutHeight;
// 设置新截取长宽
setCutSize();
int tempx = (cutWidth - oldcutwidth) / 2;
int tempy = (cutHeight - oldcutheight) / 2;
if (a) {
if (centerX < screenX / 2) {
// 中心点向右移动
centerX += tempx;
} else {
// 中心点向左移动
centerX -= tempx;
}
}
if (b) {
if (centerY < screenX / 2) {
// 中心点向下移动
centerY += tempy;
} else {
// 中心点向上移动
centerY -= tempy;
}
}
limitCenterXY();
}
// 限制中心点坐标
public void limitCenterXY() {
limitCenterX();
limitCenterY();
}
public boolean limitCenterX() {
// 先判断 截取长宽能否被2整除 不能整除 要对中心坐标进行改变
int tempx = 0;
if (cutWidth % 2 != 0)
tempx = 1;
// 到达边界返回true 未到 返回false
if (centerX < cutWidth / 2.0) {
centerX = cutWidth / 2 + tempx;
return true;
} else if (centerX > screenX - cutWidth / 2) {
centerX = screenX - cutWidth / 2 - tempx;
return true;
}
return false;
}
public boolean limitCenterY() {
// 先判断 截取长宽能否被2整除 不能整除 要对中心坐标进行改变
int tempy = 0;
if (cutHeight % 2 != 0)
tempy = 1;
// 到达边界返回true 未到 返回false
if (centerY < cutHeight / 2) {
centerY = cutHeight / 2 + tempy;
return true;
} else if (centerY > screenY - cutHeight / 2) {
centerY = screenY - cutHeight / 2 - tempy;
return true;
}
return false;
}
// 将 Image 转化为 BufferedImage
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
// boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
/*
* if (hasAlpha) { transparency = Transparency.BITMASK; }
*/
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
// int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
/*
* if (hasAlpha) { type = BufferedImage.TYPE_INT_ARGB; }
*/
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispos