Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Getting image pixel values
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


veeranna b
Getting image pixel values
1 Answer(s)      2 years and 9 months ago
Posted in : Java Beginners


how to get image pixels values on mouse click


Advertisement
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
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... we can get the pixel color with the getRGB() method with the image object. Here
PHP GD image set pixel
']) / 2; } header('Content-Type: image/png'); imagepng($gd); ?> After
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
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
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
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
Image_creation
Image_creation  how to create an image from a set of pixel values stored in array - in java
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
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
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
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 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
how to convert all the pixel value to hexadecimal and from hexadecimal to binary in vb.net
that i need to convert all the pixel values into hexadecimal how it can be done...how to convert all the pixel value to hexadecimal and from hexadecimal to binary in vb.net   actually i need in vb.net that category
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
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
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
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
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 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 emp id
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
Advertisements
 

 

 

DMCA.com