2016-12-28 9 views
0

我的程序轉換應該停止(通過調用stop()方法,您會看到,它的註釋標記爲//my problem starts here),當兩個矩形在同一軸上相遇時。但是由於某些原因,當不在同一軸上和當它們在同一軸上時,轉換停止。沒有語法錯誤。當兩個矩形不符合時,我的Javafx程序的轉換不會繼續播放

請幫幫我。非常感謝你。

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

你在期待這個代碼做,爲什麼?它在做什麼與你期望的不同?換句話說,這裏的實際問題是什麼? –

+0

你有沒有試過調試?它使您能夠通過逐行瀏覽並查看內存和變量的狀態來了解程序的行爲。 – Thrasher

+0

當兩個矩形都在y軸上時,我的程序轉換應該停止。但由於某些原因,當矩形不在同一個Y軸上時,它們仍然停止。其次,我在向Stack Overflow發佈之前調試了我的應用程序。 – IbrahimLikeJava

回答

1

關你Rectangle S的的y屬性是不是在你的代碼的任何地方進行修改。他們始終保持默認值0.0。這就是爲什麼y == y1總是產生truePathTransition使用變換(translateX,translateYrotate)。

即使你解決了這個問題,你也需要知道你正在處理的浮點值是計算結果,這意味着你不可能得到完全匹配。你需要考慮到一些錯誤...

此外,你需要知道的事實,即使用PathTransition.OrientationType.ORTHOGONAL_TO_TANGENTRectangle將被旋轉,這意味着舊的頂部可能不再在頂部,這導致您想要比較的問題。

下面的示例檢查,如果兩個rects在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(); 
    } 
});