// 窗口绘画方法
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image, 0, 0, new ImageObserver() {
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
// TODO Auto-generated method stub
return false;
}
});
g.drawRect(x, y, w, h);
}
int x, y;
int w, h;
boolean b = false;
public void mousePressed(MouseEvent e) { // 按下
if (b == false) {
x = e.getX();
y = e.getY();
}
}
public void mouseReleased(MouseEvent e) { // 弹起
if (b == false) {
w = e.getX() - x;
h = e.getY() - y;
this.repaint();
b = true;
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (b && e.getClickCount() == 2) {// 你的鼠标有没有双击
try {
// 把截图好的照片保存起来
Robot r = new Robot();
final BufferedImage image = r
.createScreenCapture(new Rectangle(x + 1, y + 1, w - 1,
h - 1));
// ImageIO.write(image, "jpeg", new File("c:/a.jpg"));
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);
}