Java 从鼠标X和Y位置的缓冲区图像获取像素RGB

Java 从鼠标X和Y位置的缓冲区图像获取像素RGB,java,bufferedimage,pixels,jcolorchooser,Java,Bufferedimage,Pixels,Jcolorchooser,我正在制作一个带有图像的颜色选择器程序。程序首先加载图像,然后当您将鼠标悬停在图像上时,它将从鼠标的X和Y位置获取当前像素的RGB值。我已经设置了帧并加载了它们的图像,有人能帮我处理像素吗 package net.ogpc.settings; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import javax.imageio

我正在制作一个带有图像的颜色选择器程序。程序首先加载图像,然后当您将鼠标悬停在图像上时,它将从鼠标的X和Y位置获取当前像素的RGB值。我已经设置了帧并加载了它们的图像,有人能帮我处理像素吗

package net.ogpc.settings;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;

public class ColorChooser implements Runnable, MouseListener{
    public static String path = "FileIO Plug-Ins\\Resources\\color-picker.png";
    public static boolean running = false;
    public static String r = "100";
    public static String g = "100";
    public static String b = "100";
    JFrame frame = new JFrame("Choose A Color");
    JTextField JR = new JTextField();
    JTextField JG = new JTextField();
    JTextField JB = new JTextField();
    Container colorImage = new Container();
    Container east = new Container();
    public ColorChooser() {
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        frame.setResizable(false);
        //set up JFrame
        east.setLayout(new GridLayout(3, 3));
        east.add(JR);
        east.add(JG);
        east.add(JB);
        frame.add(east);
        //import the color chooser image
        Import();
        frame.setVisible(true);
        running = true;
        run();
    }
    public void run() {
        while (running) {
            getPixel();
            try {
                    Thread.sleep(250);
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    public void getPixel() {

        //get it m9!
        JR.setText(r);
        JG.setText(g);
        JB.setText(b);      
        System.out.println("r: " + r + " g: " + g + " b: " + b);
    }
    public void Import() {
        colorImage.setLayout(new FlowLayout());
        try {
            File file = new File(path);
            BufferedImage image;
            image = ImageIO.read(file);
            JLabel label = new JLabel(new ImageIcon(image));
            label.addMouseListener(this);
            colorImage.add(label, BorderLayout.NORTH);
            frame.getContentPane().add(colorImage, BorderLayout.WEST);
        } catch (IOException ex) {
                ex.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new ColorChooser();
    }
    public void mouseClicked(MouseEvent arg0) {
    }
    public void mouseEntered(MouseEvent arg0) {
    }
    public void mouseExited(MouseEvent arg0) {
    }
    public void mousePressed(MouseEvent arg0) {
        //each time you click on the image... print out the pixel RGB here

    }
    public void mouseReleased(MouseEvent arg0) {
    }
}

您的
虽然循环让我感到害怕,但它表明您可能不了解Swing或事件驱动环境是如何工作的。简而言之,你不需要它,将来也不应该使用这种技术

基本上,您需要访问图像的
BufferedImage
实例,因为这提供了对底层光栅数据的访问

如果小心布局,可以使用
JLabel
显示图像,只需向图像中添加
MouseMotionListener
,并监视鼠标移动,拾取其下方像素的压缩
int
颜色

然后需要将此压缩的
int
转换为RGB值。现在您可以进行一些位操作,但老实说,使用
Color(int)
更简单

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class ColorPicky {

    public static void main(String[] args) {
        new ColorPicky();
    }

    public ColorPicky() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private JLabel label;

        private JPanel fields;
        private JTextField red;
        private JTextField green;
        private JTextField blue;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            label = new JLabel();
            try {
                img = ImageIO.read(new File("C:\\hold\\thumbnails\\issue459.jpg"));
                label.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            add(label, gbc);

            fields = new JPanel();
            fields.setBorder(new EmptyBorder(5, 5, 5, 5));
            red = new JTextField(3);
            green = new JTextField(3);
            blue = new JTextField(3);
            fields.add(red);
            fields.add(green);
            fields.add(blue);
            add(fields, gbc);

            label.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    int packedInt = img.getRGB(e.getX(), e.getY());
                    Color color = new Color(packedInt, true);
                    fields.setBackground(color);
                    red.setText(Integer.toString(color.getRed()));
                    green.setText(Integer.toString(color.getGreen()));
                    blue.setText(Integer.toString(color.getBlue()));
                }
            });

        }

    }

}

我想你可能想花点时间通读


您的
虽然循环让我感到害怕,但它表示您可能不了解Swing或事件驱动环境是如何工作的。简而言之,你不需要它,将来也不应该使用这种技术

基本上,您需要访问图像的
BufferedImage
实例,因为这提供了对底层光栅数据的访问

如果小心布局,可以使用
JLabel
显示图像,只需向图像中添加
MouseMotionListener
,并监视鼠标移动,拾取其下方像素的压缩
int
颜色

