如题,可以监听鼠标单击、双击、移动等,谢谢了。急。
解决方案 »
- 一道面试题
- 迷茫了
- 谁给我分析分析这个问题,谢啦
- Hibernate 的外键问题,跪求各位大侠帮忙!!!
- 我的List list=new ArrayList();为什么编译通不过?提示:不可达代码?
- 再谈一个设计理念,献给所有想提高的java程序员!(续第一篇)
- 急!有关毕业设计的问题,稍微熟悉JBuilder中opentools的请进!
- 听说用jbuilder可以实现代码混淆,如何做?效果如何?还有更好的办法吗
- 谁有java核心技术的五章的MailboxTest.java的代码?
- jdk的配置问题,随你要多少分,我的3000分全给你都行,急啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- Oracle 10g操作异常,很奇怪,大家快来围观。。
- 不知道怎么用java解析XML.新人求帮助。
要用 jni 么?
你运行下这个试一试
package ui.frame;import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;import com.sun.awt.AWTUtilities;public class PrintScreen {
private JFrame frame;
private GraphicsEnvironment ge;
private GraphicsDevice gd;
private DisplayMode dm;
private JFrame f;
private Graphics g;
private int x1,y1;
private int x2,y2;
private boolean b1 = true;
public void createAndShowUI(){
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
dm = gd.getDisplayMode();
int dim_w = 200;
int dim_h = 200;
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setUndecorated(false);
frame.setPreferredSize(new Dimension(dim_w, dim_h));
Container contentPane = frame.getContentPane();
JButton b1 = new JButton("截图");
b1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
printScreen();
}
}); contentPane.add(b1,BorderLayout.SOUTH);
//
int x = dm.getWidth();
int y = dm.getHeight();
frame.setLocation(x - dim_w, 0);
frame.pack();
frame.setVisible(true);
}
private void printScreen(){
createFrame();
}
private void createFrame(){
f = new JFrame("选择区域");
f.setPreferredSize(new Dimension(dm.getWidth(),dm.getHeight()));
f.setUndecorated(true);
// gd.setFullScreenWindow(f); f.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
if(b1==true){
x2 = e.getXOnScreen();
y2 = e.getYOnScreen();
b1 = false;
g.setColor(Color.BLACK);
g.drawRect(x1, y1, (x2-x1), (y2-y1));
// System.out.println("released " + x2 + " : " + y2);
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(b1==true){
x1 = e.getXOnScreen();
y1 = e.getYOnScreen();
// System.out.println("pressed " + x1 + " : " + y1);
}
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getClickCount()==2){
createImage();
f.setVisible(false);
f.dispose();
}
}
});
f.pack();
f.setVisible(true);
g = f.getGraphics();
AWTUtilities.setWindowOpacity(f, 0.1f);
// AWTUtilities.setWindowOpaque(f, false); //window does not support this effects
}
private void createImage(){
System.out.println("双击截图");
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Rectangle screenRect = null;
screenRect = new Rectangle(new Dimension((x2-x1),(y2-y1)));
screenRect.setLocation(x1, y1);
BufferedImage image = robot.createScreenCapture(screenRect);
FileOutputStream fos = null;
// 生成全球唯一id
UUID uuid = UUID.randomUUID();
String file = "F:\\screen" + uuid + ".jpg";
try {
fos = new FileOutputStream(new File(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ImageIO.write(image, "JPG", fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
final PrintScreen ps = new PrintScreen();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
ps.createAndShowUI();
}
});
}
}