2017-12-20 8 views
0

タイムラインアニメーションを使用して画像の配列のアニメーションを行っています。配列は299画像です。 0から298までの画像を一度反復してからアニメーションを停止します。javafxアプリケーションで画像を使用したアニメーションが動作しません

アニメーションは連続的に動作する必要がありますが動作しません。タイムラインアニメーションを使用して各画像ビューに対してopacityProperty()を使用しています。 1つの画像アニメーションが完了すると、次の画像に進みます。しかし、私はそれが298のイメージに達すると連続してループすることはできません。変数xは0になり、アニメーションが再び開始されます。

public class Animation_Program_version3 extends Application { 


    Timeline timeline = null; 
    Group rootGroup = null; 
    int x = 0; 
    Image [] images = new Image[299];; 
    ArrayList imageview = null; 

    public Animation_Program_version3() { 

    } 


    @Override 
    public void start(Stage primaryStage) { 

     primaryStage.setTitle("JavaFX Welcome"); 

     rootGroup = new Group(); 

     final Scene scene = new Scene(rootGroup, 800, 400, Color.BEIGE); 

     imageview = new ArrayList(); 

     int y = 0; 
      for(int x = -50; x < 100; x=x+1){ 
       images[y] = new Image("/Image"+x+".jpg", true); 
       imageview.add(new ImageView(images[y])); 
       y = y+1; 
      } 

     int y1 = 150; 
     for(int x = 99; x > -50; x=x-1){ 
      images[y1] = new Image("/Image"+x+".jpg", true); 
      imageview.add(new ImageView(images[y1]));  
      y1 = y1+1; 
     } 

     rootGroup.getChildren().addAll(imageview); 

     int x = 0; 

     timeline = new Timeline(); 

     doAnimation(); 

     primaryStage.setScene(scene); 

     `primaryStage.show(); ` 
} 
<code> 
public void doAnimation(){ 

    KeyFrame[] kf = new KeyFrame[images.length];  

    ImageView im = (ImageView)imageview.get(x); 

<code> 
    im.setImage(images[x]); 

    kf[x] = new KeyFrame(Duration.millis(1), new KeyValue(im.opacityProperty(), 0)); 

    timeline.getKeyFrames().add(kf[x]); 

    // When timeline animation is finished it executes the seetOnFinished Event 


    timeline.setOnFinished(new EventHandler<ActionEvent>() { 

    @Override 
    public void handle(ActionEvent event) { 

     if(x == 298){ 
      System.out.println("VALUE OF x:"+x); 
      x=0; -------> This is where code does not work When it reaches end of array and x initialize to 0 then animation stops. 


      Collections.reverse(imageview); 
      doAnimation(); 
     } 

/* This if loop works fine animation iterates through 0 to 298 images. */     
    if(x < 298){ 
     x++; 
     doAnimation(); 
    } 
    } 
    }); 
    timeline.play();  

} 

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

} 

プログラムを修正しましたが、今はこのエラーが発生していません。子:子が重複しています。

しかし、まだ問題は、私はプログラムが画面上で実行されて表示されません。私はシーンにルートグループを追加しましたが、画面上には何も表示されません。私の新しいプログラム:

public class Animation_Program_version3 extends Application { 


    Timeline timeline = null; 
    Group rootGroup = null; 
    int x = 0; 
    Image [] images = new Image[299];; 
    ArrayList imageview = null; 

    ImageView im = new ImageView(); 
    public Animation_Program_version3() { 
    //  this.imageview = new TreeSet(); 
    } 


@Override 
public void start(Stage primaryStage) { 

    primaryStage.setTitle("JavaFX Welcome"); 




rootGroup = new Group(); 

    final Scene scene = 
     new Scene(rootGroup, 800, 400, Color.BEIGE); 

// 
    // final Scene scene = 
    // new Scene(rootGroup, 800, 400, Color.BEIGE); 


    // int x = 0;  
//Image [] images = 
imageview = new ArrayList(); 





     int y = 0; 
     for(int x = -50; x < 100; x=x+1){ 
      images[y] = new Image("/Image"+x+".jpg", true); 
      imageview.add(new ImageView(images[y])); 
      y = y+1; 
     } 

      int y1 = 150; 
     for(int x = 99; x > -50; x=x-1){ 
      images[y1] = new Image("/Image"+x+".jpg", true); 
imageview.add(new ImageView(images[y1]));  

// imageview[y1] = new ImageView(images[y1]); 
      y1 = y1+1; 
     } 

//for (int i = 0; i < 299; i++) { 
// rootGroup.getChildren().addAll(imageview); 
//} 


int x = 0; 

timeline = new Timeline(); 




doAnimation(); 

    primaryStage.setScene(scene); 

primaryStage.show(); 


} 


public void doAnimation(){ 

KeyFrame[] kf = new KeyFrame[images.length];  

// im = (ImageView)imageview.get(x); 


    im.setImage(images[x]); 


    rootGroup.getChildren().setAll(im); 

    kf[x] = new KeyFrame(Duration.millis(1), new KeyValue(im.opacityProperty(), 0)); 

    timeline.getKeyFrames().add(kf[x]); 


timeline.setOnFinished(new EventHandler<ActionEvent>() { 

     @Override 
     public void handle(ActionEvent event) { 
       //  timeline = null; 

        if(x == 298){ 
         System.out.println("VALUE OF x:"+x); 
         x=0; 
        //  Collections.reverse(imageview); 
        // rootGroup.getChildren().setAll(imageview); 
        // 
         doAnimation(); 
        } 

        if(x < 298){ 
         System.out.println("Inside 298 OF x:"+x); 

         x++; 
         // Animation_Program_version3.rootGroup = null; 
        //  Animation_Program_version3.rootGroup = new Group(); 

         doAnimation(); 
        } 
     } 
    }); 
timeline.play();  


} 

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

} 
+0

