I have this code for getting a image by an ID in a class called visitantes:
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;
And this is the service in the class called 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);
}
};
}
};
and i show the image this way:
imgfotovisi.imageProperty().bind(imageRetrievalService.valueProperty());
at this point is working , no problems.
But now I need the retrieved image into a circle shape or the css to do that. how can get that image into a circle o round the borders?? I'm trying like this:
Image im = imgfotovisi.getImage();
circulo.setFill(new ImagePattern(im));
But the Image variable is null.circulo is a circle shape
... 58 more
Caused by: java.lang.NullPointerException: Image must be non-null.
at javafx.scene.paint.ImagePattern.<init>(ImagePattern.java:235)
.I need some like
circulo.setFill(imgfotovisi);
I read this How to set an image in a circle and help me a little , but in that example the image is in the web. And in this other post JavaFX Circle and ImageView I can't understand the code.
I have this in the 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;
}
it round the corners but I want an entire circle. If it's possible in CSS, it will be perfect.
I hope you can understand all the problem and can help me.
This is my original post about show images from DB.