java 写的类似qq的截图工具_第1页
java 写的类似qq的截图工具_第2页
java 写的类似qq的截图工具_第3页
java 写的类似qq的截图工具_第4页
java 写的类似qq的截图工具_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、ÀàËÆQQ½Øͼ¹¤¾ß£ºpackage Util;import java.awt.*;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.event.*;import javax.swing.*;

2、import java.io.*;import javax.imageio.*;import java.awt.image.*;/* author jacky.zhu*/public class CaptureScreen extends JFrame implements ActionListenerprivate JButton start,cancel,save;private BufferedImage get;/* Creates a new instance of CaptureScreen */public CaptureScreen() doStart();/Ò

3、87;¸öÔÝʱÀ࣬ÓÃÓÚÏÔʾµ±Ç°µÄÆÁĻͼÏñprivate class Temp extends JPanel implements MouseListener,MouseMotionListenerprivate BufferedImage bi;pr

4、ivate int width,height;private int startX,startY,endX,endY,tempX,tempY;private JFrame jf;private Rectangle select=new Rectangle(0,0,0,0);/±íʾѡÖеÄÇøÓòprivate Cursor cs;/±íʾһ°ãÇé

5、;¿öϵÄÊó±ê״̬private States current=States.DEFAULT;/ ±íʾµ±Ç°µÄ±à¼­×´Ì¬private Rectangle rec;/±íʾ°Ë¸&#

6、246;±à¼­µãµÄÇøÓòpublic Temp(JFrame jf,BufferedImage bi,int width,int height)this.jf=jf;this.bi=bi;this.width=width;this.height=height;this.addMouseListener(this);this.addMouseMotionListener(this);Image icon=Toolkit.getDefaultToolkit().creat

