Getting image pixel values

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);
        }
    };
}









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
Java program to get the color of pixel
for getting the pixel color of an image. To get the pixel color we need to first have an image and then we will be able to get the pixel color of any specific... Java program to get the color of pixel   
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
PHP GD image set pixel
('Content-Type: image/png');ADS_TO_REPLACE_5 imagepng($gd); ?> After
getting values from database - JSP-Servlet
getting values from database  I tried the following code abc.html aaa.jsp I am not getting exceptions now. But I am not getting output also. Plz help.   Hi Friend, Run your
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
passing the form values with image upload - JSP-Servlet
passing the form values with image upload  Hii . I want to get the solution for passing values with an image uploading form. I cant access the request values from the fields rather than image . I want to know how can cut
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
Getting Cell Values in a JTable
Getting Cell Values in a JTable     ... values in a JTable component. It is a same as setting the cell values in a JTable. If you want to set the cell values, use the setValueAt() method and want to get
pixel into xml - XML
pixel into xml  hiii can i store image pixels in a xml file and access easily... suggest the better way...  hi friend, 201 171 81 203 175 78 210 176 87 198
Getting text values from a NodeList
Getting text values from a NodeList       This Example shows you how to Get Text values from the NodeList in a DOM document. JAXP (Java API for XML Processing) is an interface
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
Getting ..Photograph.jpg not found - Swing AWT
Getting ..Photograph.jpg not found  hello sir thankyou very much for the code ... but sir here only other values are getting displayed...but not image for image it is comming Couldn't find file: Photograph.jpg .. why so?..I
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
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
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
Getting mysql table in textbox
Getting mysql table in textbox  how to get mysql table values into textbox in java using ajax and servlets
Getting Exception
Getting Exception  How to get exception from commented code in java
getting variables
getting variables   how to get variables from servlet program to webservice program
page is not getting loaded when refreshing???
page is not getting loaded when refreshing???  I am loading an image section(image,demo link,Zoom) in a webpage by using javascript function in JSP. It will get all details of image section from database. I am getting header
getting the emplo
getting the emplo   I have one table "EMPLOYEEMASTER" contains 'emp id', 'employee name' for eg: emp id employee name... kumar i want the sql statement for getting like
How to get Keys and Values from HashMap in Java?
How to get Keys and Values from HashMap in Java? Example program of iterating... to iterate the keys and get the values and print on the console. How to get keys and values from HashMap? This is the most asked questions in the Java
image comparison - Java Beginners
image comparison  hi all, pls help me to compare 2 images(in pixel and pattern) using jsp...the image pixels are in a xml file...  Hi friend, Read for more information. http://www.roseindia.net/jsp
Getting NumberFormatException
Getting NumberFormatException  Thanks for the reply. Its works well, but whenever I'm using numeric value from 0010 to 0757 it is returning some garbage value. I dont have the idea what is going wrong. Please give me some idea
Getting NumberFormatException
Getting NumberFormatException  Hello Sir, I'm using Integer.parseInt(String str) function which returns a NumberFormatException(Unknown source). I also used Integer.parseInt(String str, int radix) but it also throws the exception
getting errors
getting errors  public class positive { public static void main(String[] args) { retainPositiveNumbers(a); } private static int n; private static int j; static int a[]={-1,2,3,4,-5}; static int
Getting an error :(
Getting an error :(  I implemented the same code as above.. But getting this error in console... Console Oct 5, 2012 10:18:14 AM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server
getting voucherPin
getting voucherPin  I am doin an online credit topup application..and i need a method to fetch and decrypt the voucherpin from Database.. this is my voucher repository Class. public interface VoucherRepository extends

Ads