2016-04-20 23 views
0

我正在嘗試創建一個運行線程而不是時間線的程序。下面是我的修改程序。我不知道爲什麼它不起作用。任何提示將不勝感激。該線程使用一個任務來啓動動畫。謝謝你的幫助。從時間線轉換到線程

import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Arc; 
import javafx.scene.shape.ArcType; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class ch30 extends Application { 
    @Override // Override the start method in the Application class 
    public void start(Stage primaryStage) {  
    FanPane fan = new FanPane(); 

    HBox hBox = new HBox(5); 
    Button btPause = new Button("Pause"); 
    Button btResume = new Button("Resume"); 
    Button btReverse = new Button("Reverse"); 
    hBox.setAlignment(Pos.CENTER); 
    hBox.getChildren().addAll(btPause, btResume, btReverse); 

    BorderPane pane = new BorderPane(); 
    pane.setCenter(fan); 
    pane.setBottom(hBox); 

    // Create a scene and place it in the stage 
    Scene scene = new Scene(pane, 200, 200); 
    primaryStage.setTitle("Exercise15_28"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 

    Runnable first = new Begin(); 

    Thread t1 = new Thread(first); 

    t1.start(); 









    //Timeline animation = new Timeline(
     //new KeyFrame(Duration.millis(100), e -> fan.move())); 
    //animation.setCycleCount(Timeline.INDEFINITE); 
    //animation.play(); // Start animation 

    scene.widthProperty().addListener(e -> fan.setW(fan.getWidth())); 
    scene.heightProperty().addListener(e -> fan.setH(fan.getHeight())); 

    //btPause.setOnAction(e -> first.wait()); 
    btResume.setOnAction(e -> first.run()); 
    btReverse.setOnAction(e -> fan.reverse()); 
    } 

    /** 
    * The main method is only needed for the IDE with limited 
    * JavaFX support. Not needed for running from the command line. 
    */ 
    public static void main(String[] args) { 
    launch(args); 


    } 
} 

class FanPane extends Pane { 
    private double w = 200; 
    private double h = 200; 
    private double radius = Math.min(w, h) * 0.45; 
    private Arc arc[] = new Arc[4]; 
    private double startAngle = 30; 
    private Circle circle = new Circle(w/2, h/2, radius); 

    public FanPane() { 
    circle.setStroke(Color.BLACK); 
    circle.setFill(Color.WHITE); 
    getChildren().add(circle); 

    for (int i = 0; i < 4; i++) { 
     arc[i] = new Arc(w/2, h/2, radius * 0.9, radius * 0.9, startAngle + i * 90, 35); 
     arc[i].setFill(Color.RED); // Set fill color 
     arc[i].setType(ArcType.ROUND); 
     getChildren().addAll(arc[i]); 
    } 
    } 

    private double increment = 5; 

    public void reverse() { 
    increment = -increment; 
    } 

    public void move() { 
    setStartAngle(startAngle + increment); 
    } 

    public void setStartAngle(double angle) { 
    startAngle = angle; 
    setValues(); 
    } 

    public void setValues() { 
    radius = Math.min(w, h) * 0.45; 
    circle.setRadius(radius); 
    circle.setCenterX(w/2); 
    circle.setCenterY(h/2); 

    for (int i = 0; i < 4; i++) { 
     arc[i].setRadiusX(radius * 0.9); 
     arc[i].setRadiusY(radius * 0.9); 
     arc[i].setCenterX(w/2); 
     arc[i].setCenterY(h/2); 
     arc[i].setStartAngle(startAngle + i * 90); 
    }  
    } 

    public void setW(double w) { 
    this.w = w; 
    setValues(); 
    } 

    public void setH(double h) { 
    this.h = h; 
    setValues(); 
    } 
} 

    class Begin implements Runnable { 
     private int times = 1000; 
     FanPane fan = new FanPane(); 

     public Begin(){ 
      // times = t; 
     } 

     @Override 
     public void run(){ 
      //for (int i = 0; i < times; i++) 
      //{ 
       fan.move(); 
      } 
     } 
+0

你對此有何看法?除了有關違反JavaFX的單線程規則(您正在從後臺線程更改UI)的問題之外,它似乎要做的只是立即,一次,移動並調整該圓圈的大小。 –

+0

我希望它不斷地多次移動圓圈。 – milliehol

+0

那麼使用'Timeline'有什麼問題?這是這類問題的常用方法。 –

回答

0

您的線程需要一個循環,該循環會反覆暫停並更新UI。如果您想使用明確的線程而不是更明顯的Timeline,則需要自行編寫該循環的代碼。

無法從您創建的後臺線程調用UI的更新:要更新FX應用程序線程上的UI,您必須將調用包裝爲更新UI的方法(Platform.runLater(...))。

+0

目前在我的程序中註釋掉的循環是否工作?它是否需要成爲嵌套循環才能暫停然後更新? – milliehol