仅当两个矩形在同一轴上相遇时,我的程序的转换才应该停止(通过调用stop()方法,您会看到它在注释被标记为//my problem starts here的位置)。但是由于某种原因,当轴不在同一轴上并且它们在同一轴上时,过渡停止。没有语法错误。

请帮我。非常感谢你。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.control.*;
import javafx.event.Event;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.util.Duration;
import java.lang.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TransoFo extends Application{
    public void start(Stage stage) {
        Pane p = new Pane();
        Button b = new Button("Play");
        b.setStyle("-fx-background-radius: 3em;" +
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" +
                "-fx-text-fill: white;");
        b.setLayoutX(307);
        b.setLayoutY(400);



             Circle bi = new Circle();
            Rectangle biRec = new Rectangle();
            Circle circle = new Circle();
            Rectangle rec = new Rectangle();

            rec.setWidth(20);
            rec.setHeight(30);

            rec.setArcWidth(5);
            rec.setArcHeight(5);
            rec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");


            circle.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            circle.setCenterX(370);
            circle.setCenterY(250);
            circle.setRadius(50);

            biRec.setWidth(30);
            biRec.setHeight(20);
            biRec.setArcWidth(5);
            biRec.setArcHeight(5);
            biRec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            bi.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            bi.setCenterX(370);
            bi.setCenterY(250);
            bi.setRadius(100);
            p.getChildren().addAll(bi, biRec, circle, rec);
            // transition for small circle and rectangle
            PathTransition pt1 = new PathTransition();
            pt1.setDuration(Duration.millis(1200));
            pt1.setPath(bi);
            pt1.setNode(biRec);
            pt1.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt1.setCycleCount(Timeline.INDEFINITE);
            pt1.setAutoReverse(false);

           pt1.play();

             PathTransition pt3 = new PathTransition();
            pt3.setDuration(Duration.millis(800));
            pt3.setPath(circle);
            pt3.setNode(rec);
            pt3.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt3.setCycleCount(Timeline.INDEFINITE);
            pt3.setAutoReverse(false);

           pt3.play();

//            button play event lambda expression
        b.setOnAction((ActionEvent event) -> {
            bi.setVisible(false);
            biRec.setVisible(false);
            circle.setVisible(false);
            rec.setVisible(false);
                 b.setVisible(false);
            Circle big = new Circle();
            // create rectangle for big circle
            Rectangle bigRec = new Rectangle();
            Circle circ = new Circle();
            Rectangle r = new Rectangle();
            //event for small rectangle
            r.setWidth(20);
            r.setHeight(30);


            r.setArcWidth(5);
            r.setArcHeight(5);
            r.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            circ.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            circ.setCenterX(370);
            circ.setCenterY(300);
            circ.setRadius(50);


            bigRec.setWidth(30);
            bigRec.setHeight(20);
            bigRec.setArcWidth(5);
            bigRec.setArcHeight(5);
            bigRec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            big.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            big.setCenterX(370);
            big.setCenterY(300);
            big.setRadius(100);

            // transition for small circle and rectangle
            PathTransition pt2 = new PathTransition();
            pt2.setDuration(Duration.millis(1200));
            pt2.setPath(big);
            pt2.setNode(bigRec);
            pt2.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt2.setCycleCount(Timeline.INDEFINITE);
            pt2.setAutoReverse(true);

            pt2.play();

            PathTransition pt = new PathTransition();
            pt.setDuration(Duration.millis(800));
            pt.setPath(circ);
            pt.setNode(r);
            pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt.setCycleCount(Timeline.INDEFINITE);
            pt.setAutoReverse(false);

            pt.play();


            Button b2 = new Button(" | | ");
              b2.setStyle("-fx-background-radius: 3em;" +
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 70;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 700;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" +
                "-fx-text-fill: white;");
        b2.setLayoutX(670);
        b2.setLayoutY(10);

        b2.setOnAction((ActionEvent event1) -> {
            pt2.stop();
            pt.stop();
            });

       Button b3  = new Button(" ▶ ");
        b3.setStyle("-fx-background-radius: 3em;" +
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 70;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 700;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" +
                "-fx-text-fill: white;");
                   b3.setLayoutX(590);
                   b3.setLayoutY(10);

                   b3.setOnAction((ActionEvent event2) -> {
                                pt.play();
                                pt2.play();

                            });
            Button match = new Button(" Match ");
             match.setStyle("-fx-background-radius: 3em;" +
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" +
                "-fx-text-fill: white;");
                match.setLayoutX(310);
             match.setLayoutY(450);
         // here is the button Match's event (lambda expression)
             match.setOnAction((ActionEvent evt) -> {

                 // here is where my problem start
                  int y = (int) r.getY();
               int y1 = (int) bigRec.getY();


                 if(y == y1){
                        pt.stop();
                        pt2.stop();
                 }
                 //here is where it ends
        });

            p.getChildren().addAll(big, bigRec, circ, r, b2, b3, match);


        });

        p.getChildren().add(b);
        p.setStyle("-fx-background-color: #88ff4d;");
        Scene s = new Scene(p, 750, 650);
        stage.setScene(s);
        stage.show();
    }
    // launch Application
    public static void main(String[] args) {
        Application.launch(args);
    }
}

最佳答案

您所有yRectangle属性都不会在代码的任何地方被修改。他们始终保持默认值0.0。这就是为什么y == y1总是产生true的原因。 PathTransition用于转换(translateXtranslateYrotate)。

即使您解决了这个问题,您也需要了解一个事实,即您正在处理的浮点值是计算结果,这意味着您不太可能获得完全匹配。您需要在那里允许一些错误...

另外,您还需要注意以下事实:使用PathTransition.OrientationType.ORTHOGONAL_TO_TANGENTRectangle将被旋转,这意味着旧的顶部可能不再位于顶部,这引出了您要比较哪些点的问题。

以下示例检查两个矩形在y坐标中是否重叠:

match.setOnAction((ActionEvent evt) -> {
    Bounds bBounds = bigRec.getBoundsInParent();
    Bounds bounds = r.getBoundsInParent();
    if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) {
        pt.stop();
        pt2.stop();
    }
});

09-05 16:08