Function to catch mouse drag coordinates

Asked

Viewed 154 times

-4

I am developing a java browser and would like to know how to create a function that stores the coordinates of the initial and final mouse using the mousePressed and mouseReleased. Grateful !

  • 3

    What have you done? What have you tried? What is the difficulty?

1 answer

0

a good mareira would be you create a class where you will have everything you want to save , type Point, Color, format , etc . type:

public class MyPoints
{
    Point pointPressed, pointReleased;

    // faz o constructor(s)
    MyPoints(Point pointPressed,Point pointReleased)
    {
        this.pointPressed = pointPressed;
        this.pointReleased = pointReleased;
    }
    // faca seus getter(s) 
}

ai you create an Arraylist of this class ,

ArrayList<MyPoints> ar = new ArrayList<>();

then you can create a Mouseadapter and override the

public void mousePressed(MouseEvent me)

and the

public void mouseReleased(MouseEvent me)

inside the mousePressed() take the Event and find the point where it was pressed and inside the mouseReleased picks up the Event , find the Point where it was Released and use

ar.add(new MyPoints(pointPressed,pointReleased));

now you have an array with your Points where the Vents occurred

Browser other questions tagged

You are not signed in. Login or sign up in order to post.