2017-03-14 20 views
0

3までカウントするカウンタが必要です。その後、0で再起動し、3まで無期限にカウントアップする必要があります。 しかし、私のカウンターが3に達すると、私のプログラマーが最後に行うことは、テキスト"0"を設定しています。 アニメーションを正しく使用していないと思います。 私が間違っていることを誰かが知っていれば幸いです。 これは実際の問題の単純化に過ぎないことに注意してください。JavaFXアニメーションが最初のサイクルの後に終了します

import javafx.animation.Animation; 
import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class GeneralTesting extends Application{ 
    private Text text; 
    private int counter = 1; 
    public static void main(String[] args) { 
     launch(); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     Pane pane = new Pane(); 

     text = new Text(500, 300, "0"); 

     text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100)); 

     Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), e -> {changeText(counter++);})); 
     animation.setCycleCount(Timeline.INDEFINITE); 
     animation.play(); 

     pane.getChildren().addAll(text); 

     Scene scene = new Scene(pane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public void changeText(int counter){ 
     if (counter > 5){ 
      counter = 0; 
     } 
     text.setText(String.valueOf(counter)); 
    } 

} 

答えて

1

の範囲を維持するためにMODを使用することです... 3までカウントする簡単な方法があるかもしれませんあなたローカル変数counter0にリセットしています。インスタンスフィールドは以前の値のままです。したがって、タイムラインの次の反復では、それはまだ5より大きく、ローカル変数を再びゼロに設定します。

完全にローカル変数を削除し、単にフィールドを更新:

import javafx.animation.Animation; 
import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class GeneralTesting extends Application{ 
    private Text text; 
    private int counter = 1; 
    public static void main(String[] args) { 
     launch(); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     Pane pane = new Pane(); 

     text = new Text(500, 300, "0"); 

     text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100)); 

     Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), e -> { 
      counter++; 
      changeText(); 
     })); 

     animation.setCycleCount(Timeline.INDEFINITE); 
     animation.play(); 

     pane.getChildren().addAll(text); 

     Scene scene = new Scene(pane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public void changeText(){ 
     if (counter > 5){ 
      counter = 0; 
     } 
     text.setText(String.valueOf(counter)); 
    } 

} 

まだよく、IntegerPropertyを使用し、その値にテキストバインド:ローカル変数と最初の事を

import javafx.animation.Animation; 
import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class GeneralTesting extends Application{ 
    private Text text; 
    public static void main(String[] args) { 
     launch(); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     Pane pane = new Pane(); 

     text = new Text(500, 300, "0"); 

     text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100)); 

     IntegerProperty counter = new SimpleIntegerProperty(1); 
     text.textProperty().bind(counter.asString()); 

     Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), 
      e -> counter.set((counter.get()+1) % 5))); 

     animation.setCycleCount(Timeline.INDEFINITE); 
     animation.play(); 

     pane.getChildren().addAll(text); 

     Scene scene = new Scene(pane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 


} 
+0

をまさに私が探していたものでした!どうもありがとうございます! – jaaBeg16

0

一つの方法は、

if (counter > 5) { 
    counter = 0 ; 
} 

では0から3

Pane pane = new Pane(); 

    Text text = new Text(500, 300, "0"); 
    int[] counter = 
    { 
     0 
    }; 
    text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100)); 

    Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), e 
      -> 
    { 
     text.setText(Integer.toString(counter[0]++ % 4)); 
    })); 
    animation.setCycleCount(Timeline.INDEFINITE); 
    animation.play(); 

    pane.getChildren().addAll(text); 

    Scene scene = new Scene(pane); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
関連する問題