2016-06-27 10 views
0

Blockのグリッド6X6要素を作成するプロジェクトを作成しました。 Blockをクリックすると、オレンジ色の円が表示されます。問題は、ボタンをクリックすると、1秒ごとにランダムに右、左、上、下に移動する各円が表示されます。 1つのサークルが上がったように、次に左に行くと、それは再び上に行く。オブジェクトごとに異なるアニメーションを開始する方法JavaFX

class Block extends StackPane { 

    Rectangle rect = new Rectangle(40, 40); 

    Circle circ = new Circle(20); 
    EventHandler a = new EventHandler() { 
     public void handle(Event t) { 
      circ.setVisible(true); 
     } 
    }; 

    Block() { 

     rect.setFill(Paint.valueOf("WHITE")); 
     rect.setStroke(Paint.valueOf("BLACK")); 
     rect.setOnMouseClicked(a); 
     circ.setFill(Paint.valueOf("ORANGE")); 
     circ.setVisible(false); 

     this.getChildren().addAll(rect, circ); 
    } 

} 
public class JavaFXApplication8 extends Application { 

    @Override 
    public void start(Stage primaryStage) { 


     GridPane root = new GridPane(); 
     Block[][] matrice = new Block[6][6]; 
     for (int i = 0; i < 6; i++) { 
      matrice[i] = new Block[6]; 
      for (int j = 0; j < 6; j++) { 
       matrice[i][j] = new Block(); 
       root.add(matrice[i][j], j, i); 
      } 
     } 

     Scene scene = new Scene(root, 300, 250); 

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

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

質問とは無関係: 'Paint.valueOf(" ORANGE ")'の代わりに 'Color.ORANGE'を使用してください。 ( 'Color extends Paint')。 – fabian

+1

http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/をご覧ください – GOXR3PLUS

答えて

0

私は(ランダム)この例ではAnimationTimerは、あなたのブロックのランダム円が見えたりunvisibleになり、あなたがそれを使用する方法を示すために、JavaFXのAnimationTimerで例を作りました。

(例えばprimaryStage.show();)後にアプリの起動方法にこれを追加

// AnimationTimer 
    new AnimationTimer() { 

     Random random = new Random(); 
     private long nextSecond = 0; 
     private long refreshRate = 1_000_000_000L/6; 

     @Override 
     public void handle(long startNanos) { 

      //refreshing every 1 second/6 
      //(if you don't set that then it will try to run 
      // as fast as 50 frames per second) 
      if (startNanos > nextSecond) { 
       nextSecond = startNanos + refreshRate; 

       // Choose some random Numbers 
       int randomColumn = random.nextInt(6); 
       int randomRow = random.nextInt(6); 
       System.out.println(randomColumn + "," + randomRow); 

       // Do a random item visible 
       matrice[randomColumn][randomRow].circ.setVisible(random.nextBoolean());        

      } 
     } 
    }.start(); 

*あなたがメソッドを使用することができますGridPaneにブロックを移動するには:

 GridPane.setConstraints(Node node,int column,int row); 

その他http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/GridPane.html

関連する問題