2017-12-14 4 views
0

안녕하세요.Java FX DB에서 검색 한 이미지의 원을 그리는 방법

내가 클래스라는 visitantes에서 ID로 이미지를 가져 오는 코드가 있습니다

public Image getImageById(String id) throws SQLException, IOException { 
      try (
        ConexionSQL cn = new ConexionSQL(); 
        Connection con = cn.conexion(); 
       PreparedStatement ps = con.prepareStatement(
        "SELECT foto_visi FROM visitantes WHERE cedula_visi = ?"); 
      ) { 
       ps.setString(1, id); 
       ResultSet results = ps.executeQuery(); 
       Image img = null ; 
       if (results.next()) { 
        Blob foto = results.getBlob("foto_visi"); 
        InputStream is = foto.getBinaryStream(); 
        img = new Image(is) ; // false = no background loading 
        is.close(); 
       } 
       results.close(); 
       return img ; 
      } catch (Throwable e) { 
       String info = e.getMessage(); 
       System.out.println(info); 
      } 
      return null; 

을 그리고 이것은 클래스라는 ver_visitantes의 서비스 :

private final Service<Image> imageRetrievalService = new Service<Image>() {// cargar imagen en visitantes 
    @Override 
    protected Task<Image> createTask() { 
     final String id; 
     final visitantes visitante = tbvisitantes.getSelectionModel().getSelectedItem(); 
     if (visitante == null) { 
      id = null; 
     } else { 
      id = visitante.getcedula(); 
     } 
     return new Task<Image>() { 
      @Override 
      protected Image call() throws Exception { 
       if (id == null) { 
        return null; 
       } 
       return visitante.getImageById(id); 
      } 
     }; 
    } 
}; 

와 내가 보여 이 방법으로 이미지 :

imgfotovisi.imageProperty().bind(imageRetrievalService.valueProperty()); 

이 시점에서 작동하지만 문제가 없습니다.

하지만 지금은 은퇴 한 이미지가 원 모양이나 CSS로 필요합니다. 어떻게 그 이미지를 테두리 둘레에 원으로 만들 수 있습니까 ?? 메신저 이런 식으로 triyin :

Image im = imgfotovisi.getImage(); 
    circulo.setFill(new ImagePattern(im)); 

을하지만 이미지 변수가 null.circulo 것은 내가이 How to set an image in a circle와 helpme을 읽을

... 58 more 
Caused by: java.lang.NullPointerException: Image must be non-null. 
    at javafx.scene.paint.ImagePattern.<init>(ImagePattern.java:235) 

.I이

circulo.setFill(imgfotovisi); 

같은 몇 가지를 필요로 원 모양입니다 조금 있지만, 예를 들어 이미지가 웹에 있습니다.이 게시물의 다른 글 JavaFX Circle and ImageView 나는 undestand 코드를 캔트 수 없습니다.

나는 내가 완벽 할 것입니다 CSS에서의 가망 circle.If 전체를 원하는, CSS의 모서리 라운드

.image-view { 
    -fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0); 
    -fx-stroke-width: 2.0; 
    -fx-border-radius: 20 20 20 20; 
    -fx-background-radius: 10 10 10 10; 
    -fx-padding: 10; 
    -fx-background-color: firebrick; 
} 

는이를 가지고 있지만. 모든 문제를 이해하고 나를 도울 수 있기를 바랍니다. DB에서 보여지는 이미지에 대한 내 원래 게시물입니다.

+0

는') (당신이 당신이'imgfotovisi.getImage를 호출되어 있습니까 'Task '이 끝나면? –

+0

@mrmcwolf 나는 pr, oblem이 정확히 –

+0

인 것처럼 생각한다. imageview에서 이미지를 볼 수는 있지만, 작업이 완료되면 정확히 알지 못한다. –

답변

1

이것은 CSS 접근 방식이 아니지만 CircleImageView과 함께 StackPane을 사용할 수 있습니다.

순수 코드 접근

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.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication66 extends Application 
{ 

    @Override 
    public void start(Stage primaryStage) 
    { 
     Circle background = new Circle(); 
     background.setRadius(50); 
     background.setFill(Color.BLUE); 

     Image image = new Image(getClass().getResourceAsStream("chevron-6.png")); 
     ImageView imageView = new ImageView(image); 
     imageView.setFitHeight(50); 
     imageView.setFitWidth(50); 

     StackPane root = new StackPane(); 
     root.getChildren().addAll(background, imageView); 

     Scene scene = new Scene(root, 300, 250); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

} 

FXML 접근

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.StackPane?> 
<?import javafx.scene.shape.Circle?> 


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Circle fill="DODGERBLUE" radius="100.0" stroke="BLACK" strokeType="INSIDE" /> 
     <ImageView fitHeight="75.0" fitWidth="78.0" pickOnBounds="true" preserveRatio="true"> 
     <image> 
      <Image url="@../Pictures/arrows/chevron-6.png" /> 
     </image> 
     </ImageView> 
    </children> 
</StackPane> 

enter image description here

+0

제가 장면 작성자와 함께 넣은 이미지로 할 수는 있지만, 검색된 이미지가 그럴 가능성이 없습니다.이미지 뷰에서 검색된 이미지를 볼 수 있지만 원으로 가져갈 수 없습니다. 메서드를 사용해 보겠습니다. –

+0

순수한 코드를 사용하여이를 수행하려고하면 올바르게 작동하지 않는다고 말하고 있습니까? 그렇다면 먼저 'Circle'이 추가 된 StackPane이 있어야하고 ImageView가 추가되어야합니다. – Sedrick

+0

https://imgur.com/a/IZaf5 일부 결과가 있습니다. if im = imgdefault.getImage(); circulo.setFill (새 ImagePattern (im)); , 작동, 버스 imgfotovisi 노력하지 않는 경우 –

관련 문제