import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import java.awt.Rectangle;
public class j extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
static int x1=0,x2=0,y1=0,y2=0;
static int x=0,y=0,Height=0,Width=0;
public static void main(String[] args)
{
JFrame frame=new JFrame();
frame.setUndecorated(true);
frame.setSize(100,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JButton start=new JButton("开始截图");
start.setActionCommand("jietu");
start.setBounds(0,0,100,50);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
dothis();
//frame.setVisible(true);
}
});
frame.add(start);
JButton exit=new JButton("EXIT");
exit.setBounds(0,50,100,50);
exit.setActionCommand("EXIT");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
frame.add(exit);
}
static public void dothis()
{
JLabel lb = new JLabel();
lb.setBounds(100, 100, 100, 50);
lb.setBackground(Color.RED);
JFrame f=new JFrame();
int width=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int height=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
f.setUndecorated(true);
f.setSize(width,height);
f.setOpacity(0.2f);
f.setVisible(true);
f.add(lb);
Draw d=new Draw();
f.add(d);
f.addMouseListener(new MouseListener() { //为窗口添加鼠标事件监听器
@Override
public void mousePressed(MouseEvent e) {
//lb.setText(e.getX()+","+e.getY());
x1=e.getX();
y1=e.getY();
d.delivery1(x1,y1);
}
@Override
public void mouseClicked(MouseEvent e) {
//lb.setText(e.getX()+","+e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
//lb.setText(e.getX()+","+e.getY());
x2=e.getX();
y2=e.getY();
d.delivery2(x2,y2);
f.setOpacity(0.01f);
if(x1<x2)
{
x=x1;
Width=x2-x1;
}
else
{
x=x2;
Width=x1-x2;
}
if(y1<y2)
{
y=y1;
Height=y2-y1;
}
else
{
y=y2;
Height=y1-y2;
}
Rectangle re=new Rectangle();
re.x=x;
re.y=y;
re.height=Height;
re.width=Width;
try{
Robot robot=new Robot();
BufferedImage image=robot.createScreenCapture(re);
String filename="D:\\test";
String folder="11.png";
File screenFile=new File(filename);
if(!screenFile.exists())
{
screenFile.mkdir();
}
File f=new File(screenFile,folder);
ImageIO.write(image,"png",f);
}
catch(Exception g){
g.printStackTrace();
}
System.exit(0);
}
@Override
public void mouseEntered(MouseEvent e) {
//lb.setText(e.getX()+","+e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
//lb.setText(e.getX()+","+e.getY());
}
});
f.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e)
{
//lb.setText(e.getX()+","+e.getY());
x2=e.getX();
y2=e.getY();
d.delivery2(x2,y2);
}
public void mouseMoved(MouseEvent e)
{
//lb.setText(e.getX()+","+e.getY());
}
});
}
static class Draw extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private int X1=0,X2=0,Y1=0,Y2=0;
public void paint(Graphics g) {
g.drawLine(X1, Y1, X1, Y2);
g.drawLine(X1, Y1, X2, Y1);
g.drawLine(X1, Y2, X2, Y2);
g.drawLine(X2, Y1, X2, Y2);
}
public void delivery1(int x1,int y1)
{
X1=x1;
Y1=y1;
}
public void delivery2(int x2,int y2)
{
X2=x2;
Y2=y2;
repaint();
}
}
}