2017-01-02 75 views
-3

在我的程序中,當我的兩個矩形匹配時,分數遞增1。但它只能使用一次。我知道我需要在我的程序中加入一個循環,我只是遇到了一些麻煩。而不是if語句,我把一個while()循環,它仍然沒有工作。請幫忙。你會看到問題出現在哪裏,我會發表評論。什麼時候應該是我的循環的條件

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; 
import javafx.animation.FadeTransition; 
import javafx.geometry.Bounds; 

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(); 
        // next path transition 
      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(); 


     b.setOnAction((ActionEvent event) -> { 
      bi.setVisible(false); 
      biRec.setVisible(false); 
      circle.setVisible(false); 
      rec.setVisible(false); 
       b.setVisible(false);  
      Circle big = new Circle(); 

      Rectangle bigRec = new Rectangle(); 
      Circle circ = new Circle(); 
      Rectangle r = new 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); 


      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: cursor;" + 
       "-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);   

      match.setOnAction((ActionEvent evt) -> { 

        Bounds bBounds = bigRec.getBoundsInParent(); 
        Bounds bounds = r.getBoundsInParent(); 


        // problem starts here 
         int total = 0; 
        if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) { 
         total += 1; 
         String str = Integer.toString(total); 
         Label score = new Label(str); 
         score.setFont(new Font("Courier New bold", 20)); 
          score.setLayoutX(100); 
          score.setLayoutY(200); 
          p.getChildren().add(score); 

          pt.stop(); 
          pt2.stop(); 
          try { 
           Thread.sleep(1000); 
          pt.play(); 
          pt2.play(); 
         } 
          catch (Exception e) {} 
       } 
//    end the event 
     }); 

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

      FadeTransition ft = new FadeTransition(Duration.millis(1500), big); 
      ft.setFromValue(0.1); 
      ft.setToValue(1); 
      ft.play(); 
      //transition for big rec 
      FadeTransition ft2 = new FadeTransition(Duration.millis(1500), bigRec); 
      ft2.setFromValue(0.1); 
      ft2.setToValue(1); 
      ft2.play(); 
      //transition for circ 
      FadeTransition ft3 = new FadeTransition(Duration.millis(1500), circ); 
      ft3.setFromValue(0.1); 
      ft3.setToValue(1); 
      ft3.play(); 
      //transition for r 
      FadeTransition ft4 = new FadeTransition(Duration.millis(1500), r); 
      ft4.setFromValue(0.1); 
      ft4.setToValue(1); 
      ft4.play(); 
      //transition for match 
      FadeTransition ft5 = new FadeTransition(Duration.millis(1500), match); 
      ft5.setFromValue(0.1); 
      ft5.setToValue(1); 
      ft5.play(); 
      //transition for pause button 
      FadeTransition ft6 = new FadeTransition(Duration.millis(3000), b2); 
      ft6.setFromValue(0.1); 
      ft6.setToValue(1); 
      ft6.play(); 
      //transtion for play button 
      FadeTransition ft7 = new FadeTransition(Duration.millis(3000), b3); 
      ft7.setFromValue(0.1); 
      ft7.setToValue(1); 
      ft7.play(); 

     }); 

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

爲了獲得更好的答案,請嘗試創建[mcve] - 特別是,在您的問題中存在大量與您的問題完全無關的代碼。 – assylias

+0

每當您點擊「匹配」按鈕時,您的總計數器就會重置爲「0」。我想這是錯誤。 –

回答

1

移動的總變量,如下圖所示:

public class TransoFo extends Application { 
    private int total; 
    public void start(Stage stage) { 

來自:

// problem starts here 
if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) { 
    total += 1; 

但是,你仍然需要修復的總被顯示的方式。據overwritting上一共有

enter image description here

希望這有助於。

+0

謝謝,我解決了把標籤作爲實例的數字重疊,然後只設置總分新的分數。 – IbrahimLikeJava

相關問題