然后需要将此压缩的
int
转换为RGB值。现在您可以进行一些位操作,但老实说,使用
Color(int)
更简单

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class ColorPicky {

    public static void main(String[] args) {
        new ColorPicky();
    }

    public ColorPicky() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private JLabel label;

        private JPanel fields;
        private JTextField red;
        private JTextField green;
        private JTextField blue;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            label = new JLabel();
            try {
                img = ImageIO.read(new File("C:\\hold\\thumbnails\\issue459.jpg"));
                label.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            add(label, gbc);

            fields = new JPanel();
            fields.setBorder(new EmptyBorder(5, 5, 5, 5));
            red = new JTextField(3);
            green = new JTextField(3);
            blue = new JTextField(3);
            fields.add(red);
            fields.add(green);
            fields.add(blue);
            add(fields, gbc);

            label.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    int packedInt = img.getRGB(e.getX(), e.getY());
                    Color color = new Color(packedInt, true);
                    fields.setBackground(color);
                    red.setText(Integer.toString(color.getRed()));
                    green.setText(Integer.toString(color.getGreen()));
                    blue.setText(Integer.toString(color.getBlue()));
                }
            });

        }

    }

}

我想你可能想花点时间通读


您的
虽然循环让我感到害怕,但它表示您可能不了解Swing或事件驱动环境是如何工作的。简而言之,你不需要它,将来也不应该使用这种技术

基本上,您需要访问图像的
BufferedImage
实例,因为这提供了对底层光栅数据的访问

如果小心布局,可以使用
JLabel
显示图像,只需向图像中添加
MouseMotionListener
,并监视鼠标移动,拾取其下方像素的压缩
int
颜色

然后需要将此压缩的
int
转换为RGB值。现在您可以进行一些位操作,但老实说,使用
Color(int)
更简单

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class ColorPicky {

    public static void main(String[] args) {
        new ColorPicky();
    }

    public ColorPicky() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private JLabel label;

        private JPanel fields;
        private JTextField red;
        private JTextField green;
        private JTextField blue;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            label = new JLabel();
            try {
                img = ImageIO.read(new File("C:\\hold\\thumbnails\\issue459.jpg"));
                label.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            add(label, gbc);

            fields = new JPanel();
            fields.setBorder(new EmptyBorder(5, 5, 5, 5));
            red = new JTextField(3);
            green = new JTextField(3);
            blue = new JTextField(3);
            fields.add(red);
            fields.add(green);
            fields.add(blue);
            add(fields, gbc);

            label.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    int packedInt = img.getRGB(e.getX(), e.getY());
                    Color color = new Color(packedInt, true);
                    fields.setBackground(color);
                    red.setText(Integer.toString(color.getRed()));
                    green.setText(Integer.toString(color.getGreen()));
                    blue.setText(Integer.toString(color.getBlue()));
                }
            });

        }

    }

}

我想你可能想花点时间通读


您的
虽然循环让我感到害怕,但它表示您可能不了解Swing或事件驱动环境是如何工作的。简而言之,你不需要它,将来也不应该使用这种技术

基本上,您需要访问图像的
BufferedImage
实例,因为这提供了对底层光栅数据的访问

如果小心布局,可以使用
JLabel
显示图像,只需向图像中添加
MouseMotionListener
,并监视鼠标移动,拾取其下方像素的压缩
int
颜色

然后需要将此压缩的
int
转换为RGB值。现在您可以进行一些位操作,但老实说,使用
Color(int)
更简单

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class ColorPicky {

    public static void main(String[] args) {
        new ColorPicky();
    }

    public ColorPicky() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private JLabel label;

        private JPanel fields;
        private JTextField red;
        private JTextField green;
        private JTextField blue;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            label = new JLabel();
            try {
                img = ImageIO.read(new File("C:\\hold\\thumbnails\\issue459.jpg"));
                label.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            add(label, gbc);

            fields = new JPanel();
            fields.setBorder(new EmptyBorder(5, 5, 5, 5));
            red = new JTextField(3);
            green = new JTextField(3);
            blue = new JTextField(3);
            fields.add(red);
            fields.add(green);
            fields.add(blue);
            add(fields, gbc);

            label.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    int packedInt = img.getRGB(e.getX(), e.getY());
                    Color color = new Color(packedInt, true);
                    fields.setBackground(color);
                    red.setText(Integer.toString(color.getRed()));
                    green.setText(Integer.toString(color.getGreen()));
                    blue.setText(Integer.toString(color.getBlue()));
                }
            });

        }

    }

}

我想你可能想花点时间通读


你的
while循环让我害怕。请确保您了解您是如何以某种方式查看此内容的。您的
,而循环
会吓到我。请确保您了解您是如何以某种方式查看此内容的。您的
,而循环
会吓到我。请确保您了解您是如何以某种方式查看此内容的。您的
,而循环
会吓到我。确保你明白你是如何看待这些想法的