Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created March 14, 2013 21:28
Show Gist options
  • Save jewelsea/5165446 to your computer and use it in GitHub Desktop.
Save jewelsea/5165446 to your computer and use it in GitHub Desktop.
Small JavaFX application for viewing png files.
import java.io.File;
import java.net.MalformedURLException;
import java.util.logging.*;
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class PngImageViewer extends Application {
@Override public void start(Stage primaryStage) {
final ImageView imageView = new ImageView();
Button imageChooser = createImageChooserButton(imageView);
ScrollPane scroll = makeScrollable(imageView);
VBox layout = new VBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(
imageChooser,
scroll
);
VBox.setVgrow(scroll, Priority.ALWAYS);
primaryStage.setScene(new Scene(layout, 800, 600));
primaryStage.show();
}
private Image choosePng() {
try {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
"PNG files (*.png)",
"*.png"
);
fileChooser.getExtensionFilters().add(extFilter);
File imageFile = fileChooser.showOpenDialog(null);
if (imageFile == null) {
return null;
}
String imageFileLoc = imageFile.toURI().toURL().toExternalForm();
return new Image(imageFileLoc);
} catch (MalformedURLException ex) {
Logger.getLogger(PngImageViewer.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
private ScrollPane makeScrollable(final ImageView imageView) {
final ScrollPane scroll = new ScrollPane();
final StackPane centeredImageView = new StackPane();
centeredImageView.getChildren().add(imageView);
scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>() {
@Override public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds) {
centeredImageView.setPrefSize(
Math.max(imageView.prefWidth(bounds.getHeight()), bounds.getWidth()),
Math.max(imageView.prefHeight(bounds.getWidth()), bounds.getHeight())
);
}
});
scroll.setContent(centeredImageView);
return scroll;
}
private Button createImageChooserButton(final ImageView imageView) {
Button imageChooser = new Button("Choose Image");
imageChooser.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent t) {
Image chosenImage = choosePng();
if (chosenImage != null) {
imageView.setImage(chosenImage);
}
}
});
return imageChooser;
}
public static void main(String[] args) { Application.launch(args); }
}
@jewelsea
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment