Search code examples
javaeventslocationmouseeventpixel

JAVA Saving Multiple X and Y Mouse Locations


Comparing Two Specific Pixels

I am trying to compare two specific pixel values by storing them both and eventually comparing the difference between them.

The code below shows what I currently have, (image_Display being my component) and then when the user clicks a location on the image the pixel location is stored. However, how do I do this if I want to collect numerous pixel values?

image_Display.addMouseListener(new MouseListener() {
     @Override
        public void mouseClicked(MouseEvent e) {
            x=e.getX();
            y=e.getY();
            System.out.println(x + "," + y);
        }      

Desired output:

Saving the location of 2 separate pixels. I'm sorry this is really obvious - I am new to event's.

How am I able to save the location of a location rather than overwriting it each time?

Thank you


Solution

  • If you need only previous coordinates then why don't keep them like this:

    int previusX,previousY;
    
    image_Display.addMouseListener(new MouseListener() {
         @Override
            public void mouseClicked(MouseEvent e) {
                previousX=x;
                previousY=y;
                x=e.getX();
                y=e.getY();
                System.out.println(x + "," + y+" ["+previousX+","+previousY+"]");
            }   
    }
    

    If you need more - use Collections and store few of MouseEvnets (as much as you need).