2017-06-20 95 views
0

我試圖一次編輯五個圖像(一次使用javafx運動模糊),而不是一個接一個地選擇它們。這是我的代碼,我不完全確定我做錯了什麼,但是當我運行它時,只有文件夾中的最後一個圖像被編輯。其他人保持原樣。JavaFX批量編輯圖像

public class IterateThrough extends Application { 

    private Desktop desktop = Desktop.getDesktop(); 


@Override 
public void start(Stage primaryStage) throws IOException { 

    File dir = new File("filepath"); 
    File [] directoryListing = dir.listFiles(); 


    if (directoryListing != null) { 

     for (File file : directoryListing) { 
      if(file.getName().toLowerCase().endsWith(".jpeg")){ 

       //desktop.open(file); 

       Image image = new Image(new FileInputStream(file)); 


       //Setting the image view 
       ImageView imageView = new ImageView(image); 

       //setting the fit height and width of the image view 
       imageView.setFitHeight(600); 
       imageView.setFitWidth(500); 

       //Setting the preserve ratio of the image view 
       imageView.setPreserveRatio(true); 

       // instantiate Motion Blur class 
       MotionBlur motionBlur = new MotionBlur(); 

       // set blur radius and blur angle 
       motionBlur.setRadius(15.0); 
       motionBlur.setAngle(110.0); 

       //set imageView effect 
       imageView.setEffect(motionBlur); 

       //Creating a Group object 
       Group root = new Group(imageView); 

       //Creating a scene object 
       Scene scene = new Scene(root, 600, 500); 

       //Setting title to the Stage 
       primaryStage.setTitle("Loading an image"); 

       //Adding scene to the stage 
       primaryStage.setScene(scene); 

       //Displaying the contents of the stage 
       primaryStage.show(); 
      } 
     } 
    } 
} 


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

} 
+0

我假設你正在使用的單詞「編輯」是指「顯示,」但我還是不明白你想要做什麼。你想顯示所有的圖像是在同一時間,在彼此之上?您是否試圖一次顯示它們,而將模糊用作過渡效果? – VGR

+0

@VGR我試圖一次一個地顯示它們,同時使用模糊作爲過渡效果。 – diamondgirl

+1

然後,您可能會想要使用[時間軸](http://docs.oracle.com/javase/8/javafx/api/javafx/animation/Timeline.html)。但首先,您需要更好地定義您希望過渡的樣子。僅僅模糊圖像並不構成過渡效果。你想讓一張圖像越來越模糊,然後被下一張圖像的模糊版本所取代,而這種模糊版本逐漸變得不明朗? – VGR

回答

0

第一,你不應該只是讓primaryStage秀()在for-each循環,因爲當primaryStage首秀(),在該指令的FX線程暫停,剩下的照片將不閱讀並關閉窗口時,循環將不會繼續繼續,它代表應用程序的結束。

所以最好讓primaryStage show()在讀完所有圖像後的每個循環之外。第二,你不應該使用「Group」容器來包含所有圖像,最好使用VBox/HBox/FlowPane,確保上面的圖像不會覆蓋下面的圖像。

這裏的修改代碼僅供參考。

public class IterateThrough2 extends Application{ 
    private Desktop desktop = Desktop.getDesktop(); 
    @Override 
    public void start(Stage primaryStage) throws IOException{ 
    File dir = new File("filepath"); 
    File[] directoryListing = dir.listFiles(); 

    FlowPane root = new FlowPane(); 

    if(directoryListing != null){ 
     for(File file : directoryListing){ 
     if(file.getName().toLowerCase().endsWith(".jpeg")){ 
      Image image = new Image(new FileInputStream(file)); 
      ImageView imageView = new ImageView(image); 
      imageView.setFitHeight(600); 
      imageView.setFitWidth(500); 
      imageView.setPreserveRatio(true); 
      MotionBlur motionBlur = new MotionBlur(); 
      motionBlur.setRadius(15.0); 
      motionBlur.setAngle(110.0); 
      imageView.setEffect(motionBlur); 
      root.getChildren().add(imageView); 
     } 
     } 
    } 

    Scene scene = new Scene(root, 600, 500); 
    primaryStage.setTitle("Loading an image"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
    } 

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

enter image description here

+0

它現在正在編輯&只顯示第一張圖片。 – diamondgirl

+0

@diamondgirl 你應該放大你的窗口,看看我上傳的圖像。 – yab

+0

非常感謝! – diamondgirl