This discussion is archived
5 Replies Latest reply: Dec 18, 2012 6:02 AM by 916652 RSS

How can i get real  coordinates of one node?

916652 Newbie
Currently Being Moderated
just like subject.
I want to use TranslateTransition to move one node from this node's coordinates to other node's coordinates.When I try to get these two node's coordinates.I get the same coordinates.No mater getBoundsInLocal,getBoundsInParent,getLayoutBounds,getLayoutX,getX,getScaleX.
  • 1. Re: How can i get real  coordinates of one node?
    James_D Guru
    Currently Being Moderated
    getBoundsInLocal() does not include any transforms: getBoundsInParent() does. So you probably need getBoundsInParent(). I'm not sure why that doesn't work: can you post a simple executable example?

    This works: dragging the circles updates their position using a Translate, double clicking them sends them to the "home" rectangle using the technique you describe (looks at the difference between the getBoundsInParent of the circle and the home rectangle and translates the circle by that amount. The console output shows the getBoundsInLocal don't change with the translation but the getBoundsInParent do.
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Bounds;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Text;
    import javafx.scene.transform.Translate;
    import javafx.stage.Stage;
    
    public class HomingCircles extends Application {
    
      private Rectangle homeRectangle;
    
      @Override
      public void start(Stage primaryStage) {
        Group group = new Group();
        group.getChildren().add(new Text(300, 20, "Home"));
        homeRectangle = new Rectangle(100, 100);
        homeRectangle.relocate(270, 30);
        homeRectangle.setFill(Color.AQUAMARINE);
        group.getChildren().add(homeRectangle);
        for (int i = 0; i < 10; i++) {
          Circle c = new DraggableCircle(Math.random() * 300,
              Math.random() * 300 + 100, 50).getCircle();
          c.setFill(new Color(0, 1, 1, 0.4));
          c.setStroke(Color.BLACK);
          group.getChildren().add(c);
        }
        Scene scene = new Scene(group);
        primaryStage.setScene(scene);
        primaryStage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    
      private class DraggableCircle {
        private final Circle circle;
        private double mouseAnchorX;
        private double mouseAnchorY;
    
        DraggableCircle(double centerX, double centerY, double radius) {
          this.circle = new Circle(centerX, centerY, radius);
          circle.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
              mouseAnchorX = event.getX();
              mouseAnchorY = event.getY();
            }
          });
          circle.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
              double x = event.getX();
              double y = event.getY();
              circle.getTransforms().add(
                  new Translate(x - mouseAnchorX, y - mouseAnchorY));
            }
          });
          circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
              if (event.getClickCount() == 2) {
                Bounds pBounds = circle.getBoundsInParent();
                Bounds lBounds = circle.getBoundsInLocal();
                System.out.println("Bounds in parent before move: " + pBounds);
                System.out.println("Bounds in local before move:" + lBounds);
                double myX = pBounds.getMinX();
                double myY = pBounds.getMinY();
                double homeX = homeRectangle.getBoundsInParent().getMinX();
                double homeY = homeRectangle.getBoundsInParent().getMinY();
                circle.getTransforms().add(new Translate(homeX - myX, homeY - myY));
                pBounds = circle.getBoundsInParent();
                lBounds = circle.getBoundsInLocal();
                System.out.println("Bounds in parent after move: " + pBounds);
                System.out.println("Bounds in local after move:" + lBounds);
              }
            }
    
          });
        }
    
        Circle getCircle() {
          return circle;
        }
      }
    }
  • 2. Re: How can i get real  coordinates of one node?
    916652 Newbie
    Currently Being Moderated
    I have try your sample code and it's work.Here is my code
    First,this is layout in FXML
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.geometry.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.effect.*?>
    <?import javafx.scene.image.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.text.*?>
    
    <fx:root type="javafx.scene.layout.StackPane" fx:id="mm" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
      <children>
        <BorderPane pickOnBounds="false" prefHeight="400.0" prefWidth="600.0">
          <bottom>
            <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0">
              <children>
                <HBox fx:id="hbUP" alignment="CENTER" prefHeight="200.0" prefWidth="600.0">
                  <children>
                    <ImageView fx:id="card1" fitHeight="150.0" fitWidth="200.0" onMouseClicked="#CardOnMouseClicked" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card2" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card3" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card4" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card5" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card6" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card7" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                    <ImageView fx:id="card8" fitHeight="150.0" fitWidth="200.0" onMouseEntered="#CardOnMouseEntered" onMouseExited="#CardOnMouseExited" pickOnBounds="true" preserveRatio="true">
                      <effect>
                        <InnerShadow />
                      </effect>
                      <image>
                        <Image url="@8F_1.png" preserveRatio="false" smooth="false" />
                      </image>
                      <rotationAxis>
                        <Point3D y="1.0" />
                      </rotationAxis>
                    </ImageView>
                  </children>
                  <padding>
                    <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x1" />
                  </padding>
                </HBox>
                <Button fx:id="btnReSelect" mnemonicParsing="false" onMouseClicked="#btnReSelectOnMouseClicked" text="洗牌重選">
                  <font>
                    <Font size="20.0" />
                  </font>
                </Button>
              </children>
            </VBox>
          </bottom>
          <center>
            <VBox fx:id="vbUP" alignment="CENTER" padding="$x1" prefHeight="200.0" prefWidth="600.0">
              <children>
                <Label text="請選上卦" textAlignment="CENTER" textOverrun="CLIP">
                  <font>
                    <Font name="System Bold" size="20.0" />
                  </font>
                </Label>
              </children>
            </VBox>
          </center>
          <padding>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </padding>
        </BorderPane>
      </children>
    </fx:root>
    And this is controller,*error in washcard()*
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package divination;
    
    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.PathTransition;
    import javafx.animation.PathTransition.OrientationType;
    import javafx.animation.PauseTransition;
    import javafx.animation.SequentialTransition;
    import javafx.animation.Timeline;
    import javafx.animation.TranslateTransition;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.shape.CubicCurveTo;
    import javafx.scene.shape.LineTo;
    import javafx.scene.shape.MoveTo;
    import javafx.scene.shape.Path;
    import javafx.scene.transform.Rotate;
    import javafx.util.Duration;
    
    /**
     * FXML Controller class
     *
     * @author bruce
     */
    public class NewInput2Controller extends StackPane {
    
        public enum pokerName {
    
            乾,
            震,
            坤,
            坎,
            巽,
            兑,
            艮,
            離
        }
        private String strUpChoice;//實際選的值
        private Timeline timeLine;
        private final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
        private final int cardCount = 8;
        private int[] poker = new int[8];
        private String strIsChoice = "N";//是否已經選牌
        private Double[] pointX = new Double[8];
        private Double[] pointY = new Double[8];
        private ImageView[] cards = new ImageView[8];
        @FXML
        private ImageView card1;
        @FXML
        private ImageView card2;
        @FXML
        private ImageView card3;
        @FXML
        private ImageView card4;
        @FXML
        private ImageView card5;
        @FXML
        private ImageView card6;
        @FXML
        private ImageView card7;
        @FXML
        private ImageView card8;
        @FXML
        private VBox vbUP;
        @FXML
        private HBox hbUP;
        @FXML
        private Button btnReSelect;
    
        /**
         * Initializes the controller class.
         */
        public NewInput2Controller() {
            FXMLLoader fxmlLoader2 = new FXMLLoader(getClass().getResource("NewInput2.fxml"));
            fxmlLoader2.setRoot(this);
            fxmlLoader2.setController(this);
            try {
                fxmlLoader2.load();
    
                for (int i = 0; i < 8; i++) {
                    poker[i] = i;
                }
                cards[0] = card1;
                cards[1] = card2;
                cards[2] = card3;
                cards[3] = card4;
                cards[4] = card5;
                cards[5] = card6;
                cards[6] = card7;
                cards[7] = card8;
                
                for (int i = 0; i < 8; i++) {
                    pointX[i] = cards.getBoundsInParent().getMinX();
    pointY[i] = cards[i].getBoundsInParent().getMinY();
    System.out.println("BoundsInParent="+cards[i].getBoundsInParent());
    System.out.println("BoundsInParent="+cards[i].getBoundsInLocal());
    }
    washCard();//洗牌

    } catch (Exception exception) {
    throw new RuntimeException(exception);
    }
    }

    private void washCard() {

    //洗牌
    for (int i = 0; i < 8; i++) {
    int a = (int) (Math.random() * cardCount);
    int b = (int) (Math.random() * cardCount);

    int tmp = poker[a];
    poker[a] = poker;
    poker[b] = tmp;
    }

    TranslateTransition[] tt = new TranslateTransition[8];
    for (int i = 0; i < 8; i++) {
    cards.getTransforms().removeAll();
    tt[i] = new TranslateTransition();
    tt[i].setDuration(Duration.millis(2000));
    tt[i].setFromX(0);
    tt[i].setFromY(0);
    tt[i].setToX(pointX[poker[i]]);
    tt[i].setToY(pointY[poker[i]]);
    tt[i].setCycleCount(1);
    tt[i].setAutoReverse(false);
    tt[i].setNode(cards[i]);
    }
    for (int i = 0; i < 8; i++) {
    tt[i].play();
    }
    }

    public String GetUpChoice() {
    return strUpChoice;
    }

    @FXML
    protected void btnReSelectOnMouseClicked(MouseEvent event) throws Exception {
    washCard();
    }

    @FXML
    protected void CardOnMouseEntered(MouseEvent event) throws Exception {
    String strID = event.getSource().toString();
    timeLine = new Timeline();
    timeLine.setCycleCount(Timeline.INDEFINITE);
    timeLine.setAutoReverse(true);
    if (strID.contains("card1")) {
    card1.getTransforms().removeAll();
    card1.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card1.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card1.rotateProperty(), 360d)));
    } else if (strID.contains("card2")) {
    card2.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card2.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card2.rotateProperty(), 360d)));
    } else if (strID.contains("card3")) {
    card3.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card3.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card3.rotateProperty(), 360d)));
    } else if (strID.contains("card4")) {
    card4.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card4.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card4.rotateProperty(), 360d)));
    } else if (strID.contains("card5")) {
    card5.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card5.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card5.rotateProperty(), 360d)));
    } else if (strID.contains("card6")) {
    card6.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card6.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card6.rotateProperty(), 360d)));
    } else if (strID.contains("card7")) {
    card7.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card7.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card7.rotateProperty(), 360d)));
    } else if (strID.contains("card8")) {
    card8.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card8.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card8.rotateProperty(), 360d)));
    }
    timeLine.play();
    }

    @FXML
    protected void CardOnMouseExited(MouseEvent event) throws Exception {
    timeLine.stop();
    String strID = event.getSource().toString();
    if (strID.contains("card1")) {
    card1.setRotate(0);
    } else if (strID.contains("card2")) {
    card2.setRotate(0);
    } else if (strID.contains("card3")) {
    card3.setRotate(0);
    } else if (strID.contains("card4")) {
    card4.setRotate(0);
    } else if (strID.contains("card5")) {
    card5.setRotate(0);
    } else if (strID.contains("card6")) {
    card6.setRotate(0);
    } else if (strID.contains("card7")) {
    card7.setRotate(0);
    } else if (strID.contains("card8")) {
    card8.setRotate(0);
    }
    }

    @FXML
    protected void CardOnMouseClicked(MouseEvent event) throws Exception {
    timeLine.stop();
    String strID = event.getSource().toString();
    if (strID.contains("card1")) {
    // card1.getTransforms().removeAll();
    this.strUpChoice = pokerName.values()[poker[0]].toString();

    Double dbNewLayoutX = (vbUP.getBoundsInParent().getWidth() / 2 + vbUP.getBoundsInParent().getMinX()) - (card1.getBoundsInParent().getWidth() / 2 + card1.getBoundsInParent().getMinX());
    Double dbNewLayoutY = -1 * (vbUP.getHeight() / 2 + vbUP.getLayoutY());

    TranslateTransition tt = new TranslateTransition(Duration.millis(4000));
    tt.setFromX(0);
    tt.setFromY(0);
    tt.setToX(dbNewLayoutX);
    tt.setToY(dbNewLayoutY);
    tt.setCycleCount(1);
    tt.setAutoReverse(false);
    tt.setNode(card1);
    tt.play();
    // SequentialTransition seqT = new SequentialTransition(card1, tt);
    // seqT.play();
    // seqT.setDelay(Duration.millis(4000));

    // Path path = new Path();
    // path.getElements().add(new MoveTo(dbNewLayoutX, dbNewLayoutX));
    //// path.getElements().add(new CubicCurveTo(40f, 10f, 390f, 240f, 1904, 50f));
    // PathTransition pathTransition = new PathTransition();
    // pathTransition.setDuration(Duration.millis(10000));
    // pathTransition.setNode(card1);
    // pathTransition.setPath(path);
    // pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
    // pathTransition.setCycleCount(0);
    // pathTransition.setAutoReverse(false);
    //
    // pathTransition.play();


    // hbUP.getChildren().remove(card1);
    // vbUP.getChildren().add(card1);
    } else if (strID.contains("card2")) {
    this.strUpChoice = pokerName.values()[poker[1]].toString();
    } else if (strID.contains("card3")) {
    this.strUpChoice = pokerName.values()[poker[2]].toString();
    } else if (strID.contains("card4")) {
    this.strUpChoice = pokerName.values()[poker[3]].toString();
    } else if (strID.contains("card5")) {
    this.strUpChoice = pokerName.values()[poker[4]].toString();
    } else if (strID.contains("card6")) {
    this.strUpChoice = pokerName.values()[poker[5]].toString();
    } else if (strID.contains("card7")) {
    this.strUpChoice = pokerName.values()[poker[6]].toString();
    } else if (strID.contains("card8")) {
    this.strUpChoice = pokerName.values()[poker[7]].toString();
    }
    }
    }
    Edited by: 913649 on 2012/12/8 上午 5:07                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 3. Re: How can i get real  coordinates of one node?
    James_D Guru
    Currently Being Moderated
    I just quickly scanned that, but it looks like the parents of the two nodes whose positions you're comparing are different. The parent of vbUP is the BorderPane, but the parent of the card is the HBox. So I don't think this will work; you need them to be in the same direct parent. But again, I haven't looked in detail.

    Incidentally, I don't think you need all those if statements in your event handlers. Can't you just do
        @FXML
        protected void CardOnMouseExited(MouseEvent event) throws Exception {
            timeLine.stop();
            Node card = (Node) event.getSource();
            card.setRotate(0);
        }
    for example?
  • 4. Re: How can i get real  coordinates of one node?
    916652 Newbie
    Currently Being Moderated
    I modify my code again.I try to use two HBOX layout to put imageview object.One is named choicecardarea and the other is named standbycardarea.I put 8 imageview in standbycardarea.When user move cursor into imageview,imageview will rotate.This work is done by CardOnMouseEntered method.When cursor is moved out imageview,then i stop the rotation.This work is done by CardOnMouseExited method.When user click one of imageview in standbycardarea,move the imageview from standbycardarea,add this card to choicecardarea .When user click the button named btnReSelect,i move the imageview in the choicecardarea into standbycardarea,then change position of these 8 imageview radom.
    In run time,I click one of imageview in the standbycardarea,the imageview is moved to choicecardarea ,when i click the other imageview,the new one will be move to choicecardarea ,the old one will be moved to standbycardarea.That's all right.But I click button btnReSelect,then do the same thing,the position of these card is confusion.I really don't know why.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package divination;
    
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.EnumMap;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ResourceBundle;
    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.Timeline;
    import javafx.animation.TranslateTransition;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.control.Button;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.transform.Rotate;
    import javafx.util.Duration;
    
    /**
     * FXML Controller class
     *
     * @author bruce
     */
    public class NewInput2Controller extends StackPane {
    
        public enum pokerName {
    
            乾(1),
            震(2),
            坤(3),
            坎(4),
            巽(5),
            兑(6),
            艮(7),
            離(8);
            private int numeric;
    
            pokerName(int numeric) {
                this.numeric = numeric;
            }
    
            /**
             * numeric getter. notice: you can not override ordinal()
             * assertEqual(200, HttpStatusCode.numeric("Ok"));
             */
            public int numeric() {
                return numeric;
            }
    
            /**
             * message getter. assertEqual("Ok", HttpStatusCode.Ok.message());
             */
            public String message() {
                return toString();
            }
            private static EnumMap<pokerName, Integer> forward =
                    new EnumMap<pokerName, Integer>(pokerName.class);
            private static HashMap<Integer, pokerName> reverse =
                    new HashMap<Integer, pokerName>();
    
            static {
                pokerName[] values = pokerName.values();
                for (pokerName value : values) {
                    forward.put(value, value.numeric());
                    reverse.put(value.numeric(), value);
                }
            }
    
            /**
             * assertEqual(200, HttpStatusCode.Ok.numeric());
             */
            public static pokerName code(int numeric) {
                return reverse.get(numeric);
            }
    
            /**
             * assertEqual(HttpStatusCode.Ok, HttpStatusCode.code("Ok"));
             */
            public static pokerName code(String message) {
                return pokerName.valueOf(message);
            }
    
            /**
             * assertEqual(HttpStatusCode.Ok, HttpStatusCode.code(200));
             */
            public static int numeric(String message) {
                return forward.get(pokerName.valueOf(message));
            }
    
            /**
             *
             * assertEqual(200, HttpStatusCode.numeric(HttpStatusCode.Ok));
             */
            public static int numeric(pokerName code) {
                return code.numeric();
            }
    
            /**
             * assertEqual("Ok", HttpStatusCode.message(200));
             */
            public static String message(int numeric) {
                return code(numeric).message();
            }
    
            /**
             * assertEqual("Ok", HttpStatusCode.message(HttpStatusCode.Ok));
             */
            public static String message(pokerName code) {
                return code.message();
            }
        }
        private String strUpChoice;//實際選的值
        private Timeline timeLine;
        private final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
        private final int cardCount = 8;
        private int[] pokerOld = new int[8];
        private int[] pokerNew = new int[8];
        private String strIsChoice = "N";//是否已經選牌
        private ImageView[] cards = new ImageView[8];
        private ArrayList<ImageView> dd =new ArrayList<ImageView>();
        private ImageView choiceCard;
        @FXML
        private ImageView card1;
        @FXML
        private ImageView card2;
        @FXML
        private ImageView card3;
        @FXML
        private ImageView card4;
        @FXML
        private ImageView card5;
        @FXML
        private ImageView card6;
        @FXML
        private ImageView card7;
        @FXML
        private ImageView card8;
        @FXML
        private HBox choicecardarea;
        @FXML
        private HBox standbycardarea;
        @FXML
        private Button btnReSelect;
    
        /**
         * Initializes the controller class.
         */
        public NewInput2Controller() {
            FXMLLoader fxmlLoader2 = new FXMLLoader(getClass().getResource("NewInput2.fxml"));
            fxmlLoader2.setRoot(this);
            fxmlLoader2.setController(this);
            try {
                fxmlLoader2.load();
    
                for (int i = 0; i < 8; i++) {
                    pokerOld[i] = i;
                }
                pokerNew = pokerOld.clone();
                cards[0] = card1;
                cards[1] = card2;
                cards[2] = card3;
                cards[3] = card4;
                cards[4] = card5;
                cards[5] = card6;
                cards[6] = card7;
                cards[7] = card8;
    //            dd.add(card1);
    //            dd.add(card2);
    //            dd.add(card3);
    //            dd.add(card4);
    //            dd.add(card5);
    //            dd.add(card6);
    //            dd.add(card7);
    //            dd.add(card8);
            } catch (Exception exception) {
                throw new RuntimeException(exception);
            }
        }
    
        private void washCard() {
            //洗牌
            for (int i = 0; i < 8; i++) {
                int a = (int) (Math.random() * cardCount);
                int b = (int) (Math.random() * cardCount);
    
                int tmp = pokerNew[a];
                pokerNew[a] = pokerNew;
    pokerNew[b] = tmp;
    }

    TranslateTransition[] tt = new TranslateTransition[8];
    for (int i = 0; i < 8; i++) {
    cards[pokerNew[i]].getTransforms().clear();
    tt[i] = new TranslateTransition();
    tt.setDuration(Duration.millis(2000));
    // tt[i].setFromX(cards[pokerNew[i]].getBoundsInParent().getMinX());
    // tt[i].setFromY(cards[pokerNew[i]].getBoundsInParent().getMinY());
    tt[i].setByX(cards[pokerOld[i]].getBoundsInParent().getMinX() - cards[pokerNew[i]].getBoundsInParent().getMinX());
    tt[i].setByY(cards[pokerOld[i]].getBoundsInParent().getMinY() - cards[pokerNew[i]].getBoundsInParent().getMinY());
    // tt[i].setCycleCount(1);
    tt[i].setAutoReverse(false);
    tt[i].setNode(cards[pokerNew[i]]);
    }
    for (int i = 0; i < 8; i++) {
    tt[i].play();
    }
    pokerOld = pokerNew.clone();
    for (int i = 0; i < 8; i++) {
    cards[i].getTransforms().clear();
    }
    standbycardarea.requestLayout();
    choicecardarea.requestLayout();
    }

    public String GetUpChoice() {
    return strUpChoice;
    }

    @FXML
    protected void btnReSelectOnMouseClicked(MouseEvent event) throws Exception {
    if (strIsChoice.equals("Y")) {
    this.choicecardarea.getChildren().remove(choiceCard);
    this.standbycardarea.getChildren().add(choiceCard);
    this.choiceCard = null;
    this.strIsChoice = "N";
    this.strUpChoice = "";
    }
    washCard();
    }

    @FXML
    protected void CardOnMouseEntered(MouseEvent event) throws Exception {
    Node card = (Node) event.getSource();
    timeLine = new Timeline();
    timeLine.setCycleCount(Timeline.INDEFINITE);
    timeLine.setAutoReverse(true);
    card.getTransforms().clear();
    card.getTransforms().addAll(ry);
    timeLine.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
    new KeyValue(card.rotateProperty(), 0d)),
    new KeyFrame(Duration.seconds(2),
    new KeyValue(card.rotateProperty(), 360d)));
    timeLine.play();
    System.out.println("mouse enter node=" + card.getId());
    System.out.println("mouse enter card x location=" + card.getBoundsInParent().getMinX());
    }

    @FXML
    protected void CardOnMouseExited(MouseEvent event) throws Exception {
    Node card = (Node) event.getSource();
    timeLine.stop();
    card.setRotate(0);
    }

    @FXML
    protected void CardOnMouseClicked(MouseEvent event) throws Exception {
    timeLine.stop();
    for (int i = 0; i < 8; i++) {
    cards[i].getTransforms().clear();
    // dd.get(i).getTransforms().clear();
    System.out.println("cards["+i+"].getTransforms().size()=" + cards[i].getTransforms().size());
    }
    standbycardarea.requestLayout();
    choicecardarea.requestLayout();

    if (strIsChoice.equals("Y")) {
    this.choicecardarea.getChildren().remove(choiceCard);
    this.standbycardarea.getChildren().add(choiceCard);
    }
    ImageView card = (ImageView) event.getSource();
    card.getTransforms().removeAll();
    this.strUpChoice = pokerName.message(Integer.parseInt(card.getId().substring(4)));
    this.choiceCard = card;
    this.standbycardarea.getChildren().remove(card);
    this.choicecardarea.getChildren().add(card);

    this.strIsChoice = "Y";
    }
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 5. Re: How can i get real  coordinates of one node?
    916652 Newbie
    Currently Being Moderated
    does anyone have idea?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points