Function to take coordinates mouse drag

-4

I'm developing a paint in java and would like to know how to create a function that stores the mouse coordinates of the initial and final mousePressed and mouseReleased. Thanks!

    
asked by anonymous 22.07.2016 / 14:02

1 answer

0

A good fireplace 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) 
}

You can 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 () takes the event and finds the point where it was pressed and within the mouseReleased takes 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 events occurred

    
24.07.2016 / 05:26