IbrahimLikeJava
IbrahimLikeJava

Reputation: 91

My Javafx program's transition doesn't keep on playing when the two rectangles don't meet

My program's transition is supposed to stop (by invoking the stop() method, you'll see, it's where the comment is marked //my problem starts here), only when the two rectangles meet on the same axis. But for some reason the transition stops when the aren't on the same axis and when they are on the same axis. Has no syntax error.

Please help me. Thank you very much.

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);
    }  
}

Upvotes: 0

Views: 197

Answers (1)

fabian
fabian

Reputation: 82461

The y property of none of your Rectangles is not modified anywhere in your code. They always keep their default values of 0.0. This is why y == y1 always yields true. PathTransition works with transforms (translateX, translateY, rotate).

Even if you fix this issue, you need to be aware of the fact that you're dealing with floating point values here that are the result of a calculation which means you're unlikely to get a exact match. You need to allow for some error there...

Also you need to be aware of the fact that using PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT the Rectangles will be rotated, which means the old top may no longer be at the top, which leads to the question which points you want to compare.

The following example checks, if both rects overlap in y coordinates:

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();
    }
});

Upvotes: 1

Related Questions