LucioMaximo
LucioMaximo

Reputation: 51

How do I get the current co-ordinates of a Circle/Node during a TranslationTransition Javafx

This has been racking by brain for two days now. I've read through the javadocs for node/circle and can't seem to find a solution.

I have made a set of circles and have them grouped as a node.

 target = new Group(OuterRingTarget, OuterWhiteRingTarget, MiddleRingTarget, MiddleWhiteRingTarget, InnerRingTarget, BullseyeTarget);

I set the sizes and locations in the the animation e.g

    OuterRingTarget.setCenterX(200);
    OuterRingTarget.setCenterY(200);
    OuterRingTarget.setRadius(25);

    OuterWhiteRingTarget.setCenterX(200);
    OuterWhiteRingTarget.setCenterY(200);
    OuterWhiteRingTarget.setRadius(22);
    OuterWhiteRingTarget.setStroke(Color.WHITE);

etc

I create 4 random numbers (starting x/y and setBy x/y) for the TranslateTransition. For some reason .settoX doesn't work so I had to use setByX.

       final TranslateTransition targetAnimation = new TranslateTransition(Duration.seconds(2));
        targetAnimation.setNode(target);
        targetAnimation.setFromX(x0);
        targetAnimation.setByX(x1);
        targetAnimation.setFromY(y0);
        targetAnimation.setByY(y1);
        targetAnimation.setCycleCount(1);
        targetAnimation.onFinishedProperty().set(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                AnimationTargetStart();
            }
        });
        targetAnimation.play();

I need to check when the user clicks, whether the co-ordinates that the user clicked is equal to where the circles are located (i.e that the user has clicked the node). And to do this I need to check where the circles are mid-translationtransition.

Someone suggested a double property on another thread, I tried it, but it didn't work:

DoubleProperty xValue = new SimpleDoubleProperty();
    xValue.bind(BullseyeTarget.centerXProperty());
    xValue.addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue arg0, Object arg1, Object arg2) {

        System.out.println(" Current center X: " + (double) arg2);
    }
});

I also tried:

     double TargetXCentre = BullseyeTarget.getCenterX();
     double TargetYCentre = BullseyeTarget.getCenterY();

Which just returned the value that I set earlier, rather than returning the updated value during the transition. Is there a way of setting the .centerX during the transition without moving the shape itself? or getting the current position of the circles/node during the translationtransition.

I'm sure I am overcomplicating things, and that there is a way of setting each circle to have an onclick mouse event to detect hits, so that I don't need to do calculations for a hit, but I'm not sure how to do this either.

Cheers

Lucio

Upvotes: 0

Views: 277

Answers (2)

LucioMaximo
LucioMaximo

Reputation: 51

The solution to this question was to change the following:

    target = new Group(OuterRingTarget,OuterWhiteRingTarget,MiddleRingTarget,MiddleWhiteRingTarget,InnerRingTarget,BullseyeTarget);
    Group root = new Group(RectangleMouseBox,target);

to

    target = new Group(OuterRingTarget, OuterWhiteRingTarget, MiddleRingTarget, MiddleWhiteRingTarget, InnerRingTarget, BullseyeTarget);
    Group root = new Group(RectangleMouseBox);
    root.getChildren().addAll(target);

Upvotes: 0

James_D
James_D

Reputation: 209330

You shouldn't need to do the coordinate computation: just register a mouse listener with the node you are interested in. I'm not sure exactly what your requirements are, but

target.setOnMousePressed(event -> {
    System.out.println("hit target!");
});

should do what you want. You can replace target with any of the component circles if you need.

If you did want to track coordinates, by the way, you need to know that the translate transition works by manipulating the translateX and translateY properties of the node that is being animated. These property values are essentially added to the centerX and centerY properties of the circles (this is something of a simplification, but it will work). The boundsInParent property of a node contains the bounds of the node in the coordinate system of its parent node (i.e. the node to whose getChildren() list it is added), and accounts for all transformations (including the translation defined by the translateX and translateY properties). So if you registered a mouse listener with the parent of the target, you could do something along the lines of

targetContainer.setOnMousePressed(event -> {
    // coordinates of mouse relative to targetContainer:
    double mouseX = event.getX();
    double mouseY = event.getY();
    if (target.getBoundsInParent().contains(mouseX, mouseY)) {
        System.out.println("Hit target");
    }
});

(This isn't quite the same as the code above, since the bounds represent the rectangular region containing the target, whereas a mouse event handler registered on the target itself will only be invoked if the mouse is pressed on the target itself - i.e. it takes the shape into account.)

The first version (registering the mouse listener with the target) is more natural and probably what you want.

Upvotes: 1

Related Questions