2016-07-25 2 views
3
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 


public class Testwebimage extends Application { 

    public void start(Stage primaryStage) { 

     Image image = new Image("http://i.imgur.com/hbjCOgg.jpg"); 
     ImageView imageView = new ImageView(); 
     imageView.setImage(image); 

     StackPane root = new StackPane(); 
     root.getChildren().add(imageView); 
     Scene scene = new Scene(root); 

     primaryStage.setScene(scene); 
     primaryStage.setMaximized(true); 
     primaryStage.show(); 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 

나는 URL에서 바로 이미지를 표시하는 프로그램을 만들려고 해요,하지만 난이 문제는 이미지가 완전히로드 될 때까지 대기하고 다음 의미를 표시하는 이미지 크기 경우 큰 경우 이미지를 표시하는 데 시간이 많이 걸리므로 짜증이납니다. 하지만 이미지로드가 열려 있으면 this과 같은 종류의 이미지가 표시됩니다.사진을 Java에서 다운로드가 완료되기를 기다리는 대신 지연로드하는 방법은 무엇입니까?

누군가 this과 같은 것을 달성하는 방법을 알고 있습니까?

+0

[어쩌면 이미지의 InputStream? (https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html#Image (java.io.InputStream의)) – Jeeter

답변

2

다음은 이미지를 비동기 적으로로드하는 예제입니다. 로컬 파일을 사용할지 원격 URL을 사용할지 여부를 선택할 수 있습니다.

package jfxfeatures.graphics.image.loading.async; 

import java.io.File; 
import java.net.MalformedURLException; 

import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class AsyncImageDemo extends Application { 

    @Override 
    public void start(Stage stage) { 
     String imgURL = null; 
     try { 
      final String remoteURL = "http://farm5.staticflickr.com/4129/4960490401_71a3d05d28_o_d.jpg"; 
      final String remoteURL2 = "http://www.spacetelescope.org/static/archives/posters/large/earth02.jpg"; 
      final String localURL = new File("data/earth02.jpg").toURI().toURL().toExternalForm(); 
      final String localFile = "/earth02.jpg"; 

      //=========================== 
      // Select local or remote image source. 
      imgURL = localFile; 
      //=========================== 
     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } 

     StackPane root = new StackPane(); 
     Scene scene = new Scene(root, 800, 800); 
     scene.setFill(Color.BLACK); 

     ImageView iv = new ImageView(); 
     iv.setPreserveRatio(true); 
     iv.fitHeightProperty().bind(root.heightProperty()); 
     iv.fitWidthProperty().bind(root.widthProperty()); 
     root.getChildren().add(iv); 

     stage.setTitle(getClass().getSimpleName()); 
     stage.setScene(scene); 
     stage.show(); 

     if (imgURL != null) { 
      Image image = new Image(imgURL, true); 
      image.progressProperty().addListener(new ChangeListener<Number>() { 
       @Override 
       public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { 
        System.out.println("Progress: " + Math.rint(newValue.doubleValue() * 100) + "%"); 
       } 
      }); 
      iv.setImage(image); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
관련 문제