using getX() getY() in a mouseclick

This is a long question. I've tried to make it more concise but I think you need all the info to assist.

Here's a summary: I'm trying to capture consecutive clicks as endpoints. I'll draw line2D objects between these points. I have a listener that sends x and y values from click events to a helper class. I'll never have more than two points, so the helper class only has firstPoint and secondPoint members. I get the clicks and send the co-ordinates to the helper class using evt.getX() and evt.getY(). The helper creates the points, but firstPoint ends up with the same co-ords as secondPoint. My detail on what's going on can be found below the code blocks.

Here's the code in my mouse click handler:

public class Dashboard extends javax.swing.JFrame {

    public static ClickMaster myClicks = new ClickMaster();
    public static boolean drawing = false;
    private MyPoint firstPoint = new MyPoint();
    private MyPoint secondPoint = new MyPoint();

. . .

private void MyDrawingPanelMouseClicked(java.awt.event.MouseEvent evt) {
        if (firstPoint.getPointType().equals(MyPoint.PointType.NULL)) {
            drawing = !drawing;
            firstPoint = myClicks.parseClick(evt.getX(), evt.getY(), drawing);
        } else if (drawing){
            drawing = !drawing;
            secondPoint = myClicks.parseClick(evt.getX(), evt.getY(), drawing);
            myClicks.parsePoints(firstPoint, secondPoint);
        }
}

Here's the helper class that returns points:

public class ClickMaster {

    MyPoint anyPoint = new MyPoint();

    public ClickMaster() {
        anyPoint.setPointType(MyPoint.PointType.NULL);
    }

    public MyPoint parseClick(double x, double y, boolean firstClick) {
            if (firstClick) {
                anyPoint.setPointType(MyPoint.PointType.ANCHOR);
            } else {
                anyPoint.setPointType(MyPoint.PointType.END);
            }

        anyPoint.setX(x);
        anyPoint.setY(y);

        return anyPoint;
    }

And here's my implementation of Point2D objects, in case it's relevant:

public class MyPoint extends Point2D {

    public enum PointType {

        ANCHOR, END, SOLO, NULL
    };

    double x;
    double y;
    PointType pointType;

    public MyPoint(PointType pType, double x, double y) {

        this.x = x;
        this.y = y;
        this.pointType = pType;
    }

    public MyPoint() {
        this.x = 0.0;
        this.y = 0.0;
        this.pointType = PointType.NULL;
    }

+ standard getters/setters as you would expect.

debugging shows that

firstPoint = myClicks.parseClick(firstX, firstY, drawing);

results in a point with the x and y values of the mouse event. This is expected.

Likewise,

secondPoint = myClicks.parseClick(evt.getX(), evt.getY(), drawing);

Results in a point with the x and y values of the second click. Also expected.

However, when I call

myClicks.parsePoints(firstPoint, secondPoint);

I see that firstPoint and secondPoint have the same x,y values.

I'm not sure but I believe it's because both firstPoint and secondPoint are getting their x and y values from the mouse evt. I think I need to put values of type double for x's and y's in to firstPoint and secondPoint as opposed to a reference to the double value returned from the mouse event by getX() and getY().

I just don't know how to make it happen.

Also, if there's a much better way to capture consecutive clicks and translate them to endpoints for shapes, I would welcome the feedback.


Actually, your problem is anyPoint. Since you only have one instance of ClickMaster, and anyPoint is only initialized once, you're returning a reference to the same object no matter how many times you call parseClick. ParseClick needs to create a new MyPoint every time it's called and return that.

链接地址: http://www.djcxy.com/p/83248.html

上一篇: 实现扩展Rectangle类的子类Square

下一篇: 在鼠标点击中使用getX()getY()