java 取色器 坐标值

2021/9/18 14:34:44

本文主要是介绍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);
    }
}


这篇关于java 取色器 坐标值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程