java 取色器 坐标值

使用java获取鼠标位置的坐标和颜色值,就是几个java类api的使用

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.util.Timer;
import java.util.TimerTask;
public class PointColor extends JFrame {
    private final JPanel jPanel = new JPanel();
    JLabel show_x = null;
    JLabel show_y = null;
    JLabel show_r = null;
    JLabel show_h = null;

    public static void main(String[] args) {
        try {
            PointColor pointColor = new PointColor();
            pointColor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pointColor.setVisible(true);
            pointColor.setAlwaysOnTop(true);
            Timer timer = new Timer();
            Robot r = new Robot();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    //坐标对象
                    Point point = MouseInfo.getPointerInfo().getLocation();
                    //坐标处的颜色对象
                    Color c = r.getPixelColor(point.x, point.y);
                    //显示坐标值
                    pointColor.show_x.setText("" + point.x);
                    pointColor.show_y.setText("" + point.y);
                    //显示RGB颜色值
                    pointColor.show_r.setText(c.getRed()+","+c.getGreen()+","+c.getBlue());
                    //显示16进制颜色
                    pointColor.show_h.setText(String.format("#%02X%02X%02X", c.getRed(), c.getGreen(),c.getBlue()));

                }
            }, 200, 100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PointColor() throws AWTException {
        setTitle("鼠标");
        setBounds(100, 100, 217, 180);
        getContentPane().setLayout(new BorderLayout());
        jPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(jPanel, BorderLayout.CENTER);
        jPanel.setLayout(null);

        JLabel showx = new JLabel("坐标x:");
        showx.setFont(new Font("宋体", Font.PLAIN, 15));
        showx.setBounds(22, 15, 66, 31);
        jPanel.add(showx);

        JLabel showy = new JLabel("坐标y:");
        showy.setFont(new Font("宋体", Font.PLAIN, 15));
        showy.setBounds(22, 45, 66, 31);
        jPanel.add(showy);

        JLabel showr = new JLabel("rgb值:");
        showr.setFont(new Font("宋体", Font.PLAIN, 15));
        showr.setBounds(22, 75, 66, 31);
        jPanel.add(showr);

        JLabel showh = new JLabel("Hex值:");
        showh.setFont(new Font("宋体", Font.PLAIN, 15));
        showh.setBounds(22, 105, 66, 31);
        jPanel.add(showh);

        show_x = new JLabel("0");
        show_x.setForeground(Color.BLUE);
        show_x.setFont(new Font("宋体", Font.PLAIN, 20));
        show_x.setBounds(82, 15, 66, 31);
        jPanel.add(show_x);

        show_y = new JLabel("0");
        show_y.setForeground(Color.BLUE);
        show_y.setFont(new Font("宋体", Font.PLAIN, 20));
        show_y.setBounds(82, 45, 66, 31);
        jPanel.add(show_y);

        show_r = new JLabel("0");
        show_r.setForeground(Color.BLUE);
        show_r.setFont(new Font("宋体", Font.PLAIN, 20));
        show_r.setBounds(82, 75, 120, 31);
        jPanel.add(show_r);

        show_h = new JLabel("0");
        show_h.setForeground(Color.BLUE);
        show_h.setFont(new Font("宋体", Font.PLAIN, 20));
        show_h.setBounds(82, 105, 120, 31);
        jPanel.add(show_h);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值