java 截图工具.doc_第1页
java 截图工具.doc_第2页
java 截图工具.doc_第3页
java 截图工具.doc_第4页
java 截图工具.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

java 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 Point3; 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 (point1.x point2.x & point1.y point2.x & point1.y point2.y) firstPointx = point2.x; firstPointy = point1.y; if (point1.x point2.y) firstPointx = point1.x; firstPointy = point2.y; if (point1.x point2.x & point1.y point2.y) firstPointx = point2.x; firstPointy = point2.y; changeFirstPointX = firstPointx; changeFirstPointY = firstPointy; if (point1 != null & point2 != 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) point2 = 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) point1 = e.getPoint(); / if (rectangleCursorUp != null & rectangleCursorUp.contains(point1) this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR); change = true; changeUP = true; else if (rectangleCursorDown != null & rectangleCursorDown.contains(point1) this.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR); change = true; changeDOWN = true; else if (rectangleCursorLeft != null & rectangleCursorLeft.contains(point1) this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR); change = true; changeLEFT = true; else if (rectangleCursorRight != null & rectangleCursorRight.contains(point1) this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR); change = true; changeRIGHT = true; else if (rectangleCursorLU != null & rectangleCursorLU.contains(point1) this.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR); change = true; changeLU = true; else if (rectangleCursorLD != null & rectangleCursorLD.contains(point1) this.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR); change = true; changeLD = true; else if (rectangleCursorRU != null & rectangleCursorRU.contains(point1) this.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR); change = true; changeRU = true; else if (rectangleCursorRD != null & rectangleCursorRD.contains(point1) 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; Override 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 (point1 != null & point2 != null & !drawHasFinish & !redraw) int xPoints = point1.x, point2.x, point2.x, point1.x, point1.x ; int yPoints = point1.y, point1.y, point2.y, point2.y, point1.y ; width = (point2.x - point1.x) 0 ? (point2.x - point1.x) : (point1.x - point2.x); height = (point2.y - point1.y) 0 ? (point2.y - point1.y) : (point1.y - point2.y); changeWidth = width; changeHeight = height; Color c = g.getColor(); g.setColor(Color.red); g.drawString(width + * + height, point1.x, point1.y - 5); / /* int i; if() */ if (point1.x point2.x & point1.y point2.x & point1.y point2.y) firstPointx = point2.x; firstPointy = point1.y; if (point1.x point2.y) firstPointx = point1.x; firstPointy = point2.y; if (point1.x point2.x & point1.y point2.y) firstPointx = point2.x; firstPointy = point2.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 -

温馨提示

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

最新文档

评论

0/150

提交评论