this drawing area to draw image with canvas
Canvas canvas= new Canvas();
canvas.setHeight(500);
canvas.setWidth(700);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.WHITE);
gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);
and I made this method to export drawing canvas to imagem.pgn or snapshot and its correctly
public void snapshotCanvasImageToPNG(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
WritableImage writableImage = new WritableImage((int) canvas.getWidth(), (int) canvas.getHeight());
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(renderedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and I have many doubts in making these methods to open edit and save the canvas drawing to file.pgm
I made this but it complicate to finish
public void openFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
//doubt here
}
}
public void saveFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
//doubt here
}
}
The only way you will get to work with PGM images in JavaFX is through ImageJ or Java Advanced Imaging as mentioned by #VGR or another third party plugin.
Please check this plugin that contains code for such IO functions.
And you might also want to look at this answer which can show you the formats you can easily handle with JavaFX.
Related
My goal is to display images that are selected from a filechooser activated by button, and add those images to my gridpane. I am able to get the right URL file path name from the file chooser in order to make a correct imageview however when doing so my gridpane does not show any images being added..
public void makeBrowseButton(Stage primaryStage) {
//attach handler
browseButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser(); // create object
fileChooser.getExtensionFilters()
.addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif")); //filter for music files
//FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
if (!parentPath.equalsIgnoreCase(
"")) { //go to previous directory if exists
File parentPathFile = new File(parentPath);
fileChooser.setInitialDirectory(parentPathFile);
}
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) { // display the dialog box
String wholePath = selectedFile.getPath();
String name = selectedFile.getName();
String megaPath = selectedFile.getAbsolutePath();
String megaUrl;
try {
megaUrl = Paths.get(megaPath).toUri().toURL().toString();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
parentPath = selectedFile.getParent();
System.out.println("wholePath: " + wholePath);
System.out.println("parent: " + parentPath);
System.out.println("File Name: " + name);
System.out.println("megaPath: " + megaUrl);
//System.out.println("Canonical: " + Canonical);
Image newAwesomeImage = new Image(megaUrl);
paneofgridmonkeys.add(new ImageView(newAwesomeImage), 0, 0);
//ImageView view = new ImageView();
//view.setImage(newAwesomeImage);
//paneofgridmonkeys.add(view, 1, 1);
//paneofgridmonkeys.setConstraints(view, 0, 4);
//paneofgridmonkeys.add(new Label("Changed the image!"), 0, 1);
createDisplay(primaryStage);
}
}
});
}
I've tried multiple ways of insterting an images and these are the filepaths im getting:
wholePath: \\jupiter\yr1005\Desktop\20190111_1340501.jpg
parent: \\jupiter\yr1005\Desktop
File Name: 20190111_1340501.jpg
megaPath: file://jupiter/yr1005/Desktop/20190111_1340501.jpg
(im using the megapath)
basically when i choose an image from filechooser I get no error but no image is shown after selection. I just get all the print statements in return.. an idea why?
This is my create Display method:
public void createDisplay(Stage primaryStage) {
primaryStage.setTitle(this.MONKEY_TITLE);
GridPane paneofgridmonkeys = new GridPane();
paneofgridmonkeys.setAlignment(Pos.CENTER);
paneofgridmonkeys.setVgap(10);
paneofgridmonkeys.add(browseButton, 10, 10);
ScrollPane allTehFaces = new ScrollPane(paneofgridmonkeys);
allTehFaces.setFitToWidth(true);
primaryStage.setScene(new Scene(allTehFaces, 500, 500));primaryStage.show();
}
}
You're problem is in the createDisplay method; specifically this line:
GridPane paneofgridmonkeys = new GridPane();
Here you're creating a locally-scoped GridPane called paneofgridmonkeys, which must be the name of another class-level variable called paneofgridmonkeys, since it's available to makeBrowseButton. When you do this in local scope, the new instance you've created becomes the one that's used inside of that method, rather than the class-level instance; thus the class-level one isn't the one being added to your scene, and you're not seeing the changes.
1.I have a code that opens a pane and I can open file explorer, but I do not know how to open said image when I select it from file explorer. Also this code is meant to be used in different #Overrride statements but I have only managed to get this far using one statement. Is there a way for me to call some of these events from another Override statement?
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
ImageView myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
EventHandler<ActionEvent> loadEventListener
= t -> {
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);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
ImageView myImageView = new ImageView();
myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
}
};
Since you already have an ImageView and have shown it in the scene, simply set its image to the image you load.
For this you need to make the ImageView an instance variable:
private ImageView myImageView ;
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
// presumably you intended this somewhere?
pane.setCenter(rootBox);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
Note there is no need to load a buffered image and then convert it. So all you need in the event handler is
loadEventListener = t -> {
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);
if (file != null) {
Image image = new Image(file.toURI().toString());
myImageView.setImage(image);
}
};
I have a MediaView on the scene in which I am trying to play the selected video. The video is playing, I can hear the sound, but it's not visible.
here's my code:
playLocalVideo.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File fileToPlay = null;
//DirectoryChooser directoryChooser = new DirectoryChooser();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Files to Upload");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("VIDEO files (*.mp4)", "*.mp4");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
try {
fileToPlay = fileChooser.showOpenDialog(stage).getCanonicalFile();
Media m = new Media(fileToPlay.toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
videoPlayer = new MediaView(mp);
videoPlayer.setPreserveRatio(true);
mp.play();
} catch (IOException ex) {
Logger.getLogger(TutorControlPanelController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Sorted:
instead of:
videoPlayer = new MediaView(mp);
You should do:
videoPlayer.setMediaPlayer(mp);
I had the same issue that audio is playing but no video is playing when MP4 file used code MPEG-4. After I changed the codec to H.264 the video became playing correct.
So, I have a program that loads an image with a FileChooser. I use a lambda expression to define this loading action so the function LoadImage() returns the chosen image inside the event block but I can't use it outside this block.
Can someone help me?
Here is my code so far:
public class Main extends Application{
Stage window;
BorderPane layout;
ImageView imageView;
Image currentImage;
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
window.setTitle("Image Viewer v.1.0");
//MENU
Menu fileMenu = new Menu("_File");
MenuItem openFile = new MenuItem("Open file...");
openFile.setOnAction(e -> currentImage = LoadImage());
fileMenu.getItems().add(openFile);
//Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
imageView = new ImageView();
imageView.setImage(currentImage); //here the currentImage is null
layout = new BorderPane();
layout.setTop(menuBar);
layout.setCenter(imageView);
Scene scene = new Scene(layout, 900, 600);
window.setScene(scene);
window.show();
}
public Image LoadImage()
{
final Image image;
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg = new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng = new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
return new Image(file.toURI().toString());
}
} // end of Main class
So forth, nothing is displayed in the ImageView.
How can I use the value returned by the LoadImage() method?
The value of currentImage is indeed changed, but when you set the value of imageView's image property, it is not yet set. You have to change the property of your ImageView, not the variable (which you use only before setting the image).
Try changing your lambda to:
openFile.setOnAction(e -> imageView.setImage(LoadImage()) )
i would like to add a frame to a picture using Java and Javafx and then save the framed picture. What would be the best way to do that?
For example say I have a photo of a landscape and want to add a frame to it. The framed photo should look like this:
You could add two images, first the frame, then the image, to the same canvas like this:
GraphicsContext gc1 = canvas.getGraphicsContext2D();
gc1.drawImage(frameimage,0,0,image.getFitWidth()+20,image.getFitHeight()+20);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(i,10,10,image.getFitWidth(),image.getFitHeight());
and then save them as png (or whatever format you like) using the canvas.snapshot function:
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter =new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
Stage primaryStage = (Stage) canvas.getScene().getWindow();
File file = fileChooser.showSaveDialog(primaryStage);
if(file != null){
try {
WritableImage writableImage = new WritableImage((int)canvas.getWidth(), (int)canvas.getHeight());
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
File file1 = new File(file.getAbsolutePath()+".png");
file.renameTo(file1);
ImageIO.write(renderedImage, "png", file1);
} catch (IOException ex) {
ex.printStackTrace();
}