簡體   English   中英

在Javafx應用程序中使用圖像的動畫不起作用

[英]Animation using images in javafx application does not work

我正在使用時間軸動畫制作圖像數組的動畫。 該陣列為299張圖像。 從0到298圖像進行一次迭代,然后動畫停止。

它應該不斷進行動畫處理,但是不起作用。 我正在使用時間軸動畫為每個imageview使用opacityProperty()。 一旦完成一個圖像動畫,便轉到下一個圖像。 但是當它達到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);
}

}

我已經更正了程序,現在沒有得到此錯誤java.lang.IllegalArgumentException:子代:重復的子代。

但是問題仍然是我沒有在屏幕上看到正在運行的程序。 我在場景中添加了根組,但在屏幕上看不到任何東西。 我的新程序:

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);
    }

}

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

這段代碼完成了所有工作。 在這里,不透明度從im.opacityProperty()變為1到1。所以沒有褪色。

doAnimation()是遞歸方法和循環。 我們已經使用時間軸動畫來使動畫平滑,否則,如果我使用了循環動畫,動畫就會閃爍。 我花了將近一個月的時間才能使它工作,因為我不是JavaFx api中使用的動畫專家。

我做的動畫類似於gif動畫,其中有一系列圖像,當一個接一個地運行時,會產生動畫效果。

該代碼仍需要一些工作。 在doAnimation()方法中,我創建了KeyFrame數組:KeyFrame [] kf = new KeyFrame [images.length]; 我覺得這不是必需的。

同樣,也不需要使用Group類。 我使用了HBox來包含ImageView。 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);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM