1、javajava 编写截图工具编写截图工具java 编写截图工具 2010-05-04 21:16/* 作者:xdj 功能:用于截取图片,方便快捷! */ 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 j
2、ava.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,
3、 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 drawHa
4、sFinish = 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 = dimensi
5、on.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(“重绘“); clearBu
6、tton.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(t
7、rue); 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
8、, 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 firstPointy = point2.y; if (point1.x point2.x first
9、Pointy = point2.y; changeFirstPointX = firstPointx; changeFirstPointY = firstPointy; if (point1 != null 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,
《java编写截图工具》由会员子分享,可在线阅读,更多相关《java编写截图工具》请在金锄头文库上搜索。