package org.apache.log4j;
/*
作者:泡沫
地址:http://hi.baidu.com/av51
功能:用于截取图片,方便快捷!
mail:yuhuidog#163.com (注意:其中#为@)
*/
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class AWTpicture extends Frame implements MouseListener,MouseMotionListener,ActionListener{
private int firstX,firstY,frameWidth,frameHeight;
private int firstWith,firstHeight,firstPointx,firstPointy;
private BufferedImage bi,sbi,original;
private Robot robot;
private Rectangle rectangle;
private Rectangle rectangleCursor,rectangleCursorUp,rectangleCursorDown,rectangleCursorLeft,rectangleCursorRight;
private Rectangle rectangleCursorRU,rectangleCursorRD,rectangleCursorLU,rectangleCursorLD;
private Image bis;
private Dimension dimension;
private Button button,button2,clearButton;
private Point[] point=new Point[3];
private int width,height;
private int nPoints=5;
private Panel panel;
private boolean drawHasFinish=false,change=false;
private int changeFirstPointX,changeFirstPointY,changeWidth,changeHeight;
private boolean changeUP=false,changeDOWN=false,changeLEFT=false,changeRIGHT=false,changeRU=false,changeRD=false,changeLU=false,changeLD=false;
private boolean clearPicture=false,redraw=false;
private FileDialog fileDialog;
private AWTpicture(){
//取得屏幕大小
dimension=Toolkit.getDefaultToolkit().getScreenSize();
frameWidth=dimension.width;
frameHeight=dimension.height;
fileDialog=new FileDialog(this,"泡沫截图",FileDialog.SAVE);
rectangle=new Rectangle(frameWidth,frameHeight);
panel=new Panel();
button=new Button("退出");
button.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.setBackground(Color.green);
button2=new Button("截取");
button2.setBackground(Color.darkGray);
button2.addActionListener(new MyTakePicture(this));
button2.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.addActionListener(this);
clearButton=new Button("重绘");
clearButton.setBackground(Color.green);
clearButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
clearButton.addActionListener(new MyClearPicture(this));
panel.setLayout(new BorderLayout());
panel.add(clearButton, BorderLayout.SOUTH);
panel.add(button, BorderLayout.NORTH);
panel.add(button2, BorderLayout.CENTER);
try {
robot=new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//截取全屏
bi=robot.createScreenCapture(rectangle);
original=bi;
this.setSize(frameWidth,frameHeight);
this.setUndecorated(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.add(panel,BorderLayout.EAST);
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
this.setVisible(true);
this.repaint();
}
public static void main(String[] args){
new AWTpicture();
}
public void paint(Graphics g) {
this.drawR(g);
}
//缓存图片
public void update(Graphics g){
if(bis==null){
bis=this.createImage(frameWidth, frameHeight);
}
Graphics ga=bis.getGraphics();
Color c=ga.getColor();
ga.setColor(Color.black);
ga.fillRect(0, 0, frameWidth, frameHeight);
ga.setColor(c);
paint(ga);
g.drawImage(bis, 0, 0, frameWidth, frameHeight, null);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
if(!drawHasFinish){
if(point[1].x<point[2].x && point[1].y<point[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].x>point[2].x && point[1].y<point[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].x<point[2].x && point[1].y>point[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].x>point[2].x && point[1].y>point[2].y){
firstPointx=point[2].x;
firstPointy=point[2].y;
}
changeFirstPointX=firstPointx;
changeFirstPointY=firstPointy;
if(point[1]!=null && point[2]!=null ){
rectangleCursorUp=new Rectangle(firstPointx+20,firstPointy-10,width-40,20);
rectangleCursorDown=new Rectangle(firstPointx+20,firstPointy+height-10,width-40,20);
rectangleCursorLeft=new Rectangle(firstPointx-10,firstPointy+10,20,height-20);
rectangleCursorRight=new Rectangle(firstPointx+width-10,firstPointy+10,20,height-20);
rectangleCursorLU=new Rectangle(firstPointx-10,firstPointy-10,30,20);
rectangleCursorLD=new Rectangle(firstPointx-10,firstPointy+height-10,30,20);
rectangleCursorRU=new Rectangle(firstPointx+width-10,firstPointy-10,20,20);
rectangleCursorRD=new Rectangle(firstPointx+width-10,firstPointy+height-10,20,20);
drawHasFinish=true;
}
}
//确定每边能改变大小的矩形
if(drawHasFinish){
rectangleCursorUp=new Rectangle(changeFirstPointX+20,changeFirstPointY-10,changeWidth-40,20);
rectangleCursorDown=new Rectangle(changeFirstPointX+20,changeFirstPointY+changeHeight-10,changeWidth-40,20);
rectangleCursorLeft=new Rectangle(changeFirstPointX-10,changeFirstPointY+10,20,changeHeight-20);
rectangleCursorRight=new Rectangle(changeFirstPointX+changeWidth-10,changeFirstPointY+10,20,changeHeight-20);
rectangleCursorLU=new Rectangle(changeFirstPointX-2,changeFirstPointY-2,10,10);
rectangleCursorLD=new Rectangle(changeFirstPointX-2,changeFirstPointY+changeHeight-2,10,10);
rectangleCursorRU=new Rectangle(changeFirstPointX+changeWidth-2,changeFirstPointY-2,10,10);
rectangleCursorRD=new Rectangle(changeFirstPointX+changeWidth-2,changeFirstPointY+changeHeight-2,10,10);
}
}
public void mouseDragged(MouseEvent e) {
point[2]=e.getPoint();
//if(!drawHasFinish){
this.repaint();
// }
//托动鼠标移动大小
if(change){
if(changeUP){
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointY=e.getPoint().y;
}
if(changeDOWN){
changeHeight=e.getPoint().y-changeFirstPointY;
}
if(changeLEFT){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeFirstPointX=e.getPoint().x;
}
if(changeRIGHT){
changeWidth=e.getPoint().x-changeFirstPointX;
}
if(changeLU){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointX=e.getPoint().x;
changeFirstPointY=e.getPoint().y;
}
if(changeLD){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeHeight=e.getPoint().y-changeFirstPointY;
changeFirstPointX=e.getPoint().x;
}
if(changeRU){
changeWidth=e.getPoint().x-changeFirstPointX;
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointY=e.getPoint().y;
}
if(changeRD){
changeWidth=e.getPoint().x-changeFirstPointX;
changeHeight=e.getPoint().y-changeFirstPointY;
}
this.repaint();
}
}
public void mouseMoved(MouseEvent e) {
point[1]=e.getPoint();
//改变鼠标的形状
if(rectangleCursorUp!=null && rectangleCursorUp.contains(point[1])){
this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
change=true;
changeUP=true;
}else if(rectangleCursorDown!=null && rectangleCursorDown.contains(point[1])){
this.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
change=true;
changeDOWN=true;
}else if(rectangleCursorLeft!=null && rectangleCursorLeft.contains(point[1])){
this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
change=true;
changeLEFT=true;
}else if(rectangleCursorRight!=null && rectangleCursorRight.contains(point[1]) ){
this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
change=true;
changeRIGHT=true;
}else if(rectangleCursorLU !=null && rectangleCursorLU.contains(point[1])){
this.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
change=true;
changeLU=true;
}else if(rectangleCursorLD !=null && rectangleCursorLD.contains(point[1])){
this.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
change=true;
changeLD=true;
}else if(rectangleCursorRU!=null && rectangleCursorRU.contains(point[1])){
this.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
change=true;
changeRU=true;
}else if(rectangleCursorRD!=null && rectangleCursorRD.contains(point[1])){
this.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
change=true;
changeRD=true;
}else{
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
changeUP=false;changeDOWN=false;changeRIGHT=false;changeLEFT=false;changeRU=false;
changeRD=false;changeLU=false;changeLD=false;
}
redraw=false;
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
class MyTakePicture implements ActionListener{
AWTpicture aWTpicture;
MyTakePicture(AWTpicture aWTpicture){
this.aWTpicture=aWTpicture;
}
//保存图片
public void actionPerformed(ActionEvent e) {
fileDialog.setVisible(true);
if(changeWidth>0){
sbi=bi.getSubimage(changeFirstPointX,changeFirstPointY,changeWidth,changeHeight);
File file=new File(fileDialog.getDirectory());
file.mkdir();
try {
ImageIO.write(sbi, "jpeg",new File(file,fileDialog.getFile()+".jpg") );
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
class MyClearPicture implements ActionListener{
AWTpicture aWTpicture;
MyClearPicture(AWTpicture aWTpicture){
this.aWTpicture=aWTpicture;
}
public void actionPerformed(ActionEvent e) {
drawHasFinish=false;
change=false;
redraw=true;
rectangleCursorUp=null;
rectangleCursorDown=null;
rectangleCursorLeft=null;
rectangleCursorRight=null;
rectangleCursorRU=null;
rectangleCursorRD=null;
rectangleCursorLU=null;
rectangleCursorLD=null;
changeWidth=0;
changeHeight=0;
aWTpicture.repaint();
}
}
public void drawR(Graphics g){
g.drawImage(bi, 0,0,frameWidth,frameHeight, null);
if(point[1]!=null &&point[2]!=null &&!drawHasFinish && !redraw){
int[] xPoints={point[1].x,point[2].x,point[2].x,point[1].x,point[1].x};
int[] yPoints={point[1].y,point[1].y,point[2].y,point[2].y,point[1].y};
width=(point[2].x-point[1].x)>0?(point[2].x-point[1].x):(point[1].x-point[2].x);
height=(point[2].y-point[1].y)>0?(point[2].y-point[1].y):(point[1].y-point[2].y);
changeWidth=width;
changeHeight=height;
Color c=g.getColor();
g.setColor(Color.red);
g.drawString(width+"*"+height, point[1].x, point[1].y-5);
//画点
/*int i;
if()*/
if(point[1].x<point[2].x && point[1].y<point[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].x>point[2].x && point[1].y<point[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].x<point[2].x && point[1].y>point[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].x>point[2].x && point[1].y>point[2].y){
firstPointx=point[2].x;
firstPointy=point[2].y;
}
g.fillRect(firstPointx-2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+(width)/2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+width-2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+width-2,firstPointy+ height/2-2, 5,5);
g.fillRect(firstPointx+width-2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx+(width)/2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx-2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx-2,firstPointy+ height/2-2, 5,5);
//画矩形
//g.drawString("fafda", point[1].x-100, point[1].y-5);
g.drawPolyline(xPoints, yPoints, nPoints);
}
if(change){
g.setColor(Color.red);
g.drawString(changeWidth+"*"+changeHeight, changeFirstPointX, changeFirstPointY-5);
g.fillRect(changeFirstPointX-2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+(changeWidth)/2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY+ changeHeight/2-2, 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX+(changeWidth)/2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX-2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX-2,changeFirstPointY+ changeHeight/2-2, 5,5);
g.drawRect(changeFirstPointX, changeFirstPointY, changeWidth, changeHeight);
}
}
}
分享到:
相关推荐
java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作...
java 获取地址工具类 java 获取地址工具类java 获取地址工具类 java 获取地址工具类java 获取地址工具类 java 获取地址工具类java 获取地址工具类 java 获取地址工具类java 获取地址工具类 java 获取地址工具类java ...
Java截图工具源码是开发者们常用的实用工具之一,它能够帮助程序员在开发过程中快速获取屏幕快照,便于问题排查、功能演示或者用户交互设计。这个“很牛的Java截图工具源码”提供了一种高效、易用的方法来实现这一...
java 转义和反转义工具类 java 转义和反转义工具类java 转义和反转义工具类 java 转义和反转义工具类java 转义和反转义工具类 java 转义和反转义工具类java 转义和反转义工具类 java 转义和反转义工具类java 转义和...
java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表工具java报表...
JAVA截图小工具是一款基于Java编程语言开发的实用应用程序,它为用户提供了方便快捷的屏幕截图功能。通过这款工具,用户能够轻松捕获屏幕上的任何区域,并将其保存到本地,同时自动复制到剪贴板,方便进行后续的粘贴...
Java屏幕截图工具是一种基于Java编程语言开发的实用程序,它允许用户从计算机屏幕上捕捉图像,通常包括全屏、...虽然这个工具目前存在一些小bug,但通过不断改进和完善,可以将其发展成一个功能丰富的屏幕截图应用。
java ID生成器工具类 java ID生成器工具类java ID生成器工具类 java ID生成器工具类java ID生成器工具类 java ID生成器工具类java ID生成器工具类 java ID生成器工具类java ID生成器工具类 java ID生成器工具类java ...
使用这个工具,开发者可以轻松地在Java环境中实现视频截图功能,无论是在桌面应用还是服务器端服务中,都能灵活地处理视频数据。这对于视频分析、内容检索或者多媒体应用开发来说是非常有价值的。 总结起来,这个...
java 验证码工具类 java 验证码工具类 java 验证码工具类java 验证码工具类 java 验证码工具类 java 验证码工具类java 验证码工具类 java 验证码工具类 java 验证码工具类java 验证码工具类 java 验证码工具类 java ...
在本文中,我们将深入探讨如何使用Java来创建一个功能完备的截屏工具,涉及的技术点包括图形用户界面(GUI)、图像处理以及事件处理。 首先,我们需要了解Java中的AWT(Abstract Window Toolkit)和Swing库,这两个...
标题中的“一个Java写的日志分析工具”表明这是一个利用Java编程语言开发的软件工具,其主要功能是解析和分析日志文件。日志文件通常记录了应用程序运行过程中的各种事件和异常信息,对于调试、性能优化和问题排查...
"C转Java工具"就是这样一个工具,它能够将大部分C语言的语法转化为等效的Java语法,使得C语言编写的程序能够在Java平台上运行或者进行进一步的Java优化。这个工具的出现,对于那些已经拥有大量C语言代码库但又希望...
5. 学习价值:这个项目不仅是一个实用的工具,更是一个学习Java图形界面编程、网络编程和API整合的实战案例。对于希望提升Java编程技能或对开发桌面应用感兴趣的开发者来说,是一个不可多得的学习资源。 总结,...
2. **混合编程**:如果一个项目需要C++的高性能部分和Java的跨平台优势,转换工具可以用来创建接口,使两者协同工作。 3. **学习和理解**:开发者可以通过查看转换后的代码,更好地理解和学习两种语言的异同。 在...
Java仿QQ截图工具是一款基于Java编程语言开发的实用程序,旨在提供类似QQ截图的功能,让用户能够方便快捷地进行屏幕捕获。这款工具的核心亮点在于它的系统托盘集成,允许用户在后台轻松启动和操作截图功能,同时具备...
8. **用户界面**:对于一个工具来说,一个友好的用户界面是必要的。Java Swing或JavaFX可以用来创建图形界面,让用户设置截屏参数,如频率、保存路径、是否连续截取等。 9. **异常处理**:编写健壮的代码需要考虑...
描述中的重复信息进一步强调了这个工具的专注点,即C++到Java的代码转换。虽然没有提供具体工具的功能细节,但我们可以假设它至少具备以下功能: 1. **语法解析**:工具应能理解和解析C++源代码的语法,包括函数...
Java文件工具类
java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件上传工具类java 文件...