Print Stage with JavaFX 8 - javafx

I found some basic example how to print web page with JavaFX but I'm looking for examplewhich can print the complete Scene with app content. Is there any example?

You can convert your scene to an image using
WritableImage snapshot = scene.snapshot(null);
This will return a WritableImage, which can be converted to an Image File or BufferedImage, and print using the Printing API of JavaFX8 (there are not lot of examples available for this, but the new API has quite a resemblance to the old Printing API, so it won't be a problem)
Converting WritableImage to Png File
WritableImage snapshot = scene.snapshot(null);
File file = new File("image.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
Converting WritableImage to BufferedImage (used for printing)
WritableImage snapshot = scene.snapshot(null);
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(snapshot, null);
For just a small example of how to print an Image using java, please go through
Proper way of printing a BufferedImage in Java
How to print image in java

Related

save a number of images in one png file

I have a number of images that are displayed in my application. They are generated by the data I enter. Now I want to save the images in one png file (would prefer jpg but png is also doing the job). Right now the last image will be overwritten by the next image, so that only the last image in stored in the png file. I thought about creating one image that consists of all the images I want to save in the png file. Does anyone have an idea of how to do that? Or is there a simpler way of how to save the images in one file?
private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();
pngButton.setOnAction(e -> {
if (pdfFilePages.size() > 0) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(I18N.get("key.save_to_png"));
File file = fileChooser.showSaveDialog(dialogStage);
if (file != null) {
for (Image img : pdfFilePages) {
ImageView imgView = new ImageView(img);
try {
BufferedImage bi = SwingFXUtils.fromFXImage(img, null);
ImageIO.write(bi, "png", file);
} catch (IOException e1) {
LOGGER.error(e1.getMessage(), e1);
}
}
}
}
});

Load file from Sample File Directory named "images"

I have this simple structure
Now I should load 11.png to ImageView from URI.
How do I do this?
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.fromFile(new File("What should be here???")));
I think you Are New To Android?
Its Better To move the Image File to Drawable Folder and and Set Image View From the Drawable. You Can Use BatchDrawableImport Plugin in Android Studio to Import multiple Drawable Files
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.yourImageName);
Or Move the File to Assets Folder and Using Assets Manager You Can Achieve the Solution
AssetManager manager = getAssets();
// Read a Bitmap from Assets
try {
InputStream open = manager.open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
ImageView view = (ImageView) findViewById(R.id.ImageView01);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}

how to load image name from database and load it into my imageview

i have a project in which i have in my db images stored with their names only so i want to load those image with javafx
and i have no idea how to load thos images
public void initialize(URL url, ResourceBundle rb) {
// help me here to retreive my images names
}
and this is my button that allows me to change my cover picture
here is the code
changecover.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
coverpic.setImage(image);
Groupe g = gs.getGroupe(5);
g.setPhoto_couverture(coverpic.toString());
gs.update(g);
//myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(Groupe_homeController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Why load the image data to a BufferedImage and then convert this BufferedImage to a JavaFX Image when you can avoid storing the image data in memory twice by directly loading the data to a Image:
Image image = new Image(file.toURI().toURL().toExternalForm());
I'm not sure how a database is involved here though, since FileChooser allows you to choose the image from storage devices available to the client only, which means you'd need to implement some way of retrieving the data from the server... (unless the database is for use on the local machine only)

JavaFX8 presentation view (duplicate pane and content)

I'm trying to create a presentation view for my Drawing app, where only the drawing board (pane) is visible. So the user can show the presentation view on the projector while having the actual drawing pane and tools on the PC.
My current approach is to create a Snapshot from the pane in each frame and display it in an ImageView on the second stage.
public void startStream(){
new AnimationTimer() {
#Override
public void handle(long now) {
WritableImage image = drawingPane.snapshot(new SnapshotParameters(), null);
stream.setImage(image);
}
}.start();
final Screen screen = Screen.getPrimary();
final Stage stage = new Stage();
StackPane root = new StackPane();
root.getChildren().add(stream);
Scene scene = new Scene(root, drawingPane.getWidth(), drawingPane.getHeight());
stage.setScene(scene);
stage.setTitle("streaming stage");
Rectangle2D bounds = screen.getBounds();
System.out.println(bounds);
stage.setX(bounds.getMinX() + (bounds.getWidth() - 300) / 2);
stage.setY(bounds.getMinY() + (bounds.getHeight() - 200) / 2);
stage.show();
}
problem here is it's eating a lot of RAM, like 800MB and also 30% more CPU usage. I can understand, that creating an Image on every frame is not efficient, that's why I'd like to ask, if there is a way to get this working more efficiently.
Also if there is a better approach/solution to this feature, I'd be thankful for it.
To reduce the CPU usage, do not make a snapshot in an AnimationTimer—this really makes a snapshot at the frame rate (60 FPS) frequency, even if nothing has changed in the drawingPane. Instead, make the snapshot after drawingPane's layout. Adding a listener to drawingPane's needsLayoutProperty should do the trick:
drawingPane.needsLayoutProperty().addListener((obs, wasDirty, isDirty) -> {
if(!isDirty) {
WritableImage image = drawingPane.snapshot(new SnapshotParameters(), null);
stream.setImage(image);
}
});
This should lower the CPU usage when you are not editing the drawingPane.
It may also lower the memory usage, because garbage collector may kick in faster and collect the old snapshots. If not, consider re-using the WritableImage instance, if it already has the correct size:
WritableImage image = null;
drawingPane.needsLayoutProperty().addListener((obs, wasDirty, isDirty) -> {
if(!isDirty) {
if(this.image != null &&
(this.image.getWidth() != drawingPane.getWidth() ||
this.image.getHeight() != drawingPane.getHeight())) {
this.image = null;
}
this.image = drawingPane.snapshot(new SnapshotParameters(), this.image);
stream.setImage(this.image);
}
});
Note that this answer assumes that children of drawingPane are managed children (which is the default), and thus their layout being marked as dirty causes the drawingPane's layout being marked as dirty, so that you can actually observe any changes of the needsLayout property.

how open image with url in javafx

I have banner an I wont to put it in my javaFX application.
And when user click on the image open default browser.
try {
String path = "http://developer.am/webservice/banner728x90.gif";
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
label = new JLabel(new ImageIcon(image));
} catch (Exception exp) {
exp.printStackTrace();
}
also I am trying to convert above code from awt in JavaFX
Lets see. First the ingredients:
Image
Button
ImageView
Open Link in System Browser with JavaFX
Putting this together:
String path = "http://...";
String pathToOpen = "http://...";
Image image = new Image(path);
ImageView imageView = new ImageView(image);
Button button = new Button("clickMe!", imageView);
button.setOnAction(ev -> getHostServices().showDocument(pathToOpen));

Resources