Need help with Mouse events

Hi,

Can anyone show me how I can pick a pixel from an image using mouse actions?

Let say I have an image, i, loaded into an image panel, ip. If I move the mouse over the image, I want the pixel (x,y) coordinate to be shown and when I click on this pixel I want to store the info probably in a list.

If anyone can help me to start with codes, I am really glad and thankful.

Thanks

hzinoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Use a BufferedImage and getRGB
0
CEHJCommented:
int pixel = bi.getRGB(e.getX(), e.getY());
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WebstormCommented:
Using javax.swing.JLabel (named img) to display you image,
You need a mouse listener and mouse motion listener :

public class MyApp
implements MouseListener,MouseMotionListener
{
   ...
    img.addMouseListener(this);
    img.addMouseMotionListener(this);
...
    public void mouseMoved(MouseEvent e) // e.getX()  e.getY();
    {  ... }
    public void mouseDragged(MouseEvent e) // e.getX()  e.getY();
    {  ... }
    public void mousePressed(MouseEvent e) // e.getX()  e.getY();
    {  ... }
    public void mouseReleased(MouseEvent e) // e.getX()  e.getY();
    {  ... }
    public void mouseClicked(MouseEvent e) // e.getX()  e.getY();
    {  ... }
    public void mouseEntered(MouseEvent e) // e.getX()  e.getY();
    {  ... }
    public void mouseExited(MouseEvent e) // e.getX()  e.getY();
    {  ... }
}
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

objectsCommented:
get the image pixels using (just do this once):

int pixels[] = new int[width*height];
PixelGrabber pg = new PixelGrabber(image, 0,0,width,height,pixels,0,width);
pg.grabPixels();

Then to get the pixel value in your MouseMotionListener use:

  int x = event.getX();
  int y = event.getY();
  int pixel = pixels[ x + y * imageWidth ] ;
0
CEHJCommented:
:-)
0
WebstormCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.