2016-03-08 74 views
2

我想使用JavaFX的创建每次显示图像 一个时间 一定的时间,我无法将图像获取循环或显示JavaFX的帧图像

的VBOX显示一个窗口空手而归

这是我在获得VBOX孩子最后一次尝试添加到VBOX

任何的想法?我一直停留在此的

package digitalframe; 

import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.stage.Stage; 
import java.util.*; 
import javafx.scene.image.Image; 
import javafx.util.Duration; 
import java.util.ArrayList; 
import javafx.scene.image.ImageView; 

import javafx.scene.layout.VBox; 



public class DigitalFrame extends Application { 

@Override 
public void start(Stage stage) throws Exception { 

    final ArrayList imageList = new ArrayList<>(); 

    imageList.add(new ImageView("1.jpg")); 
    imageList.add(new ImageView("2.jpg")); 
    imageList.add(new ImageView("3.jpg")); 

    // Create the label and align its contents 
    final VBox vBox = new VBox(); 

    vBox.setAlignment(Pos.CENTER); 

    // This is the keyframe handler. 
    class ImageHandler implements EventHandler<ActionEvent> { 

     @Override 
     public void handle(ActionEvent event) { 

      for (int n = 0; n > imageList.size(); n++) { 

       vBox.getChildren().add(new ImageView()); 

      } 

     } 

    } 

    // Build the keyframe. 
    Duration sec = new Duration(1000); 

    ImageHandler image = new ImageHandler(); 

    KeyFrame keyFrame = new KeyFrame(sec, image); 

    // Build the time line animation. 
    Timeline timeline = new Timeline(keyFrame); 
    timeline.setCycleCount(15); 

    // Set the stage and show, and play the animation 
    stage.setScene(new Scene(vBox, 250, 300)); 
    stage.setTitle("Animation Counter"); 
    stage.show(); 
    timeline.playFromStart(); 
} 

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

} 
+0

您是否曾将ImageView与您加载的图像关联? – matt

回答

1

首先,你需要ImageViewImage区分小时。 ImageView是显示一个Image场景图形节点,Image是实际图像(由ImageView示出)。 ImageView是场景图的一部分,并显示它当前包含的图像。

因此,而不是创建多个ImageView对象,只为你的图像创建一个,并创建Image对象:

... 
final List<Image> imageList = new ArrayList<>(); 

// create Image objects for each image 
imageList.add(new Image("1.jpg")); 
imageList.add(new Image("2.jpg")); 
imageList.add(new Image("3.jpg")); 

// Create the label and align its contents 
final VBox vBox = new VBox(); 

vBox.setAlignment(Pos.CENTER); 
ImageView imageView = new ImageView(); // create only one ImageView 
vBox.getChildren().add(imageView);  // and add it to the scene graph 
... 

然后,在你的处理器,每个处理器被调用时设定的下一张图片:

@Override 
public void handle(ActionEvent event) { 

    imageView.setImage(imageList.get(idx++)); 
    if (idx >= imageList.size()) { 
     idx = 0; 
    } 
} 

由于每个帧都要调用处理程序,所以您不得遍历处理程序中的所有图像 - 然后,对于每个帧,您都可以显示下一个图像。需要注意的是idx变量必须声明为字段变量,因为你不能在一个封闭使用非final的局部变量:

... 
private int idx = 0; 
... 
+0

好的,谢谢你,我相信理解图像VS ImageView的,像点实际的文件和ImageView的使用图像引用的实际位置 – user2920627

0

在你的关键帧处理循环中,您创建一个新的ImageView要添加而不是使用列表中索引处的那个:

vBox.getChildren().add(imageList.get(n)); 

您可能也只想在当时显示一幅图像。为此,我建议在添加新图像之前使用StackPane而不是VBox或从vBox中删除所有的孩子。

至于通过图像循环,您可以循环在while循环,并增加计数器每次和使用n % imageList.size()来计算当前索引。

+0

再次感谢你,我会给这个尝试看看IR工作 – user2920627

+0

好,谢谢你们所有这一切工作,我改变了:imageList.add(新图像(getClass()。getResourceAsStream(“5.jpg”)..图像保持不显示,因为它期待一个URL [关键字:错误URL javafx imageview] – user2920627