• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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

0
hzinox
Asked:
hzinox
  • 3
  • 2
4 Solutions
 
CEHJCommented:
Use a BufferedImage and getRGB
0
 
CEHJCommented:
int pixel = bi.getRGB(e.getX(), e.getY());
0
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now