Java获取鼠标当前颜色RGB、获取鼠标当前HEX值、获取鼠标当前坐标(源码)

Lete 乐特自制实用工具(Java EE - JDK1.8)

现在自己也是一名程序员了,想着以前用的都是别人打包好的exe文件,于是就动手自己写个吧?把 RGB、HEX 值、坐标、合并到一起自己用的也方便。

XY.java

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

package io.github.lete114.tools;

import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 04-09 11:31
*/
public class XY extends JFrame {
private final JPanel contentPanel = new JPanel();

// 创建窗体内容
JLabel vx = null;
JLabel vy = null;
JLabel r = null;
JLabel g = null;
JLabel b = null;
JLabel h = null;

public XY() {

getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);

// logo图片
setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getResource("lete.jpg")));


// 坐标X
JLabel lblx = new JLabel("\u5750\u6807X:");
lblx.setFont(new Font("黑体", Font.PLAIN, 30));
lblx.setBounds(70, 30, 100, 30);
contentPanel.add(lblx);
// 坐标Y
JLabel lbly = new JLabel("\u5750\u6807Y:");
lbly.setFont(new Font("黑体", Font.PLAIN, 30));
lbly.setBounds(270, 30, 100, 30);
contentPanel.add(lbly);

// X=
vx = new JLabel("0");
vx.setForeground(Color.BLUE);
vx.setFont(new Font("黑体", Font.PLAIN, 30));
vx.setBounds(160, 30, 66, 31);
contentPanel.add(vx);

// Y=
vy = new JLabel("0");
vy.setForeground(Color.BLUE);
vy.setFont(new Font("黑体", Font.PLAIN, 30));
vy.setBounds(360, 30, 66, 30);
contentPanel.add(vy);

// R=
r = new JLabel("0");
r.setForeground(Color.BLUE);
r.setFont(new Font("黑体", Font.PLAIN, 30));
r.setBounds(120, 80, 66, 31);
contentPanel.add(r);
//红字
JLabel hong = new JLabel("红:");
hong.setFont(new Font("黑体", Font.PLAIN, 30));
hong.setForeground(Color.RED);
hong.setBounds(70, 80, 100, 30);
contentPanel.add(hong);

// G=
g = new JLabel("0");
g.setForeground(Color.BLUE);
g.setFont(new Font("黑体", Font.PLAIN, 30));
g.setBounds(120, 130, 66, 31);
contentPanel.add(g);
// 绿字
JLabel lv = new JLabel("绿:");
lv.setFont(new Font("黑体", Font.PLAIN, 30));
lv.setForeground(Color.green);
lv.setBounds(70, 130, 100, 30);
contentPanel.add(lv);

// B=
b = new JLabel("0");
b.setForeground(Color.BLUE);
b.setFont(new Font("黑体", Font.PLAIN, 30));
b.setBounds(120, 170, 66, 30);
contentPanel.add(b);
// 蓝字
JLabel lan = new JLabel("蓝:");
lan.setFont(new Font("黑体", Font.PLAIN, 30));
lan.setForeground(Color.blue);
lan.setBounds(70, 170, 100, 30);
contentPanel.add(lan);

// HEX 值
h = new JLabel("");
h.setForeground(Color.BLUE);
h.setFont(new Font("黑体", Font.PLAIN, 30));
h.setBounds(290, 130, 500, 30);
contentPanel.add(h);
// HEX 字
JLabel hexz = new JLabel("HEX值:");
hexz.setFont(new Font("黑体", Font.PLAIN, 30));
hexz.setBounds(200, 130, 100, 30);
contentPanel.add(hexz);

// 声明
JLabel ms1 = new JLabel("此工具纯属个人兴趣爱好,本人常用到该功能");
JLabel ms2 = new JLabel(" 于是就写了这个工具");
JLabel ms3 = new JLabel(" 请不要用于商业用途");
JLabel ms4 = new JLabel(" 一切后果自己负责");
JLabel ms5 = new JLabel(" --乐特");
ms1.setFont(new Font("黑体", Font.PLAIN, 20));
ms2.setFont(new Font("黑体", Font.PLAIN, 20));
ms3.setFont(new Font("黑体", Font.PLAIN, 20));
ms4.setFont(new Font("黑体", Font.PLAIN, 20));
ms5.setFont(new Font("黑体", Font.PLAIN, 25));
ms1.setForeground(Color.RED);
ms2.setForeground(Color.RED);
ms3.setForeground(Color.RED);
ms4.setForeground(Color.RED);
ms5.setForeground(Color.black);
ms1.setBounds(50, 240, 3000, 30);
ms2.setBounds(50, 260, 3000, 30);
ms3.setBounds(50, 280, 3000, 30);
ms4.setBounds(50, 300, 3000, 30);
ms5.setBounds(50, 320, 3000, 30);
contentPanel.add(ms1);
contentPanel.add(ms2);
contentPanel.add(ms3);
contentPanel.add(ms4);
contentPanel.add(ms5);
}
}


RGB.java

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

