在javafx应用程序中使用图像的动画不起作用

时间:2017-12-20 06:35:40

标签: java javafx

我正在使用时间轴动画对图像数组进行动画制作。该阵列是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:Children:重复的孩子。

但问题仍然是我看不到程序在屏幕上运行。我在场景中添加了根组,但我在屏幕上看不到任何内容。我的新计划:

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

}

1 个答案:

答案 0 :(得分:0)

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

这段代码可以完成所有工作。这里的不透明度来自im.opacityProperty(),它是1并且变为1.因此没有淡出。

doAnimation()是递归方法和循环。我们使用时间轴动画来使动画变得平滑,否则如果我使用了循环则动画会闪烁。由于我不是JavaFx api中使用的动画专家,因此我花了差不多一个月的时间才能使它工作。

我所做的动画类似于动画gif,你有一系列的图像,当一个接一个地运行时给出一个动画效果。

代码仍需要一些工作。在doAnimation()方法中,我创建了KeyFrame数组:KeyFrame [] kf = new KeyFrame [images.length];我认为不是必需的。

此外,无需使用Group类。我用过HBox来包含ImageView。 ImageView正在动画。

public class Animation_Program_version3扩展了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);


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

}