Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a program I am working on and it is meant to open a ppm file from file explorer and display it into a

I have a program I am working on and it is meant to open a ppm file from file explorer and display it into a window for editing in javaFX. I am able to open other file types such as JPEG, PNG, etc... but in order to display a ppm file i need to make a pixel reader/ writer. I currently have the reader printing the pixels but I cannot get the image to display in my window. My code calls from a interface and extends application. My question is how do I get my ppm file to display on my window. Thanks in advance!

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.*; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.FileChooser; import javafx.stage.Stage; import java.io.File; import java.io.FileNotFoundException; public class Program3 extends Application implements Program3Interface { public static void main(String[] args) { launch(args); } private ImageView myImageView; private Pane pane; @Override public void start(Stage primaryStage) { //Stage primaryStage.setTitle("stage"); BorderPane pane = new BorderPane(); Scene scene = new Scene(pane); Button load = new Button("Load"); load.setOnAction(loadEventListener); myImageView = new ImageView(); myImageView.setFitWidth(640); myImageView.setFitHeight(480); HBox rootBox = new HBox(); rootBox.getChildren().addAll(load, myImageView); 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.setTop( toolbarArea ); } EventHandler loadEventListener = (ActionEvent 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.ExtensionFilter extFilterTXT = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.TXT"); FileChooser.ExtensionFilter extFilterPPM = new FileChooser.ExtensionFilter("PPM files (*.ppm)", "*.PPM"); fileChooser.getExtensionFilters().addAll(extFilterPPM); //Show open file dialog File file = fileChooser.showOpenDialog(null); if (file != null) { Image image = new Image(file.toURI().toString()); PixelReader rdr = image.getPixelReader(); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { Color c = rdr.getColor(x, y); System.out.printf("(%3d, %3d, %3d) ", (int) (c.getRed() * 255), (int) (c.getGreen() * 255), (int) (c.getBlue() * 255)); } System.out.println(); } myImageView.setImage(image); Label label = new Label(); label.setGraphic(myImageView); } }; @Override public WritableImage loadImage(String filename) throws FileNotFoundException { ImageView view = new ImageView(); return null; } @Override public void saveImage(String filename, WritableImage image) throws FileNotFoundException { } @Override public WritableImage invertImage(WritableImage image) { return null; } @Override public WritableImage grayifyImage(WritableImage image) { return null; } @Override public WritableImage pixelateImage(WritableImage image) { return null; } @Override public WritableImage flipImage(WritableImage image) { return null; } private class JavaFXPixel { } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions

Question

What is Constitution, Political System and Public Policy? In India

Answered: 1 week ago

Question

What is Environment and Ecology? Explain with examples

Answered: 1 week ago