Find the X coordinate in ancestor's coordinate system that corresponds to the X=0 axis in descendant's JavaFX coordinate system . - Java JavaFX

Java examples for JavaFX:Node

Description

Find the X coordinate in ancestor's coordinate system that corresponds to the X=0 axis in descendant's JavaFX coordinate system .

Demo Code

/*/*from   w w  w.  j  a va 2  s.co m*/
 * Copyright 2013 Jason Winnebeck
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import javafx.scene.Node;

public class Main {
    /**
     * Find the X coordinate in ancestor's coordinate system that corresponds to the X=0 axis in
     * descendant's coordinate system.
     *
     * @param descendant a Node that is a descendant (direct or indirectly) of the ancestor
     * @param ancestor   a Node that is an ancestor of descendant
     */
    public static double getXShift(Node descendant, Node ancestor) {
        double ret = 0.0;
        Node curr = descendant;
        while (curr != ancestor) {
            ret += curr.getLocalToParentTransform().getTx();
            curr = curr.getParent();
            if (curr == null)
                throw new IllegalArgumentException(
                        "'descendant' Node is not a descendant of 'ancestor");
        }

        return ret;
    }
}

Related Tutorials