2017-12-20 1 views
0

타임 라인 애니메이션을 사용하여 이미지 배열의 애니메이션을 만들고 있습니다. 배열은 299 개 이미지입니다. 0에서 298까지 이미지를 한 번 반복 한 다음 애니메이션을 중단합니다.javafx 응용 프로그램에서 이미지를 사용하는 애니메이션이 작동하지 않습니다.

계속 애니메이션을 적용해야하지만 작동하지 않아야합니다. 타임 라인 애니메이션을 사용하여 각 이미지 뷰에 대해 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); 
    } 

} 
+0

당신이 특정 오류를받을 수 있나요 확장? 또한 질문을 다시 포맷하십시오. – Pieter

+0

'java.lang.IllegalArgumentException : Children : duplicate children'을 던지는 코드가 문제입니까? – aKilleR

+0

문제를 해결 했으므로 이제 java.lang.IllegalArgumentException이 발생하지 않을 것입니다. 자식 : 중복 자식 오류 – Mike

답변

0

KF는 [X] 새로운 키 프레임 = (Duration.millis (10), 새로운 키 값 (im.opacityProperty(), 1));

이 코드는 모든 작업을 수행합니다. 여기서 불투명도는 im.opacityProperty()에서 1로 1로 이동합니다. 따라서 페이드 아웃이 없습니다.

doAnimation()은 재귀 적 메서드 및 루프입니다. 우리는 타임 라인 애니메이션을 사용하여 애니메이션을 매끄럽게 만들었습니다. 그렇지 않으면 루프에 사용했다면 애니메이션이 깜박 거립니다. JavaFx API에서 사용 된 애니메이션 전문가가 아니기 때문에 거의 월간 작업에 성공했습니다.

내가 한 애니메이션은 연속적으로 실행할 때 애니메이션 효과를주는 일련의 이미지가있는 애니메이션 GIF와 비슷합니다.

코드는 여전히 약간의 작업이 필요합니다. doAnimation() 메서드에서 KeyFrame 배열을 만들었습니다 : KeyFrame [] kf = new KeyFrame [images.length]; 나는 그것이 필요하지 않다고 느낀다.

또한 그룹 클래스를 사용할 필요가 없습니다. 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); 
    } 

} 
관련 문제