编程语言
首页 > 编程语言> > java 取色器 坐标值

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,show,jPanel,取色,add,new,坐标值,Font,JLabel
来源: https://blog.csdn.net/Erwinl/article/details/120366180