を使用すると、特定のエラーを取得していますか?拡張また、あなたの質問を再フォーマットしてください。 – Pieter

+0

あなたのコードは 'java.lang.IllegalArgumentException:Children:duplicate children'を投げています、それはあなたの問題ですか? – aKilleR

+0

問題を修正しましたが、このjava.lang.IllegalArgumentExceptionが発生しません:子:重複子エラー – Mike

答えて

0

KF [x]は、新しいキーフレームを=(Duration.millis(10)、新しいです。KeyValue(im.opacityProperty()、1))。

このコードはすべての作業を行います。ここで、不透明度はim.opacityProperty()から1になり、1になります。したがって、フェードアウトはありません。

doAnimation()は再帰的なメソッドとループです。タイムラインアニメーションを使用してアニメーションをスムーズにする、または私がループのために使用していた場合、アニメーションがちらつくでしょう。これは私がJavaFx APIで使用されるアニメーションの専門家ではないので、動作させるにはほとんど月にかかりました。

私が行ったアニメーションはアニメーションGIFと似ていますが、アニメーションGIFには一連のイメージがあり、連続して実行するとアニメーション効果があります。

コードにはまだいくつかの作業が必要です。 doAnimation()メソッドで、KeyFrameの配列を作成しました。KeyFrame [] kf = new KeyFrame [images.length];私はそれが必要ではないと感じます。

また、グループクラスを使用する必要はありません。私はImageViewを含めるためにHBoxを使用しました。 ImageViewがアニメ化されています。

パブリッククラスAnimation_Program_version3は、アプリケーション{

Timeline timeline = null; 
    Group rootGroup = null; 
    int x = 0; 
    Image [] images = new Image[299];; 
    ArrayList imageview = null; 

    ImageView im = new ImageView(); 
    public Animation_Program_version3() { 
    //  this.imageview = new TreeSet(); 
    } 


@Override 
public void start(Stage primaryStage) { 

    primaryStage.setTitle("JavaFX Welcome"); 




rootGroup = new Group(); 


// 
    // final Scene scene = 
    // new Scene(rootGroup, 800, 400, Color.BEIGE); 


    // int x = 0;  
//Image [] images = 
imageview = new ArrayList(); 





     int y = 0; 
     for(int x = -50; x < 100; x=x+1){ 
      images[y] = new Image("/Image"+x+".jpg", true); 
      imageview.add(new ImageView(images[y])); 
      y = y+1; 
     } 

      int y1 = 150; 
     for(int x = 99; x > -50; x=x-1){ 
      images[y1] = new Image("/Image"+x+".jpg", true); 
imageview.add(new ImageView(images[y1]));  

// imageview[y1] = new ImageView(images[y1]); 
      y1 = y1+1; 
     } 

//for (int i = 0; i < 299; i++) { 
// rootGroup.getChildren().addAll(imageview); 
//} 
    HBox layout2 = new HBox(); 
     layout2.getChildren().add(im); 

Scene scene = 
     new Scene(layout2, 800, 400); 

int x = 0; 

timeline = new Timeline(); 






    primaryStage.setScene(scene); 

primaryStage.show(); 
doAnimation(); 

} 


public void doAnimation(){ 

KeyFrame[] kf = new KeyFrame[images.length];  

// im = (ImageView)imageview.get(x); 

System.out.println("WHAT IS THE VALUE OF:"+x); 
    im.setImage(images[x]); 
    System.out.println(images[x]); 

// rootGroup.getChildren().setAll(im); 

    kf[x] = new KeyFrame(Duration.millis(10), new KeyValue(im.opacityProperty(), 1)); 

    timeline.getKeyFrames().add(kf[x]); 


timeline.setOnFinished(new EventHandler<ActionEvent>() { 

     @Override 
     public void handle(ActionEvent event) { 
       //  timeline = null; 

        if(x == 298){ 
         System.out.println("VALUE OF x:"+x); 
         x=0; 
        //  Collections.reverse(imageview); 
        // rootGroup.getChildren().setAll(imageview); 
        // 
         doAnimation(); 
        } 

        if(x < 298){ 
         System.out.println("Inside 298 OF x:"+x); 

         x++; 
        //  im.setImage(images[x]); 
         // Animation_Program_version3.rootGroup = null; 
        //  Animation_Program_version3.rootGroup = new Group(); 

         doAnimation(); 
        } 
     } 
    }); 
timeline.play();  


} 

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

}