2016-01-18 38 views
1

我打算通过在for循环中向时间轴对象添加关键帧来更改鸟类的图像。事实证明,只显示第一个图像。有人能指出我哪里错了吗?提前致谢。如何使用JavaFX中的时间线更改图像

此外,我注意到我必须在for循环后将计数器“index”重置为0,否则它会生成java.lang.ArrayIndexOutOfBoundsException。

包应用;

import javafx.animation.Animation; 
import javafx.animation.KeyFrame; 
import javafx.animation.KeyValue; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.stage.Stage; 
import javafx.util.Duration; 
import javafx.scene.Group; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.effect.BoxBlur; 
import javafx.scene.effect.DropShadow; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.StackPane; 

public class Main extends Application 

{

int index=0; 
@Override 
public void start(Stage primaryStage) { 
    try { 

     ImageView bgV = new ImageView(); 
     Image img_BG = new Image(Main.class.getResourceAsStream("background.png")); 
     bgV.setImage(img_BG); 
     bgV.setEffect(new BoxBlur()); 
     bgV.setOpacity(0.5); 

     ImageView t1V = new ImageView(); 
     Image img_t1 = new Image(Main.class.getResourceAsStream(
       "t1.png" 
       )); 
     t1V.setImage(img_t1); 

     ImageView t2V = new ImageView(); 
     Image img_t2 = new Image(Main.class.getResourceAsStream(
       "t2.png" 
       )); 
     t2V.setImage(img_t2); 

     ImageView t3V = new ImageView(); 
     Image img_t3 = new Image(Main.class.getResourceAsStream(
       "t3.png" 
       )); 
     t3V.setImage(img_t3); 

     Group foreground = new Group(t1V,t2V,t3V); 

     t1V.setTranslateX(20); 
     t1V.setTranslateY(200); 

     t2V.setTranslateX(300); 
     t2V.setTranslateY(200); 

     t3V.setTranslateX(550); 
     t3V.setTranslateY(200); 
     foreground.setEffect(new DropShadow()); 

     String[] 
       birdFiles = {"b1.png", "b2.png", "b3.png", "b4.png", "b5.png", "b6.png"}; 
     double[] ds = { 300,   600,    900,   1200, 1500, 1800}; 

     ImageView birdV = new ImageView(new Image(Main.class.getResourceAsStream(birdFiles[0]))); 
     Group birds = new Group(birdV); 
     birds.setTranslateX(img_BG.getWidth()-100); 

     Timeline timeline = new Timeline(); 
     timeline.setCycleCount(
       Animation.INDEFINITE 
       ); 


     KeyFrame[] kframs = new KeyFrame[birdFiles.length]; 

     for(index=0; index<birdFiles.length; index++) 
     { 
      EventHandler<ActionEvent> 
       onFishined = new EventHandler<ActionEvent>() 
      { 

       @Override 
       public void handle(ActionEvent arg0) 
       { 
        birds.getChildren().setAll(new ImageView(new Image(Main.class.getResourceAsStream(birdFiles[index])))); 

       } 

      }; 
      Duration duration = Duration.millis(ds[index]); 
      KeyFrame 
       kf = new KeyFrame(duration, onFishined,null,null ); 
      timeline.getKeyFrames().add(kf); 

     }//End for i 
     index = 0; 







     timeline.play(); 





     Group root = new Group(bgV,foreground,birds); 

     Scene scene = new Scene(root,img_BG.getWidth(), img_BG.getHeight()); 
     scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    launch(args); 
} 

}

+0

参见相关:【如何显示在JavaFX的图像的特定部分(http://stackoverflow.com/questions/23440980/how-show-specific-image-in-javafx)和[用JavaFX创建Sprite动画](http://blog.netopyr.com/2012/03/09/creating-a -sprite-animation-with-javafx /) – jewelsea

回答

1

你不必外声明的index - 场。这也会导致你的问题:无论何时调用handle方法,它都会引用你的字段:index,你在循环后将其设置为0。

为此你可以宣布一个新的领域作为final并把它传递到处理程序:

for (int index = 0; index < birdFiles.length; index++) { 
    final int birdIndex = index; 
    EventHandler<ActionEvent> onFishined = new EventHandler<ActionEvent>() { 

       @Override 
       public void handle(ActionEvent arg0) { 
        birds.getChildren().setAll(new ImageView(new Image(Main.class.getResourceAsStream(birdFiles[birdIndex])))); 
       } 

      }; 
    ... 
} 
+0

非常感谢,KnusperPudding,它确实解决了问题 – user1304846

相关问题