java 截图工具.doc
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.y) firstPointx = point1.x; firstPointy = point1.y; if (point1.x > point2.x && point1.y < point2.y) firstPointx = point2.x; firstPointy = point1.y; if (point1.x < point2.x && point1.y > 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, firstPoi