Getting image pixel values

Ads
 

Getting image pixel values

how to get image pixels values on mouse click

View Answers

March 22, 2012 at 12:54 PM

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;

public class GetPixels extends JPanel {
    BufferedImage image;
    JLabel[] labels;

    public GetPixels(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;
        if(rect.contains(p)) {
            x = p.x - rect.x;
            y = p.y - rect.y;
           }
        setLabels(x, y);
    }
     private void setLabels(int x, int y){
        labels[0].setText(String.valueOf(x));
        labels[1].setText(String.valueOf(y));
       }

    private JPanel getLabels() {
        labels = new JLabel[2];
        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("", labels[0], panel, gbc, d, 1.0, 0  );
        addComponents("", labels[1], 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("c:/rose1.jpg");
        BufferedImage image = javax.imageio.ImageIO.read(file);
        GetPixels test = new GetPixels(image);
        JFrame f = new JFrame("GetPixels");
        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);
        }
    };
}

Ads









Related Tutorials/Questions & Answers:
Getting image pixel values
Getting image pixel values  how to get image pixels values on mouse... GetPixels extends JPanel { BufferedImage image; JLabel[] labels; public GetPixels(BufferedImage image) { this.image = image
Storing the Color Image pixel values into 2d array
Storing the Color Image pixel values into 2d array  i want to store the color image pixel values into 2d array and agiain the array have to store into a text file using java... plz provide the code
Advertisements
getting coords from an image.
getting coords from an image.  how to get coordinates dynamically(in run time) from an image?? pls help me
getting values from dropdown list
getting values from dropdown list  I am having a dropdown list which has hardcoded values ( we need it hardcoded only and will not be populated from... to the action. My action is getting called however, i am not sure how to pass
i am getting multiple values in listbox
i am getting multiple values in listbox   hi sir, i am using msaccess 2007.my query is look like this. select distinct a.name,b.fee from... getting its working fine, but in main query also i am getting multiple name
getting dropdown values using apache commons in servlet
getting dropdown values using apache commons in servlet  i want to get dropdown values in my program in order to merge the string to my file path so...); } i tried to debug and i am getting the wright file path but while proceeding
getting error while inserting values in database
getting error while inserting values in database  AddUser.java... into LTCINFO.PERSONS values ('"+LASTNAME+"','"+FIRSTNAME+"','"+ADDRESS+"','"+CITY... followin error i don't know why "ORA-00947: not enough values". kindly chk
Get the color values as a matrix of color image
Get the color values as a matrix of color image  I am new to java and image processing, i want to get the color values as a matrix from the color image for giving input to the compression algorithm by using java. plz provide
Image_creation
Image_creation  how to create an image from a set of pixel values stored in array - in java
getting int values from form and insert it in data base in jsp
getting int values from form and insert it in data base in jsp  how can i get form input such as id convert it to int insert it into database  ...=st.executeUpdate("insert into data(name,age, address) values('"+name+"',"+age
which values we have to use for compressing any color image
which values we have to use for compressing any color image  Sir........... in the process of compressing of any color image, which values do use as input for comprseeing algorithm. and how to get the image matrix from color
ModuleNotFoundError: No module named 'pixel'
ModuleNotFoundError: No module named 'pixel'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'pixel' How to remove the ModuleNotFoundError: No module named 'pixel'
Getting and Setting Java Type Values in a Preference
Getting and Setting Java Type Values in a Preference... and get the Java Type values in a Preference.  As you know that a preference node holds only string values. Therefore the Preferences class provides some
ModuleNotFoundError: No module named 'Magic-Pixel'
ModuleNotFoundError: No module named 'Magic-Pixel'  Hi, My Python... 'Magic-Pixel' How to remove the ModuleNotFoundError: No module named 'Magic-Pixel' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pixel_panel'
ModuleNotFoundError: No module named 'pixel_panel'  Hi, My Python... 'pixel_panel' How to remove the ModuleNotFoundError: No module named 'pixel_panel' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pixel-adv'
ModuleNotFoundError: No module named 'pixel-adv'  Hi, My Python... 'pixel-adv' How to remove the ModuleNotFoundError: No module named 'pixel... have to install padas library. You can install pixel-adv python with following
ModuleNotFoundError: No module named 'pixel-clusterizer'
ModuleNotFoundError: No module named 'pixel-clusterizer'  Hi, My... named 'pixel-clusterizer' How to remove the ModuleNotFoundError: No module named 'pixel-clusterizer' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pixel_panel'
ModuleNotFoundError: No module named 'pixel_panel'  Hi, My Python... 'pixel_panel' How to remove the ModuleNotFoundError: No module named 'pixel_panel' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pixel-ring'
ModuleNotFoundError: No module named 'pixel-ring'  Hi, My Python... 'pixel-ring' How to remove the ModuleNotFoundError: No module named 'pixel-ring' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'pixel-world'
ModuleNotFoundError: No module named 'pixel-world'  Hi, My Python... 'pixel-world' How to remove the ModuleNotFoundError: No module named 'pixel-world' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pixel-zero'
ModuleNotFoundError: No module named 'pixel-zero'  Hi, My Python... 'pixel-zero' How to remove the ModuleNotFoundError: No module named 'pixel-zero' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'django-facebook-pixel-code'
ModuleNotFoundError: No module named 'django-facebook-pixel-code'  Hi...: No module named 'django-facebook-pixel-code' How to remove the ModuleNotFoundError: No module named 'django-facebook-pixel-code' error? Thanks
ModuleNotFoundError: No module named 'gdal_pixel_functions'
ModuleNotFoundError: No module named 'gdal_pixel_functions'  Hi...: No module named 'gdal_pixel_functions' How to remove the ModuleNotFoundError: No module named 'gdal_pixel_functions' error? Thanks   Hi
ModuleNotFoundError: No module named 'python-lorem-pixel'
ModuleNotFoundError: No module named 'python-lorem-pixel'  Hi, My... named 'python-lorem-pixel' How to remove the ModuleNotFoundError: No module named 'python-lorem-pixel' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-lorem-pixel'
ModuleNotFoundError: No module named 'python-lorem-pixel'  Hi, My... named 'python-lorem-pixel' How to remove the ModuleNotFoundError: No module named 'python-lorem-pixel' error? Thanks   Hi, In your
Getting mysql table in textbox
Getting mysql table in textbox  how to get mysql table values into textbox in java using ajax and servlets
image
image  how to add the image in servlet code
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
image
image   Dear every body please help me how to add and retrive image and video into oracle 11g using jsp

Ads