可以在所截全屏图上连续画任意矩形,有面积标示:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ScreenCaptureTest {
// singleton design pattern
private static ScreenCaptureTest defaultCapturer = new ScreenCaptureTest();
private BackgroundImage backgroundImage = new BackgroundImage();
private Robot robot;
// it's the dialog for holding the background image
private JDialog dialog = new JDialog();
private ScreenCaptureTest capture;
// the captured rectangle
private int recX, recY, recH, recW;
private int x1, y1, x2, y2;
private boolean isFirstPoint = true;
public static void main(String[] args) throws Exception {
ScreenCaptureTest.getInstance().go();
}
public void go() {
capture = ScreenCaptureTest.getInstance();
capture.captureFullScreen();
dialog.setVisible(true);
}
private ScreenCaptureTest() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
dialog.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
dialog.setAlwaysOnTop(true);
dialog.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
dialog.setSize(dialog.getMaximumSize());
dialog.setUndecorated(true);
dialog.setModal(true);
JPanel contentpane = (JPanel) dialog.getContentPane();
contentpane.setLayout(new BorderLayout());
contentpane.add(BorderLayout.CENTER, backgroundImage);
backgroundImage.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evn) {
if ((evn.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
System.out.println("mouse right key clicked...");
System.exit(0);
}
}
public void mouseReleased(MouseEvent evn) {
isFirstPoint = true;
}
});
backgroundImage.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evn) {
if (isFirstPoint) {
x1 = evn.getX();
y1 = evn.getY();
isFirstPoint = false;
System.out.println("x1 = " + x1);
} else {
x2 = evn.getX();
y2 = evn.getY();
int maxX = Math.max(x1, x2);
int maxY = Math.max(y1, y2);
int minX = Math.min(x1, x2);
int minY = Math.min(y1, y2);
recX = minX;
recY = minY;
recW = maxX - minX;
recH = maxY - minY;
backgroundImage.locateClippedRectangle(recX, recY, recW, recH);
System.out.println("x2 = " + x2);
}
}
public void mouseMoved(MouseEvent e) {
backgroundImage.locateCrossLine(e.getX(), e.getY());
}
});
}
// Singleton Pattern
public static ScreenCaptureTest getInstance() {
return defaultCapturer;
}
/**
* capture the full screen and make it background image displayable
*/
public void captureFullScreen() {
BufferedImage fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIcon icon = new ImageIcon(fullScreenImage);
backgroundImage.setIcon(icon);
}
}
class BackgroundImage extends JLabel {
private static final long serialVersionUID = 1L;
// the clipped rectangle left upper point and width height
private int x, y, w, h;
// cross line x and y coordinates
private int lineX, lineY;
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawClippedRectangle(g);
drawCrossLine(g);
}
public void locateClippedRectangle(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
repaint();
}
public void locateCrossLine(int x, int y) {
lineX = x;
lineY = y;
repaint();
}
private void drawClippedRectangle(Graphics g) {
g.setColor(Color.DARK_GRAY);
g.drawRect(x, y, w, h);
g.setColor(Color.LIGHT_GRAY);
String area = w + " × " + h;
// the number is tried to adapt to the center of the rectangle
g.drawString(area, x + (int)(w / 2 - area.length() * 2 - 5), y + (int) h / 2 + 5);
}
private void drawCrossLine(Graphics g) {
g.setColor(Color.RED);
// draw the vertical line
g.drawLine(lineX, 0, lineX, lineY - 10);
g.drawLine(lineX, lineY + 10, lineX, getHeight());
// draw the horizontal line
g.drawLine(0, lineY, lineX - 10, lineY);
g.drawLine(lineX + 10, lineY, getWidth(), lineY);
}
}