`
ruantongsheng
  • 浏览: 21150 次
  • 来自: ...
社区版块
存档分类
最新评论

ScreenCapture

 
阅读更多
package com;

import java.awt.AWTEvent;

public class MyScreenCapture extends JDialog{
ImageLabel imgLabel;
int xStart=0;
int yStart=0;
int subimageX=0;
int subimageY=0;
int subimageW=0;
int subimageH=0;
private BufferedImage fullScreenImage=null;
public static void main(String[] args) {
MyScreenCapture sc=null;
try {
Thread.sleep(300);
sc=new MyScreenCapture();

} catch (Exception e) {
JOptionPane.showMessageDialog(sc, e.getMessage());
e.printStackTrace();
}
}
public MyScreenCapture() {
setUndecorated(true);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setAlwaysOnTop(true);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setModal(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
JPanel imgPanel = new JPanel();
getContentPane().add(imgPanel, BorderLayout.CENTER);
imgPanel.setLayout(new BorderLayout(0, 0));

  imgLabel = new ImageLabel();
imgPanel.add(imgLabel, BorderLayout.CENTER);

//===============
imgLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent paramMouseEvent) {
xStart=paramMouseEvent.getX();
yStart=paramMouseEvent.getY();
}
public void mouseReleased(MouseEvent paramMouseEvent) {
System.out.println("DDDDDDD");
if(paramMouseEvent.getButton()==3){
System.exit(0);
}
if(subimageY>0&&subimageX>0){
BufferedImage pickedImage = fullScreenImage.getSubimage(subimageX, subimageY, subimageW,
subimageH);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
MyImageTransferable it = new MyImageTransferable(new ImageIcon(pickedImage));
cb.setContents(it, it);
System.exit(0);
}
}
});
imgLabel.addMouseMotionListener(new MouseMotionListener() {

public void mouseMoved(MouseEvent paramMouseEvent) {
}
public void mouseDragged(MouseEvent paramMouseEvent) {
//paintRectOnImglabel(imgLabel.getGraphics(), paramMouseEvent.getX(),paramMouseEvent.getY());

int w=Math.abs(paramMouseEvent.getX()-xStart);
int h=Math.abs(paramMouseEvent.getY()-yStart);
int x=Math.min(xStart,paramMouseEvent.getX());
int y=Math.min(yStart,paramMouseEvent.getY());
subimageX=x;
subimageY=y;
subimageW=w;
subimageH=h;
imgLabel.drawRectangle(x, y, w, h);

}
});
setLabelIcon(imgLabel);
setVisible(true);
}

private void setLabelIcon(JLabel label){
try {
if(fullScreenImage==null){
Robot robot =new Robot();
fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize()));
}
ImageIcon icon = new ImageIcon(fullScreenImage);
label.setIcon(icon);
} catch (Exception e) {
e.printStackTrace();
}
}

}
class ImageLabel extends JLabel {
int lineX, lineY;
int x, y, h, w;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, w, h);
String area = Integer.toString(w) + " * " + Integer.toString(h);
g.setColor(Color.blue);
g.drawString(area, x + (int) w / 2 - 15, y + (int) h / 2);
}
public void drawRectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
h = height;
w = width;
repaint();
}
public void drawCross(int x, int y) {
lineX = x;
lineY = y;
repaint();
}
}
class MyImageTransferable implements Transferable,ClipboardOwner{

private ImageIcon image=null;
public MyImageTransferable(ImageIcon img) {
this.image=img;
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("lostOwnership");
}
public Object getTransferData(DataFlavor arg0)
throws UnsupportedFlavorException, IOException {
return this.image.getImage();
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor []flavor=new DataFlavor[1];
flavor[0]=DataFlavor.imageFlavor;
return flavor;
}
public boolean isDataFlavorSupported(DataFlavor arg0) {
if(arg0.equals(DataFlavor.imageFlavor)){
System.out.println("support impageFlavor");
return true;
}
return false;
}


}
class MyAWTEvent implements AWTEventListener{
@Override
public void eventDispatched(AWTEvent paramAWTEvent) {
System.out.println("MMMMMMMMMMMMSSSSSSSS");

}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics