📄 drawinglayer.java
字号:
/*
* DrawingLayer.java - layer for drawing
* Mark Bryan Yu
* jimagemapper.sourceforge.net
*
* This file was modified by
* Heiko Schroeder
* wikiimagemapper.sourceforge.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package cn.dxm.client;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import cn.dxm.client.JImageMapper.MyFilter;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class DrawingLayer extends JPanel {
/**
*
*/
private static final long serialVersionUID = 8974575894141397104L;
private int currentShape = JImageMapper.RECTANGLE;
private List<Shape> shapes;
private boolean isMouseUp = true;
private Point startPoint;
private Point endPoint;
private Rectangle currentRectangle;
private JFrame parent;
private Shape activeShape;
//private static int imagename=new Random().nextInt(10000);
private static int imagename=1;
public DrawingLayer(JFrame parent) {
super();
this.parent = parent;
this.shapes = new ArrayList<Shape>();
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (currentShape == JImageMapper.RECTANGLE) {
if (isMouseUp) {
isMouseUp = false;
startPoint = e.getPoint();
endPoint = e.getPoint();
}
} else if (currentShape == JImageMapper.SELECTION) {
if (activeShape != null && isMouseUp) {
startPoint = e.getPoint();
}
}
}
// public void mouseClicked(MouseEvent e) {
// if (currentRectangle != null
// && currentShape == JImageMapper.SELECTION) {
//
// Point layerLocation = DrawingLayer.this
// .getLocationOnScreen();
// Rectangle rec=new Rectangle();
// // java.awt.Rectangle rec2 =rec.getCurrentRecatangle();
// Rectangle rec2 =currentRectangle;
// java.awt.Rectangle newrec = null;
//// if (rec.getCurrentRecatangle() != null) {
//// newrec = new java.awt.Rectangle(rec2.x+ layerLocation.x + 1, rec2.y+ layerLocation.y + 1, rec2.width - 1, rec2.height - 1);
//// }
//
// if (rec.getCurrentRecatangle() != null) {
// newrec = new java.awt.Rectangle(rec2.getX()+ layerLocation.x + 1, rec2.getY()+ layerLocation.y + 1, rec2.getWidth() - 1, rec2.getHeight() - 1);
// }
//
// String imageURL="client//"+imagename+".jpg";
//
// File file=new File(imageURL);
//
// if(!file.exists()){
// ImageUtil.saveImage(CatchImage.catchImage(newrec),
// imageURL);
// imagename++;
// System.out.println("保存图片"+imageURL+"成功");
// }else{
// System.out.println("图片"+imageURL+"已经存在");
// }
// }
// }
public void mouseReleased(MouseEvent e) {
if (currentShape == JImageMapper.RECTANGLE) {
if (isMouseUp == false) {
endPoint = new Point(e.getX(), e.getY());
if (currentRectangle != null) {
shapes.add(0, currentRectangle);
}
isMouseUp = true;
repaint();
}
} else if (currentShape == JImageMapper.SELECTION) {
if (currentShape == JImageMapper.RECTANGLE) {
}
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (currentShape == JImageMapper.RECTANGLE) {
if (isMouseUp == false) {
endPoint = new Point(e.getX(), e.getY());
}
}
// if a shape gets dragged, update its coordinates
if (currentShape == JImageMapper.SELECTION
&& activeShape != null) {
if (!e.isPopupTrigger()) {
activeShape.updateCoordinates(getLocation(), getSize(),
startPoint, e.getPoint());
startPoint = e.getPoint();
}
}
repaint();
}
public void mouseMoved(MouseEvent e) {
if (currentShape == JImageMapper.SELECTION) {
activeShape = null;
Shape shape = null;
Iterator iter = shapes.iterator();
while (iter.hasNext()) {
shape = (Shape) iter.next();
if (shape.contains(e.getPoint())) {
activeShape = shape;
break;
}
}
if (activeShape != null) {
setCursor(Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR));
activeShape.setActive(true);
} else {
setCursor(Cursor.getDefaultCursor());
}
for (int i = 0; i < shapes.size(); i++) {
Shape s = (Shape) shapes.get(i);
if (s != activeShape){
s.setActive(false);
}
if(s.contains(e.getPoint())){
if(s instanceof Rectangle)
currentRectangle=(Rectangle)s;
}
}
}
repaint();
}
});
setOpaque(false);
}
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
if (currentShape == JImageMapper.RECTANGLE) {
if (isMouseUp == false) {
if (currentShape == JImageMapper.RECTANGLE) {
currentRectangle = generateRectangle(startPoint, endPoint);
java.awt.Rectangle re = new java.awt.Rectangle(
currentRectangle.getX(), currentRectangle.getY(),
currentRectangle.getWidth(), currentRectangle
.getHeight());
Rectangle.setCurrentRecatangle(re);
g2d.drawRect((int) currentRectangle.getX(),
(int) currentRectangle.getY(),
(int) currentRectangle.getWidth(),
(int) currentRectangle.getHeight());
}
}
}
for (int i = shapes.size() - 1; i >= 0; i--) {
// System.out.println("the images number is:"+i);
Shape s = (Shape) shapes.get(i);
s.draw(g2d);
}
}
private Rectangle generateRectangle(Point startPoint, Point endPoint) {
Point pointOne = startPoint;
Point pointTwo = endPoint;
if ((endPoint.x > startPoint.x) && (endPoint.y > startPoint.y)) {
pointOne = new Point(startPoint);
pointTwo = new Point(endPoint);
} else if ((endPoint.x > startPoint.x) && (endPoint.y < startPoint.y)) {
pointOne = new Point(startPoint.x, endPoint.y);
pointTwo = new Point(endPoint.x, startPoint.y);
} else if ((endPoint.x < startPoint.x) && (endPoint.y < startPoint.y)) {
pointOne = endPoint;
pointTwo = startPoint;
} else if ((endPoint.x < startPoint.x) && (endPoint.y > startPoint.y)) {
pointOne = new Point(endPoint.x, startPoint.y);
pointTwo = new Point(startPoint.x, endPoint.y);
}
// 将矩形框限制在图片区域以内.
Point layerLocation = this.getLocation();
Dimension layerSize = this.getSize();
// 设置第一个点在图片区域以内.
if (pointOne.x < layerLocation.x) {
pointOne.x = layerLocation.x;
} else if (pointOne.x > layerLocation.x + layerSize.width) {
pointOne.x = layerLocation.x + layerSize.width;
} else {
pointOne.x = pointOne.x;
}
if (pointOne.y < layerLocation.y) {
pointOne.y = layerLocation.y;
} else if (pointOne.y > layerLocation.y + layerSize.height) {
pointOne.y = layerLocation.y + layerSize.height;
} else {
pointOne.y = pointOne.y;
}
// 设置第二个点在图片区域以内.
if (pointTwo.x < layerLocation.x) {
pointTwo.x = layerLocation.x;
} else if (pointTwo.x > layerLocation.x + layerSize.width) {
pointTwo.x = layerLocation.x + layerSize.width;
} else {
pointTwo.x = pointTwo.x;
}
if (pointTwo.y < layerLocation.y) {
pointTwo.y = layerLocation.y;
} else if (pointTwo.y > layerLocation.y + layerSize.height) {
pointTwo.y = layerLocation.y + layerSize.height;
} else {
pointTwo.y = pointTwo.y;
}
return new Rectangle(pointOne.x, pointOne.y, pointTwo.x - pointOne.x,
pointTwo.y - pointOne.y);
}
public List getShapes() {
return shapes;
}
public void addShapes(List<Shape> shapeList) {
shapes.addAll(0, shapeList);
repaint();
}
public void setCurrentShape(int currentShape) {
this.currentShape = currentShape;
}
public void deleteShapes() {
currentRectangle=null;
//currentShape = JImageMapper.Delete All;
shapes.clear();
repaint();
}
public void saveShapes() {
if (currentRectangle != null
&& currentShape == JImageMapper.RECTANGLE) {
Point layerLocation = DrawingLayer.this
.getLocationOnScreen();
Rectangle rec=new Rectangle();
// java.awt.Rectangle rec2 =rec.getCurrentRecatangle();
Rectangle rec2 =currentRectangle;
java.awt.Rectangle newrec = null;
// if (rec.getCurrentRecatangle() != null) {
// newrec = new java.awt.Rectangle(rec2.x+ layerLocation.x + 1, rec2.y+ layerLocation.y + 1, rec2.width - 1, rec2.height - 1);
// }
if (rec.getCurrentRecatangle() != null) {
newrec = new java.awt.Rectangle(rec2.getX()+ layerLocation.x + 1, rec2.getY()+ layerLocation.y + 1, rec2.getWidth() - 1, rec2.getHeight() - 1);
}
String imageURL="client//images//"+imagename+".jpg";
File file=new File(imageURL);
if(!file.exists()){
ImageUtil.saveImage(CatchImage.catchImage(newrec),
imageURL);
imagename++;
System.out.println("保存图片"+imageURL+"成功");
}else{
System.out.println("图片"+imageURL+"已经存在");
}
}
else{
String imagepath1=System.getProperty("user.dir")+"\\client\\images";
String imagepath2=System.getProperty("user.dir")+"\\client\\image";
MatchImage.copyFile(imagepath2+"\\All.jpg",imagepath1+"\\All.jpg");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -