2016-07-30 109 views
1

我想暫停PathTransition並使用鍵盤鍵重新啓動P. 而且我正嘗試使用UP/DOWN鍵增加和減小動畫速度。onKeyPressed函數不能在JavaFX中工作

但是當我運行代碼時,這些按鈕似乎不起作用。我究竟做錯了什麼?

package exercise_15_29; 

import javafx.animation.Animation; 
import javafx.animation.PathTransition; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.scene.shape.Polygon; 
import javafx.scene.input.KeyCode; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class Exercise_15_29 extends Application { 
    Group car = new Group(); 
    Circle wheel1 = new Circle(15,95,5); 
    Circle wheel2 = new Circle(35,95,5); 
    Polygon body1 = new Polygon(); 
    Rectangle body2 = new Rectangle(0.0,80.0,50,10); 
    Line path = new Line(0,90,500,90); 

    int speed = 4000; 
    boolean play = true; 

    @Override 
    public void start(Stage primaryStage) { 

     body1.getPoints().addAll(new Double[]{ 
      10.0, 80.0, 
      20.0, 70.0, 
      30.0, 70.0, 
      40.0, 80.0 
     }); 
     body1.setFill(Color.BLUE); 
     body2.setFill(Color.SKYBLUE); 
     path.setVisible (false); 

     car.getChildren().addAll(wheel1,wheel2,body1,body2); 

     PathTransition pt = new PathTransition(); 
     pt.setDuration(Duration.millis(speed)); 
     pt.setPath(path); 
     pt.setNode(car); 
     pt.setCycleCount(Timeline.INDEFINITE); 
     pt.setAutoReverse(false); 

     pt.play(); 

     Pane root = new Pane(); 
     root.getChildren().add(car); 
     root.getChildren().add(path); 

     Scene scene = new Scene(root, 500, 100); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     root.setOnKeyTyped(e -> { 
      if(e.getCode() == KeyCode.P) { 
       if(play == true) 
        pt.stop(); 
       else 
        pt.play(); 
       play = !play; 
      } 
     }); 

     root.setOnKeyPressed(e -> { 
      if(e.getCode() == KeyCode.UP) 
       pt.setDuration(Duration.millis(++speed)); 
      else if(e.getCode() == KeyCode.DOWN) 
       pt.setDuration(Duration.millis(--speed)); 
     }); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+1

不知道爲什麼,但它似乎工作,如果你在'場景'而不是'窗格'上設置事件偵聽器。可能與焦點模型有關... – Itai

回答

2

此代碼的工作對我來說:

import javafx.animation.PathTransition; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.input.KeyCode; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.scene.shape.Polygon; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class Exercise_15_29 extends Application { 
    Group car = new Group(); 
    Circle wheel1 = new Circle(15, 95, 5); 
    Circle wheel2 = new Circle(35, 95, 5); 
    Polygon body1 = new Polygon(); 
    Rectangle body2 = new Rectangle(0.0, 80.0, 50, 10); 
    Line path = new Line(0, 90, 500, 90); 

    int speed = 4000; 
    boolean play = true; 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) { 

     body1.getPoints().addAll(new Double[]{ 
       10.0, 80.0, 
       20.0, 70.0, 
       30.0, 70.0, 
       40.0, 80.0 
     }); 
     body1.setFill(Color.BLUE); 
     body2.setFill(Color.SKYBLUE); 
     path.setVisible(false); 

     car.getChildren().addAll(wheel1, wheel2, body1, body2); 

     PathTransition pt = new PathTransition(); 
     pt.setDuration(Duration.millis(speed)); 
     pt.setPath(path); 
     pt.setNode(car); 
     pt.setCycleCount(Timeline.INDEFINITE); 
     pt.setAutoReverse(false); 

     pt.play(); 

     Pane root = new Pane(); 
     root.getChildren().add(car); 
     root.getChildren().add(path); 

     Scene scene = new Scene(root, 500, 100); 

     scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> { 
      System.out.println("P"); 
      if (e.getCode() == KeyCode.P) { 
       if (play) 
        pt.stop(); 
       else 
        pt.play(); 
       play = !play; 
      } 
     }); 
     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

//  root.addEventFilter(KeyEvent.KEY_PRESSED, e -> { 
//   if (e.getCode() == KeyCode.P) { 
//    if (play == true) 
//     pt.stop(); 
//    else 
//     pt.play(); 
//    play = !play; 
//   } 
//  }); 


     scene.setOnKeyPressed(e -> { 
      System.out.println("UP"); 
      if (e.getCode() == KeyCode.UP) 
       pt.setDuration(Duration.millis(++speed)); 
      else if (e.getCode() == KeyCode.DOWN) 
       pt.setDuration(Duration.millis(--speed)); 
     }); 
//  root.setOnKeyPressed(e -> { 
//   if(e.getCode() == KeyCode.UP) 
//    pt.setDuration(Duration.millis(++speed)); 
//   else if(e.getCode() == KeyCode.DOWN) 
//    pt.setDuration(Duration.millis(--speed)); 
//  }); 
    } 

} 

我改變KeyTyped事件KEY_PRESSED(我推薦這個),也用於根據scene.addEventFilter代替root.setOnKeyPressedhttps://stackoverflow.com/a/24126049/3291867,最後你不能改變車的速度,你不能改變動畫的持續時間或播放後(如我所知),你可以使用AnimationTimer這個。

+0

爲什麼我們無法更改動畫持續時間?如果我暫停然後改變速度會怎樣?我讀過'AnimationTimer',有沒有更簡單的方法來做到這一點? –

+0

我不確定,但我知道的是,您在播放動畫時無法更改持續時間。 @MohsinAnees – mojtab23