想做个Java的局域网相互传东西和聊天,正巧遇到想要截图在网上找来找去没有找到看起来合适的,最想要的还是有点阴影凸显一下截图区域,然后在截图区写字画图形。
思路是这样:1屏幕截图放到背景面板,2在用一个面板盖住它,3在盖住的面板上用Graphics画出框选区域,4在框选的区域在进行图图画画,5然后再把款框选的区域再截图一次保存下来。
因为网上找不到类似的然后我就分享出来,方便有想法的小伙伴不用重复造轮子了。
对于java,屏幕截图可以主要通过一个robot来实现。使用robot类需要导入java.awt包,操作如下
截图的时候 左键双击保存、右键一次重选,两次退出
import CaptureScreen.filter.BMPfilter;
import CaptureScreen.filter.GIFfilter;
import CaptureScreen.filter.JPGfilter;
import CaptureScreen.filter.PNGfilter;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
public class MyScreencx {
private JPanel beijing;
private JPanel right;
private JPanel top;
private JPanel left;
private JPanel bottom;
private JButton doCopyButton;
private JPanel controlPanel;
private JButton closeButton;
private JButton doSaveButton;
private JButton boxButton;
private JRadioButton mindian;
private JRadioButton defaultdian;
private JRadioButton maxdian;
private JRadioButton ashkuang;
private JRadioButton bluekuang;
private JRadioButton greenkuang;
private JRadioButton redkuang;
private JRadioButton whitekuang;
private JRadioButton yellowkuang;
private JButton circleButton;
private JButton ctrlZButton;
private JButton brushButton;
private JButton textButton;
private int startX, startY, endX, endY;
private int startXTemp, startYTemp, endXTemp, endYTemp;
private int smoveX, smoveY, emoveX, emoveY;
private boolean topMove = false, leftMove = false;
private boolean move = false;
// 画框的大小
private int boxStartX, boxStartY, boxEndX, boxEndY;
private Dimension screenSize;
private BufferedImage bufImage;
private static int width = 800;
private static int height = 500;
private ButtonGroup groupsize;
private ButtonGroup groupcolor;
private boolean isShowTip = true;//是否显示提示.如果鼠标左键一按,则提示就不再显示了
private int isShowTipCount = 0;// 显示提示计数
private Point showTipPoint = new Point();
private boolean isbBox = false;
private boolean isCircle = false;
private boolean isBrush = false;
private boolean isText = false;
private boolean isCtrlZ = false;
private JPanel frameSelection;
private int mouseEvent = 0;
private List<JSONArray> boxArr = new ArrayList<>();// 保存矩形数组
private List<JSONArray> circleArr = new ArrayList<>();// 保存圆形数组
private ArrayList<BrushLine> brushArr = new ArrayList<>(); // 保存画笔画形
private ArrayList<Map<String, Object>> textArr = new ArrayList<>(); // 保存画笔画形
private ArrayList<Point> brush = null; // 当前画笔画形
private List<Object> ctrlZList = new ArrayList<>();
private boolean[] clickedMoveStatus = {
false, false, false, false};// 按下/移动/抬起/点击
private int[] frameSelectionArea = {
0, 0}; // 选择框的面积0w 1h
private String paintbrush = "";
private GridBagLayout gridBagLayout = new GridBagLayout();
private GridBagConstraints gridBagConstraints = new GridBagConstraints();
private JPanel toptext, lefttext, righttext, buttomtext;
public MyScreencx(JFrame jFrame) {
addButtonGroup();
// 获取屏幕尺寸
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
width = screenSize.width;
height = screenSize.height;
// 创建需要截取的矩形区域
Rectangle rect = new Rectangle(0, 0, screenSize.width, screenSize.height);
try {
bufImage = new Robot().createScreenCapture(rect);// 截屏操作
} catch (AWTException e) {
e.printStackTrace();
}
beijing.add(createSelectionPanel(jFrame));
boxButton.addActionListener(e -> {
restoreReleasedDragged();
isbBox = true;
paintbrush = "box";
controlPanel.setSize(new Dimension(controlPanel.getWidth(), 80));
ctrlZButton.setEnabled(true);
ctrlZButton.requestFocus(true);
});
circleButton.addActionListener(e -> {
restoreReleasedDragged();
isCircle = true;
paintbrush = "circle";
controlPanel.setSize(new Dimension(controlPanel.getWidth(), 80));
ctrlZButton.setEnabled(true);
ctrlZButton.requestFocus(true);
});
brushButton.addActionListener(e -> {
restoreReleasedDragged();
isBrush = true;
paintbrush = "brush";
controlPanel.setSize(new Dimension(controlPanel.getWidth(), 80));
ctrlZButton.setEnabled(true);
ctrlZButton.requestFocus(true);
});
textButton.addActionListener(e -> {
restoreReleasedDragged();
isText = true;
paintbrush = "text";
controlPanel.setSize(new Dimension(controlPanel.getWidth(), 80));
ctrlZButton.setEnabled(true);
ctrlZButton.requestFocus(true);
});
ctrlZButton.addActionListener(e -> ctrlZClear());
closeButton.addActionListener(e -> jFrame.dispose());
doSaveButton.addActionListener(e -> doSaveBufImage(jFrame));
doCopyButton.addActionListener(e -> doCopyBufImage(jFrame));
}
// 判断画笔状态
public boolean isPaintComponent() {
return isCtrlZ || isText || isBrush || isCircle || isbBox;
}
// 判断画笔状态
public boolean isReleasedDragged() {
return !isbBox && !isCircle && !isBrush && !isText;
}
// 还原画笔状态
public void restoreReleasedDragged() {
isText = false;
isBrush = false;
isCircle = false;
isbBox = false;
}
/** * 框选选中面板* * * @param jFrame * @return */
public JPanel createSelectionPanel(JFrame jFrame) {
// 创建选择帧
frameSelection = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
if (clickedMoveStatus[0] && clickedMoveStatus[1] || isPaintComponent()) {
g.setColor(Color.green);
for (int i = 0; i < 2; i++) {
g.drawLine(startX, startY - i, endX, startY - i);
g.drawLine(startX, endY - i, endX, endY - i);
g.drawLine(startX + i, startY, startX + i, endY);
g.drawLine(endX - i, startY, endX - i, endY);
}
g.fillRect(startX - 5, startY - 5, 10, 10); // 上线的 起点描点
g.fillRect((startX + endX) / 2 - 5, startY - 5, 10, 10); // 上线的 中间描点
g.fillRect(endX - 5, startY - 5, 10, 10); // 上线的 终点描点
g.fillRect(startX - 5, endY - 5, 10, 10); // 下线的 起点描点
g.fillRect((startX + endX) / 2 - 5, endY - 5, 10, 10); // 下线的 中间描点
g.fillRect(endX - 5, endY - 5, 10, 10); // 下线的 终点描点
g.fillRect(startX - 5, (startY + endY) / 2 - 5, 10, 10); // 右线的 中间描点
g.fillRect(endX - 5, (startY + endY) / 2 - 5, 10, 10); // 左线的 中间描点
boxWrite(g); // 画出存量图形
int lineSize;
if (paintbrush.equals("box")) {
g.setColor(Color.decode(groupcolor.getSelection().getActionCommand())); //设置颜色
lineSize = Integer.parseInt(groupsize.getSelection().getActionCommand());
BasicStroke stokeLine = new BasicStroke(1.0f);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stokeLine);
for (int i = 0; i < lineSize; i++) {
g.drawLine(boxStartX, boxStartY - i, boxEndX, boxStartY - i);
g.drawLine(boxStartX, boxEndY - i, boxEndX, boxEndY - i);
g.drawLine(boxStartX + i, boxStartY, boxStartX + i, boxEndY);
g.drawLine(boxEndX - i, boxStartY, boxEndX - i, boxEndY);
}
} else if (paintbrush.equals("circle")) {
g.setColor(Color.decode(groupcolor.getSelection().getActionCommand())); //设置颜色
lineSize = Integer.parseInt(groupsize.getSelection().getActionCommand());
BasicStroke stokeLine = new BasicStroke(2.0f);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stokeLine);
for (int i = 0; i < lineSize; i++) {
if (boxStartX < boxEndX)
g.drawOval(boxStartX + i, boxStartY + i, (boxEndX - i * 2) - boxStartX, (boxEndY - i * 2) - boxStartY);
if (boxStartX > boxEndX)
g.drawOval(boxEndX + i, boxStartY + i, (boxStartX - i * 2) - boxEndX, (boxEndY - i * 2) - boxStartY);
if (boxEndY < boxStartY)
g.drawOval(boxStartX + i, boxEndY + i, (boxEndX - i * 2) - boxStartX, (boxStartY - i * 2) - boxEndY);
if (boxStartY > boxEndY && boxStartX > boxEndX)
g.drawOval(boxEndX + i, boxEndY + i, (boxStartX - i * 2) - boxEndX, (boxStartY - i * 2) - boxEndY);
}
} else if (paintbrush.equals("brush")) {
g.setColor(Color.decode(groupcolor.getSelection().getActionCommand())); //设置颜色
lineSize = Integer.parseInt(groupsize.getSelection().getActionCommand());
BasicStroke stokeLine = new BasicStroke(lineSize);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stokeLine);
if (brush.size() >= 2) {
for (int i = 1; i < brush.size(); i++) {
g.drawLine(brush.get(i - 1).x, brush.get(i - 1).y, brush.get(i).x, brush.get(i).y);
}
}
}
}
if (!topMove && !leftMove) bottomRight();
if (topMove && !leftMove) topLeft();
if (!topMove && leftMove) bottomLeft();
if (topMove && leftMove) topRight();
if (endX > startX) controlPanel.setBounds(endX - controlPanel.getWidth(), 0, controlPanel.getWidth(), controlPanel.getHeight());
else controlPanel.setBounds(startX - controlPanel.getWidth(), 0, controlPanel.getWidth(), controlPanel.getHeight());
if (isShowTip) {
int height = 30;
if (isShowTipCount == 2) height = 50;
g.setColor(Color.WHITE);
g.fillRect(showTipPoint.x + 10, showTipPoint.y + 25, 230, height);
g.setColor(new Color(62, 62, 62));
g.setFont(new Font("微软雅黑", Font.PLAIN, 18));
Graphics2D g2d = (Graphics2D) g;
g2d.drawRoundRect(showTipPoint.x + 10, showTipPoint.y + 25, 230, height, 15, 15);
g.drawString("按住鼠标左键选择截图区", showTipPoint.x + 15, showTipPoint.y + 47);
if (isShowTipCount == 2)
g.drawString("再按一次右键退出截图", showTipPoint.x + 15, showTipPoint.y + 67);
}
}
};
frameSelection.setOpaque(false);
bottom.setBackground(new Color(0, 0, 0, 0));
controlPanel.setBackground(new Color(0, 0, 0, 0));
bottom.setOpaque(false);
frameSelection.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
if ("true".equals(controlPanel.getName())) {
beijing.remove(frameSelection);
bottom.setBackground(new Color(0, 0, 0, 0));
beijing.setVisible(false);
beijing.setVisible(true);
beijing.setVisible(false);
beijing.setVisible(true);
startX = 0;
startY = 0;
endX = 0;
endY = 0;
beijing.add(createSelectionPanel(jFrame));
controlPanel.setName("false");
move = false;
restoreReleasedDragged(); // 还原画笔状态
boxArr.clear();
circleArr.clear();
textArr.clear();
brushArr.clear();
if (brush != null) brush.clear();
boxStartX = 0;
boxStartY = 0;
boxEndX = 0;
boxEndY = 0;
isShowTip = true;
isShowTipCount = 2;
} else jFrame.dispose();
}
if (e.getClickCount() == 2) doCopyBufImage(jFrame);
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
mouseEvent = MouseEvent.BUTTON3;
return;
} else mouseEvent = e.getButton();
isShowTip = false;
clickedMoveStatus[0] = true;
if (topLeftMoveCheck(e) && isReleasedDragged()) {
move = true;
smoveX = e.getX();
smoveY = e.getY();
} else if (!move && isReleasedDragged()) {
controlPanel.setName("true");
right.setBackground(new Color(0, 0, 0, 100));
top.setBackground(new Color(0, 0, 0, 100));
left.setBackground(new Color(0, 0, 0, 100));
bottom.setBackground(new Color(0, 0, 0, 100));
bottom.setOpaque(true);
startX = e.getX();
startY = e.getY();
startXTemp = e.getX();
startYTemp = e.getY();
} else if (isbBox || isCircle || isBrush || isText) {
if (!topLeftMoveCheck(e)) return;
boxStartX = e.getX();
boxStartY = e.getY();
brush = new ArrayList<>();
if (isBrush) brush.add(e.getPoint());
if (isText) {
toptext.setLayout(gridBagLayout);
JTextPane jTextField = new JTextPane();
jTextField.setMinimumSize(new Dimension(100, 30));
int fontSize = Integer.parseInt(groupsize.getSelection().getActionCommand());
jTextField.setFont(new Font("微软雅黑", Font.BOLD, 20 + fontSize));
String color = groupcolor.getSelection().getActionCommand();
jTextField.setForeground(Color.decode(color));
jTextField.setMargin(new Insets(15, 5, 5, 5));
JPanel contenttext = new JPanel();
contenttext.add(jTextField);
contenttext.setOpaque(false);
jTextField.setOpaque(false);
jTextField.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weighty = 1;
gridBagConstraints.weightx = 1;
gridBagConstraints.anchor = GridBagConstraints.WEST;
int startY = boxStartY;
int startX = boxStartX;
gridBagConstraints.insets = new Insets(startY, startX, 0, 0);
gridBagLayout.setConstraints(contenttext, gridBagConstraints);
toptext.add(contenttext);
jTextField.requestFocus(true);
jTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (StringUtils.isNotBlank(jTextField.getText())) {
Map<String, Object> map = new HashMap<>();
map.put("str", jTextField.getText());
map.put("size", fontSize);
map.put("color", color);
map.put("boxStartY", startY + 30);
map.put("boxStartX", startX + 10);
textArr.add(map);
ctrlZList.add(map);
}
toptext.removeAll();
}
});
toptext.updateUI();
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (smoveX == e.getX() && smoveY == e.getY()) return;
if (startX == e.getX() && startY == e.getY()) return;
if (boxStartX == e.getX() && boxStartY == e.getY()) return;
startXTemp = startX;
startYTemp = startY;
endXTemp = endX;
endYTemp = endY;
move = false;
if (isbBox && paintbrush.equals("box")) {
JSONArray array = new JSONArray();
array.put(boxStartX);
array.put(boxStartY);
array.put(boxEndX);
array.put(boxEndY);
array.put(groupcolor.getSelection().getActionCommand());
array.put(groupsize.getSelection().getActionCommand());
boxArr.add(array);
ctrlZList.add(array);
} else if (isCircle && paintbrush.equals("circle")) {
JSONArray array = new JSONArray();
int circleSize = Integer.parseInt(groupsize.getSelection().getActionCommand());
for (int i = 0; i < circleSize; i++) {
if (boxStartX < boxEndX && boxStartY < boxEndY) {
array.put(boxStartX + i);
array.put(boxStartY + i);
array.put((boxEndX - i * 2) - boxStartX);
array.put((boxEndY - i * 2) - boxStartY);
} else if (boxStartX > boxEndX && boxStartY < boxEndY) {
array.put(boxEndX + i);
array.put(boxStartY + i);
array.put((boxStartX - i * 2) - boxEndX);
array.put((boxEndY - i * 2) - boxStartY);
} else if (boxEndY < boxStartY && boxStartX < boxEndX) {
array.put(boxStartX + i);
array.put(boxEndY + i);
array.put((boxEndX - i * 2) - boxStartX);
array.put((boxStartY - i * 2) - boxEndY);
} else if (boxStartY > boxEndY && boxStartX > boxEndX) {
array.put(boxEndX + i);
array.put(boxEndY + i);
array.put((boxStartX - i * 2) - boxEndX);
array.put((boxStartY - i * 2) - boxEndY);
}
}
if (array.length() != 0) {
array.put(groupcolor.getSelection().getActionCommand());
array.put(circleSize);
circleArr.add(array);
ctrlZList.add(array);
}
} else if (isBrush && paintbrush.equals("brush")) {
int size = Integer.parseInt(groupsize.getSelection().getActionCommand());
BrushLine brushLine = new BrushLine(groupcolor.getSelection().getActionCommand(), size, brush);
brushArr.add(brushLine);
ctrlZList.add(brushLine);
}
Arrays.fill(clickedMoveStatus, false);
}
});
frameSelection.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (mouseEvent == 3) return;// 右键不操作
clickedMoveStatus[1] = true;
if (!move && isReleasedDragged()) {
endX = e.getX();
endY = e.getY();
endXTemp = e.getX();
endYTemp = e.getY();
if (endY > startY) {
// 鼠标向上移动 超过初始点
topMove = false;
if (endX > startX) {
// 鼠标向左右移动 超过初始点
bottomRight(); // 向下移动 并向右
leftMove = false;
frameSelectionArea[0] = endX - startX;
frameSelectionArea[1] = endY - startY;
} else {
bottomLeft();
leftMove = true;
frameSelectionArea[0] = startX - endX;
frameSelectionArea[1] = endY - startY;
}
} else {
topMove = true;
if (endX > startX) {
// 鼠标向上并向左移动 超过初始点
topLeft();
leftMove = false;
frameSelectionArea[0] = endX - startX;
frameSelectionArea[1] = startY - endY;
} else {
topRight();
leftMove = true;
frameSelectionArea[0] = startX - endX;
frameSelectionArea[1] = startY - endY;
}
}
} else if (topLeftMoveCheck(e) && isReleasedDragged()) {
emoveX = e.getX();
emoveY = e.getY();
startX = startXTemp + (emoveX - smoveX);
if (startX < 0) startX = 0;
endX = endXTemp + (emoveX - smoveX);
if (endX >= width - 1) endX = width - 1;
startY = startYTemp + (emoveY - smoveY);
if (startY < 0) startY = 0;
endY = endYTemp + (emoveY - smoveY);
if (endY >= height - 40) endY = height - 40;
if (!topMove && !leftMove) bottomRight();
if (topMove && !leftMove) topLeft();
if (!topMove && leftMove) bottomLeft();
if (topMove && leftMove) topRight();
} else if (isbBox || isCircle || isBrush || isText) {
if (!topLeftMoveCheck(e)) return;
boxEndX = e.getX();
boxEndY = e.getY();
if (isBrush) brush.add(e.getPoint());
}
controlPanel.setVisible(true);
if (endX > startX) controlPanel.setBounds(endX - controlPanel.getWidth(), 0, controlPanel.getWidth(), controlPanel.getHeight());
else controlPanel.setBounds(startX - controlPanel.getWidth(), 0, controlPanel.getWidth(), controlPanel.getHeight());
frameSelection.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
if (isShowTip) {
showTipPoint = e.getPoint();
frameSelection.repaint();
}
}
});
ctrlZButton.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getModifiers() == KeyEvent.CTRL_MASK) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_Z) {
ctrlZClear();
}
}
}
});
toptext = new JPanel();
lefttext = new JPanel();
righttext = new JPanel();
buttomtext = new JPanel();
frameSelection.setLayout(new BorderLayout());
frameSelection.add(toptext, BorderLayout.NORTH);
frameSelection.add(lefttext, BorderLayout.WEST);
frameSelection.add(righttext, BorderLayout.EAST);
frameSelection.add(buttomtext, BorderLayout.SOUTH);
toptext.setOpaque(false);
lefttext.setOpaque(false);
righttext.setOpaque(false);
buttomtext.setOpaque(false);
return frameSelection;
}
// 上下左右检测
public boolean topLeftMoveCheck(MouseEvent e) {
if (e.getX() > startX && e.getX() < endX && e.getY() > startY && e.getY() < endY) return true;
else if (topMove && e.getX() > startX && e.getX() < endX && e.getY() < startY && e.getY() > endY) return true;
else if (leftMove && e.getX() < startX && e.getX() > endX && e.getY() > startY && e.getY() < endY) return true;
else if (e.getX() < startX && e.getX() > endX && e.getY() < startY && e.getY() > endY) return true;
return false;
}
// 向下移动 并向右
public void bottomRight() {
top.setBounds(0, 0, top.getWidth(), startY);
bottom.setBounds(0, endY, top.getWidth(), height - endY);
left.setBounds(0, startY, startX, endY - startY);
right.setBounds(endX, startY, width - endX, endY - startY);
}
// 向上移动 并向右
public void topRight() {
top.setBounds(0, 0, top.getWidth(), endY);
bottom.setBounds(0, startY, top.getWidth(), height - endY);
left.setBounds(0, endY, endX, startY - endY);
right.setBounds(startX, endY, width - endX, startY - endY);
}
// 向下移动 并向左
public void bottomLeft() {
top.setBounds(0, 0, top.getWidth(), startY);
bottom.setBounds(0, endY, top.getWidth(), height - endY);
left.setBounds(0, startY, endX, endY - startY);
right.setBounds(startX, startY, width - startX, endY - startY);
}
// 向上移动 并向左
public void topLeft() {
top.setBounds(0, 0, top.getWidth(), endY);
bottom.setBounds(0, startY, top.getWidth(), height - endY);
left.setBounds(0, endY, startX, startY - endY);
right.setBounds(endX, endY, width - endX, startY - endY);
}
// 保存的画笔数据写出
public void boxWrite(Graphics g) {
Integer lineSize;
for (JSONArray array : boxArr) {
int boxStartX = array.getInt(0);
int boxStartY = array.getInt(1);
int boxEndX = array.getInt(2);
int boxEndY = array.getInt(3);
Object color = array.get(4);
g.setColor(Color.decode(color + "")); //设置颜色
lineSize = array.getInt(5);
for (int i = 0; i < lineSize; i++) {
g.drawLine(boxStartX, boxStartY - i, boxEndX, boxStartY - i);
g.drawLine(boxStartX, boxEndY - i, boxEndX, boxEndY - i);
g.drawLine(boxStartX + i, boxStartY, boxStartX + i, boxEndY);
g.drawLine(boxEndX - i, boxStartY, boxEndX - i, boxEndY);
}
}
BasicStroke stokeLine = new BasicStroke(2.0f);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stokeLine);
for (JSONArray array : circleArr) {
int circleNum = array.getInt(array.length() - 1);
g.setColor(Color.decode(array.get(array.length() - 2) + "")); //设置颜色
for (int i = 0; i < circleNum; i++) {
int startX = array.getInt(i * 4 + 0);//0 4 8 12
int startY = array.getInt(i * 4 + 1);//1 5 9 13
int endX = array.getInt(i * 4 + 2); //2 6 10 14
int endY = array.getInt(i * 4 + 3); //3 7 11 15
g.drawOval(startX, startY, endX, endY);
}
}
for (BrushLine bline : brushArr) {
g.setColor(Color.decode(bline.color)); //设置颜色
BasicStroke brushLine = new BasicStroke(bline.size);
Graphics2D brushLineG2 = (Graphics2D) g;
brushLineG2.setStroke(brushLine);
ArrayList<Point> ap = bline.apoint;
for (int i = 0; i < bline.apoint.size(); i++) {
int endi = i + 1 < bline.apoint.size() ? i + 1 : i;
g.drawLine(ap.get(i).x, ap.get(i).y, ap.get(endi).x, ap.get(endi).y);
}
}
for (Map<String, Object> map : textArr) {
g.setColor(Color.decode(map.get("color") + ""));
int fontSize = 20 + Integer.parseInt(map.get("size") + "");
Font font = new Font("微软雅黑", Font.BOLD, fontSize);
g.setFont(font);
int boxStartY = Integer.parseInt(map.get("boxStartY") + "");
int boxStartX = Integer.parseInt(map.get("boxStartX") + "");
String[] strings = (map.get("str") + "").split("\r\n");
for (int i = 0; i < strings.length; i++) {
g.drawString(strings[i], boxStartX, boxStartY + (i * fontSize));
}
}
}
// 撤销清除画笔
public void ctrlZClear() {
if (ctrlZList.size() > 0) {
Object array = ctrlZList.get(ctrlZList.size() - 1);
boxArr.remove(array);
circleArr.remove(array);
brushArr.remove(array);
textArr.remove(array);
ctrlZList.remove(array);
}
boxStartX = 0;
boxStartY = 0;
boxEndX = 0;
boxEndY = 0;
if (brush != null) brush.clear();
isCtrlZ = true;
frameSelection.repaint();
}
// 保存图片到磁盘
public void doSaveBufImage(JFrame jFrame) {
try {
Rectangle rect = new Rectangle(startX + 5, startY + 5, frameSelectionArea[0] - 10, frameSelectionArea[1] - 10);
BufferedImage bufImage = new Robot().createScreenCapture(rect);// 截屏操作
JFileChooser jfc = new JFileChooser(".");
jfc.addChoosableFileFilter(new GIFfilter());
jfc.addChoosableFileFilter(new BMPfilter());
jfc.addChoosableFileFilter(new JPGfilter());
jfc.setFileFilter(new PNGfilter());
DateFormat dft = new SimpleDateFormat("yyyyMMddHHmmss");
String format = dft.format(new Date());
File file = new File("截图工具_" + format + ".png");
jfc.setSelectedFile(file);
AtomicReference<String> about = new AtomicReference<>("PNG");
jfc.addPropertyChangeListener(evt -> {
if (JFileChooser.FILE_FILTER_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
if (evt.getNewValue() instanceof JPGfilter) about.set("JPG");
if (evt.getNewValue() instanceof BMPfilter) about.set("BMP");
if (evt.getNewValue() instanceof GIFfilter) about.set("GIF");
if (evt.getNewValue() instanceof PNGfilter) about.set("PNG");
jfc.setSelectedFile(new File("截图工具_" + format + "." + about.get().toLowerCase()));
}
});
int ch = jfc.showDialog(jFrame, "保存文件");
if (ch == JFileChooser.APPROVE_OPTION) {
ImageIO.write(bufImage, about.get(), jfc.getSelectedFile());
jFrame.dispose();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 复制图片到内存
public void doCopyBufImage(JFrame jFrame) {
try {
Rectangle rect = new Rectangle(startX + 5, startY + 5, frameSelectionArea[0] - 10, frameSelectionArea[1] - 10);
BufferedImage bufImage = new Robot().createScreenCapture(rect);// 截屏操作
Transferable transferable = new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{
DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) return bufImage;
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
jFrame.dispose();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void createUIComponents() {
// TODO: place custom component creation code here
beijing = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
g.drawImage(bufImage, 0, 0, screenSize.width, screenSize.height, this);
}
};
}
public void addButtonGroup(){
groupsize = new ButtonGroup();
mindian.setIcon(gitImageIcon("/icons/capterScreen/圆点小1.png", 20, 20));
mindian.setSelectedIcon(gitImageIcon("/icons/capterScreen/圆点小.png", 20, 20));
mindian.setActionCommand("2");
groupsize.add(mindian);
defaultdian.setIcon(gitImageIcon("/icons/capterScreen/圆点中1.png", 20, 20));
defaultdian.setSelectedIcon(gitImageIcon("/icons/capterScreen/圆点中.png", 20, 20));
defaultdian.setActionCommand("4");
groupsize.add(defaultdian);
maxdian.setIcon(gitImageIcon("/icons/capterScreen/圆点大1.png", 20, 20));
maxdian.setSelectedIcon(gitImageIcon("/icons/capterScreen/圆点大.png", 20, 20));
maxdian.setActionCommand("8");
groupsize.add(maxdian);
groupcolor = new ButtonGroup();
ashkuang.setIcon(gitImageIcon("/icons/capterScreen/ash多选框.png", 20, 20));
ashkuang.setSelectedIcon(gitImageIcon("/icons/capterScreen/ash多选框-选中.png", 20, 20));
ashkuang.setActionCommand("#707070");
groupcolor.add(ashkuang);
bluekuang.setIcon(gitImageIcon("/icons/capterScreen/blue多选框.png", 20, 20));
bluekuang.setSelectedIcon(gitImageIcon("/icons/capterScreen/blue多选框-选中.png", 20, 20));
bluekuang.setActionCommand("#0000ff");
groupcolor.add(bluekuang);
greenkuang.setIcon(gitImageIcon("/icons/capterScreen/green多选框.png", 20, 20));
greenkuang.setSelectedIcon(gitImageIcon("/icons/capterScreen/green多选框-选中.png", 20, 20));
greenkuang.setActionCommand("#2AA515");
groupcolor.add(greenkuang);
redkuang.setIcon(gitImageIcon("/icons/capterScreen/red多选框.png", 20, 20));
redkuang.setSelectedIcon(gitImageIcon("/icons/capterScreen/red多选框-选中.png", 20, 20));
redkuang.setActionCommand("#ff0000");
groupcolor.add(redkuang);
whitekuang.setIcon(gitImageIcon("/icons/capterScreen/white多选框.png", 20, 20));
whitekuang.setSelectedIcon(gitImageIcon("/icons/capterScreen/white多选框-选中.png", 20, 20));
whitekuang.setActionCommand("#FFFFFF");
groupcolor.add(whitekuang);
yellowkuang.setIcon(gitImageIcon("/icons/capterScreen/yellow多选框.png", 20, 20));
yellowkuang.setSelectedIcon(gitImageIcon("/icons/capterScreen/yellow多选框-选中.png", 20, 20));
yellowkuang.setActionCommand("#EFB336");
groupcolor.add(yellowkuang);
}
/** * 获取图片 * @param fileUrl * @return */
public static ImageIcon gitImageIcon(String fileUrl,int width,int height){
try {
InputStream input= ViewMain.class.getClass().getResourceAsStream(fileUrl);
byte[] byt = new byte[input.available()];
input.read(byt);
input.close();
ImageIcon imageIcon= new ImageIcon(byt);
Image image= imageIcon.getImage();
Image smallImage = image.getScaledInstance(width,height,Image.SCALE_SMOOTH);
ImageIcon smallIcon = new ImageIcon(smallImage);
return smallIcon;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
MyScreencx screenTest = new MyScreencx(jFrame);
jFrame.getContentPane().add(screenTest.beijing, BorderLayout.CENTER);
jFrame.setUndecorated(true);
jFrame.setSize(width, height);
jFrame.setVisible(true);
}
}
4 结语
先从简单的屏幕截图做起,然后实现对键鼠的操作,最难的是图片比对,通过不断地学习和钻研都能实现的。
项目包地址:https://download.csdn.net/download/QinHuanJiDeWoMa/87244628