Results 1 to 2 of 2
  1. #1
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default How to show pixel Coordinate and RGB value when mouse is tracking over an image

    How to show pixel Coordinate and RGB value when mouse is tracking over an image which is displayed on Jpanel with scroll bar or or any kind of contain ter (not in applet)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class ReadingPixels extends JPanel {
        BufferedImage image;
        JLabel[] labels;
    
        public ReadingPixels(BufferedImage image) {
            this.image = image;
            addMouseMotionListener(mml);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = (getWidth() - image.getWidth())/2;
            int y = (getHeight() - image.getHeight())/2;
            g.drawImage(image, x, y, this);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(image.getWidth(), image.getHeight());
        }
    
        private void showPixel(Point p) {
            int w = getWidth();
            int h = getHeight();
            int iw = image.getWidth();
            int ih = image.getHeight();
            Rectangle rect = new Rectangle(iw, ih);
            rect.x = (w - iw)/2;
            rect.y = (h - ih)/2;
            int x = 0, y = 0, a = 0, r = 0, g = 0, b = 0;
            if(rect.contains(p)) {
                x = p.x - rect.x;
                y = p.y - rect.y;
                int pixel = image.getRGB(x, y);
                a = (pixel >> 24) & 0xff;
                r = (pixel >> 16) & 0xff;
                g = (pixel >>  8) & 0xff;
                b = (pixel >>  0) & 0xff;
            }
            setLabels(x, y, a, r, g, b);
        }
    
        private void setLabels(int x, int y, int a, int r, int g, int b) {
            labels[0].setText(String.valueOf(x));
            labels[1].setText(String.valueOf(y));
            labels[2].setText(String.valueOf(a));
            labels[3].setText(String.valueOf(r));
            labels[4].setText(String.valueOf(g));
            labels[5].setText(String.valueOf(b));
        }
    
        private JPanel getLabels() {
            labels = new JLabel[6];
            for(int i = 0; i < labels.length; i++) {
                labels[i] = new JLabel();
            }
            Dimension d = new Dimension(35, 25);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            addComponents("x =", labels[0], panel, gbc, d, 1.0, 0  );
            addComponents("y =", labels[1], panel, gbc, d, 0,   1.0);
            addComponents("a =", labels[2], panel, gbc, d, 1.0, 0  );
            addComponents("r =", labels[3], panel, gbc, d, 0,   0  );
            addComponents("g =", labels[4], panel, gbc, d, 0,   0  );
            addComponents("b =", labels[5], panel, gbc, d, 0,   1.0);
            return panel;
        }
    
        private void addComponents(String s, JComponent jc, Container c,
                                   GridBagConstraints gbc, Dimension d,
                                   double weightx1, double weightx2) {
            gbc.weightx = weightx1;
            gbc.anchor = GridBagConstraints.EAST;
            c.add(new JLabel(s), gbc);
            jc.setPreferredSize(d);
            gbc.weightx = weightx2;
            gbc.anchor = GridBagConstraints.WEST;
            c.add(jc, gbc);
        }
    
        public static void main(String[] args) throws IOException {
            File file = new File("images/blackBear.jpg");
            BufferedImage image = javax.imageio.ImageIO.read(file);
            ReadingPixels test = new ReadingPixels(image);
            JFrame f = new JFrame("ReadingPixels");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(test));
            f.add(test.getLabels(), "Last");
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private MouseMotionListener mml = new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                showPixel(p);
            }
        };
    }

Similar Threads

  1. Scanning Image Pixel by Pixel
    By the_transltr in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2012, 04:01 PM
  2. set coordinate to an image
    By nuur in forum AWT / Swing
    Replies: 2
    Last Post: 04-01-2008, 08:08 AM
  3. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM
  4. Download Tracking
    By ganeshsalian in forum Advanced Java
    Replies: 7
    Last Post: 07-30-2007, 04:57 PM
  5. Replies: 1
    Last Post: 07-17-2007, 04:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •