2012-09-08 16 views
6

Estoy usando Java FX y me gustaría convertir un nodo en una imagen. Encontré este recurso, pero no resuelve mi problema ya que quiero convertir un nodo en una imagen, no en una escena completa.Cómo convertir un nodo a una imagen en javafx 2.1?

How to output the content of a Scene graph in JavaFx 2.0 to an Image

+0

No sé mucho sobre esto y no tengo tiempo para probar, pero ¿no podría simplemente crear una nueva escena y agregar el nodo que desea convertir en una imagen como la única niño y luego utilizar el método explicado en el enlace? Supongo que nunca tendrás que mostrar la segunda escena que crees. –

Respuesta

5

Esta es la solución a mi problema. Esta solución es la ayuda de Sergey y jewelsea. Esta solución está en javafx 2.2. Gracias Sergey y jewelsea.

public class TrySnapshot extends Application { 

javafx.embed.swing.SwingFXUtils fXUtils; 
BufferedImage bufferedImage = new BufferedImage(550, 400, BufferedImage.TYPE_INT_ARGB); 
File file = new File("C:/Users/PC1/Desktop/Sample Images/test.jpg"); 
VBox vbox = null; 

@Override 
public void start(Stage primaryStage) { 
    vbox = new VBox(); 
    Button btn = new Button(); 
    Image i = new Image("file:C:\\Koala.jpg"); 
    ImageView imageView = new ImageView(); 
    imageView.setImage(i); 
    vbox.getChildren().add(imageView); 
    vbox.setSpacing(10); 
    btn.setText("Say 'Hello World'"); 
    btn.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent event) { 
     // here we make image from vbox and add it to scene, can be repeated :) 
     WritableImage snapshot = vbox.snapshot(new SnapshotParameters(), null); 
      vbox.getChildren().add(new ImageView(snapshot)); 
      saveImage(snapshot); 
      System.out.println(vbox.getChildren().size()); 
     } 
    }); 


    Scene scene = new Scene(new Group(btn), 500, 400); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

private void saveImage(WritableImage snapshot) { 
    BufferedImage image; 
    image = javafx.embed.swing.SwingFXUtils.fromFXImage(snapshot, bufferedImage); 
    try { 
     Graphics2D gd = (Graphics2D) image.getGraphics(); 
     gd.translate(vbox.getWidth(), vbox.getHeight()); 
     ImageIO.write(image, "png", file); 
    } catch (IOException ex) { 
     Logger.getLogger(TrySnapshot.class.getName()).log(Level.SEVERE, null, ex); 
    }; 
    } 
} 
+0

Puede, por favor, marque esta respuesta como correcta y elimine "Este es mi código" para limpiar este tema. –

+0

bien, Sergey. Elimino "Este es mi código". Ahora este es un tema claro. –

+0

Genial. ¡Muchas gracias! –

11

Puede utilizar la nueva FX función 2.2 instantánea:

public class TrySnapshot extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     final VBox vbox = new VBox(2); 
     final Button btn = new Button(); 
     vbox.getChildren().add(btn); 
     btn.setText("Say 'Hello World'"); 
     btn.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent event) { 
       // here we make image from vbox and add it to scene, can be repeated :) 
       WritableImage snapshot = vbox.snapshot(new SnapshotParameters(), null); 

       vbox.getChildren().add(new ImageView(snapshot)); 
       System.out.println(vbox.getChildren().size()); 
      } 
     }); 

     Scene scene = new Scene(new Group(vbox), 300, 250); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

Si tiene que usar FX mayores por alguna razón sólo cambia la escena coordina a su nodo de coordenadas utilizando Node#getBoundsInParent llamadas en el código de muestra que ha vinculado.

+0

Gracias Sergey. Pero el método snaphot() no es la solución de mi problema. Quiero algo de imagen amortiguada. –

+2

Puede obtener una imagen impresionante con la solución de Sergey y [SwingFXUtils] (http://docs.oracle.com/javafx/2/api/javafx/embed/swing/SwingFXUtils.html#fromFXImage (javafx.scene.image. Imagen,% 20java.awt.image.BufferedImage)) – jewelsea

Cuestiones relacionadas