package io.github.lete114.tools;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 04-09 16:45
*/
public class RGB {
// 获取鼠标当前位置的颜色
public int red(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
//System.out.println(di.width);
//System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
Color color = new Color(16777216 + pixelColor);
return color.getRed(); // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
}
public int green(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
Color color = new Color(16777216 + pixelColor);
return color.getGreen(); // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
}
public int blue(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
Color color = new Color(16777216 + pixelColor);
return color.getBlue(); // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
}
}


HEX.java

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

package io.github.lete114.tools;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 04-09 20:39
*/
public class HEX {
public String convertRGBToHex(int r, int g, int b) {
String rFString, rSString, gFString, gSString, bFString, bSString, result;
int red, green, blue;
int rred, rgreen, rblue;
red = r / 16;
rred = r % 16;
if (red == 10) rFString = "A";
else if (red == 11) rFString = "B";
else if (red == 12) rFString = "C";
else if (red == 13) rFString = "D";
else if (red == 14) rFString = "E";
else if (red == 15) rFString = "F";
else rFString = String.valueOf(red);

if (rred == 10) rSString = "A";
else if (rred == 11) rSString = "B";
else if (rred == 12) rSString = "C";
else if (rred == 13) rSString = "D";
else if (rred == 14) rSString = "E";
else if (rred == 15) rSString = "F";
else rSString = String.valueOf(rred);

rFString = rFString + rSString;

green = g / 16;
rgreen = g % 16;

if (green == 10) gFString = "A";
else if (green == 11) gFString = "B";
else if (green == 12) gFString = "C";
else if (green == 13) gFString = "D";
else if (green == 14) gFString = "E";
else if (green == 15) gFString = "F";
else gFString = String.valueOf(green);

if (rgreen == 10) gSString = "A";
else if (rgreen == 11) gSString = "B";
else if (rgreen == 12) gSString = "C";
else if (rgreen == 13) gSString = "D";
else if (rgreen == 14) gSString = "E";
else if (rgreen == 15) gSString = "F";
else gSString = String.valueOf(rgreen);

gFString = gFString + gSString;

blue = b / 16;
rblue = b % 16;

if (blue == 10) bFString = "A";
else if (blue == 11) bFString = "B";
else if (blue == 12) bFString = "C";
else if (blue == 13) bFString = "D";
else if (blue == 14) bFString = "E";
else if (blue == 15) bFString = "F";
else bFString = String.valueOf(blue);

if (rblue == 10) bSString = "A";
else if (rblue == 11) bSString = "B";
else if (rblue == 12) bSString = "C";
else if (rblue == 13) bSString = "D";
else if (rblue == 14) bSString = "E";
else if (rblue == 15) bSString = "F";
else bSString = String.valueOf(rblue);
bFString = bFString + bSString;
result = "#" + rFString + gFString + bFString;
return result;

}
}


Main.java

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

package io.github.lete114.tools;

import javax.swing.JFrame;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 04-25 17:47
*/
public class Main {
public static void main(String[] args) {
try {
XY lete = new XY();
RGB rgb = new RGB();
HEX hex = new HEX();
// 禁止放大和拉伸
lete.setResizable(false);
// 关闭时连同进程一起关闭,节约资源
lete.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 显示窗体
lete.setVisible(true);
// 显示在最顶层
lete.setAlwaysOnTop(true);
// 标题
lete.setTitle("Lete自制实用工具");
// 窗体出现的位置
lete.setBounds(1000, 10, 520, 400);
// 计时器
Timer timer = new Timer();
timer.schedule(new TimerTask() {
// 多线程
@Override
public void run() {
Point zbiao = MouseInfo.getPointerInfo().getLocation();

lete.vx.setText("" + zbiao.x);
lete.vy.setText("" + zbiao.y);

try {
lete.r.setText(""+rgb.red(zbiao.x,zbiao.y));
lete.g.setText(""+rgb.green(zbiao.x,zbiao.y));
lete.b.setText(""+rgb.blue(zbiao.x,zbiao.y));

lete.h.setText(""+hex.convertRGBToHex(rgb.red(zbiao.x,zbiao.y),rgb.green(zbiao.x,zbiao.y),rgb.blue(zbiao.x,zbiao.y)));
} catch (AWTException e) {
e.printStackTrace();
}
}
}, 10, 10);//0.01毫秒执行一次
} catch (Exception e) {
e.printStackTrace();
}
}
}


还有一张(图标)
lete.jpg
JyJ301.jpg


下载地址


这里顺便推荐个云盘曲奇云盘https://quqi.com用了有几个月了还不错

同样和百度云一样送 2T 空间,晚了可能就没有了,关键是不限速

这里我打包好了exe 文件

曲奇云盘:https://quqi.gblhgk.com/s/47889/5GK78MXIEVsOhilS

Github:https://github.com/lete114/Java.Tools

Gitee:https://github.com/lete114/Java.Tools

注意

由于 java 程序需要JVM(java 虚拟机)而 JVM 是在 jre 下的,所以软件大小很大,这里我已经对 JRE 进行精简压缩到了30MB(原 JRE 打包后大小为:200MB)



【Lete 乐特个人博客】:https://lete114.now.sh

Authorship: Lete乐特
Article Link: https://blog.imlete.cn/article/986f7d9c.html
Copyright: All posts on this blog are licensed under the CC BY-NC-SA 4.0 license unless otherwise stated. Please cite Lete乐特 's Blog !