7、eImage(this.getClass().getResource("icon.png");cs=Toolkit.getDefaultToolkit().createCustomCursor(icon,new Point(0,0),"icon");this.setCursor(cs);initRecs();private void initRecs()rec=new Rectangle8;for(int i=0;i<rec.length;i+)reci=new Rectangle();public void paintComponent(Grap

8、hics g)g.drawImage(bi,0,0,width,height,this);g.setColor(Color.RED);g.drawLine(startX,startY,endX,startY);g.drawLine(startX,endY,endX,endY);g.drawLine(startX,startY,startX,endY);g.drawLine(endX,startY,endX,endY);int x=startX<endX?startX:endX;int y=startY<endY?startY:endY;select=new Rectangle(x,

9、y,Math.abs(endX-startX),Math.abs(endY-startY);int x1=(startX+endX)/2;int y1=(startY+endY)/2;g.fillRect(x1-2,startY-2,5,5);g.fillRect(x1-2,endY-2,5,5);g.fillRect(startX-2,y1-2,5,5);g.fillRect(endX-2,y1-2,5,5);g.fillRect(startX-2,startY-2,5,5);g.fillRect(startX-2,endY-2,5,5);g.fillRect(endX-2,startY-2

10、,5,5);g.fillRect(endX-2,endY-2,5,5);rec0=new Rectangle(x-5,y-5,10,10);rec1=new Rectangle(x1-5,y-5,10,10);rec2=new Rectangle(startX>endX?startX:endX)-5,y-5,10,10);rec3=new Rectangle(startX>endX?startX:endX)-5,y1-5,10,10);rec4=new Rectangle(startX>endX?startX:endX)-5,(startY>endY?startY:en

11、dY)-5,10,10);rec5=new Rectangle(x1-5,(startY>endY?startY:endY)-5,10,10);rec6=new Rectangle(x-5,(startY>endY?startY:endY)-5,10,10);rec7=new Rectangle(x-5,y1-5,10,10);public void mouseMoved(MouseEvent me)if(select.contains(me.getPoint()this.setCursor(new Cursor(Cursor.MOVE_CURSOR);current=States

12、.MOVE; elseStates st=States.values();for(int i=0;i<rec.length;i+)if(reci.contains(me.getPoint()current=sti;this.setCursor(sti.getCursor();return;this.setCursor(cs);current=States.DEFAULT;public void mouseExited(MouseEvent me)public void mouseEntered(MouseEvent me)public void mouseDragged(MouseEve

13、nt me)int x=me.getX();int y=me.getY();if(current=States.MOVE)startX+=(x-tempX);startY+=(y-tempY);endX+=(x-tempX);endY+=(y-tempY);tempX=x;tempY=y;else if(current=States.EAST)if(startX>endX)startX+=(x-tempX);tempX=x; elseendX+=(x-tempX);tempX=x;else if(current=States.NORTH)if(startY<endY)startY+

14、=(y-tempY);tempY=y;elseendY+=(y-tempY);tempY=y;else if(current=States.WEST)if(startX<endX)startX+=(x-tempX);tempX=x; elseendX+=(x-tempX);tempX=x;else if(current=States.SOUTH)if(startY>endY)startY+=(y-tempY);tempY=y;elseendY+=(y-tempY);tempY=y; else if(current=States.NORTH_EAST)if(startX>end

15、X)startX+=(x-tempX);tempX=x; elseendX+=(x-tempX);tempX=x;if(startY<endY)startY+=(y-tempY);tempY=y;elseendY+=(y-tempY);tempY=y;else if(current=States.NORTH_WEST)if(startX<endX)startX+=(x-tempX);tempX=x; elseendX+=(x-tempX);tempX=x;if(startY<endY)startY+=(y-tempY);tempY=y;elseendY+=(y-tempY);

16、tempY=y;else if(current=States.SOUTH_EAST)if(startY>endY)startY+=(y-tempY);tempY=y;elseendY+=(y-tempY);tempY=y;if(startX>endX)startX+=(x-tempX);tempX=x; elseendX+=(x-tempX);tempX=x;else if(current=States.SOUTH_WEST)if(startY>endY)startY+=(y-tempY);tempY=y;elseendY+=(y-tempY);tempY=y;if(star

17、tX<endX)startX+=(x-tempX);tempX=x; elseendX+=(x-tempX);tempX=x;elsestartX=tempX;startY=tempY;endX=me.getX();endY=me.getY();this.repaint();public void mousePressed(MouseEvent me)tempX=me.getX();tempY=me.getY();public void mouseReleased(MouseEvent me)System.out.println("mouseReleased");if

18、(me.isPopupTrigger()if(current=States.MOVE)startX=0;startY=0;endX=0;endY=0;repaint(); elsejf.dispose();/doSave();public void mouseClicked(MouseEvent me)if(me.getClickCount()=2)/Rectangle rec=new Rectangle(startX,startY,Math.abs(endX-startX),Math.abs(endY-startY);Point p=me.getPoint();if(select.conta

19、ins(p)if(select.x+select.width<this.getWidth()&&select.y+select.height<this.getHeight()get=bi.getSubimage(select.x,select.y,select.width,select.height);jf.dispose();/save.setEnabled(true);doSave();elseint wid=select.width,het=select.height;if(select.x+select.width>=this.getWidth()wi

20、d=this.getWidth()-select.x;if(select.y+select.height>=this.getHeight()het=this.getHeight()-select.y;get=bi.getSubimage(select.x,select.y,wid,het);jf.dispose();/save.setEnabled(true);doSave();private void doStart()tryRobot ro=new Robot();Toolkit tk=Toolkit.getDefaultToolkit();Dimension di=tk.getSc

21、reenSize();Rectangle rec=new Rectangle(0,0,di.width,di.height);BufferedImage bi=ro.createScreenCapture(rec);JFrame jf=new JFrame();jf.getContentPane().add(new Temp(jf,bi,di.width,di.height);jf.setUndecorated(true);jf.setSize(di);jf.setVisible(true);jf.setAlwaysOnTop(true);catch(Exception exe)exe.pri

22、ntStackTrace();private void doSave()throws NullPointerExceptionsetClipboardImage2(get);/*tryFile file = new File("d:/temp/123.jpg");/ÕâÀïÎÒдËÀÁË·¾¶ºÍÀàÐÍjpgString about="jpg

23、"String ext=file.toString().toLowerCase();System.out.println(ext);ImageIO.write(get,about,file); catch(Exception exe)exe.printStackTrace();*/public void actionPerformed(ActionEvent ae)if(ae.getSource()=start)doStart(); else if(ae.getSource()=cancel)/System.exit(0); else if(ae.getSource()=save)d

24、oSave();protected static void setClipboardImage2(final Image image) Transferable trans = new Transferable()public DataFlavor getTransferDataFlavors() return new DataFlavor DataFlavor.imageFlavor ;public boolean isDataFlavorSupported(DataFlavor flavor) return DataFlavor.imageFlavor.equals(flavor);public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException if(isDataFlavorSupported(flavor)return image;throw new UnsupportedFlavorException(flavor);Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论