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

源码 同时被 2 个专栏收录
6 篇文章 0 订阅
3 篇文章 0 订阅

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

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

Jy1DiD.gif

XY.java


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


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


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


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://gitee.com/lete114/Java.Tools

Github

Gitee

注意

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



  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页

打赏

Lete乐特

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值