Question: You are to develop a GUI-based application that loads, displays, modifies, and saves images. Your main class must be called Program3 . Your Program2 class

You are to develop a GUI-based application that loads, displays, modifies, and saves images.

Your main class must be called Program3. Your Program2 class must implement the Program3Interface. (

// BE SURE TO COMMENT YOUR CODE

import javafx.scene.image.WritableImage;

import javafx.scene.image.WritableImage;

import java.io.FileNotFoundException;

/**

* Public API for Program3.

*

* You must implement this interface in your Program3 class.

*

*/

public interface Program3Interface {

/**

* Load the specified PPM image file.

* The image file must be in the PPM P3 format

* @link http://netpbm.sourceforge.net/doc/ppm.html

*

* Don't forget to add a load button to the application!

*

* @param filename

* @return WritableImage

* @throws FileNotFoundException

*/

public WritableImage loadImage(String filename) throws

FileNotFoundException;

/**

* Save the specified image to a PPM file.

* The image file must be in the PPM P3 format

* @link http://netpbm.sourceforge.net/doc/ppm.html

*

* Don't forget to add a save button to the application!

*

* @param filename

* @param image

* @throws FileNotFoundException

*/

public void saveImage(String filename, WritableImage image) throws

FileNotFoundException;

/**

* Invert an image by subtracting each RGB component from its max

value

*

* For example:

* rbg( 255, 255, 255 ) -- invert --> rbg( 0, 0, 0 )

* rbg( 0, 0, 0 ) -- invert --> rbg( 255, 255, 255 )

* rbg( 255, 110, 63 ) -- invert --> rbg( 0, 145, 192 )

* rbg( 0, 145, 192 ) -- invert --> rbg( 255, 110, 63 )

*

* @param image - the image to be inverted, do not modify!

* @return a new inverted image

*/

public WritableImage invertImage(WritableImage image);

/**

* Convert an image to grayscale using the following formula:

* intensity = 0.2989*red + 0.5870*green + 0.1140*blue

* new rgb( intensity, intensity, intensity );

*

* For example:

* rbg( 0, 255, 255 ) -- grayify --> rbg( 178, 178, 178 )

* rbg( 255, 0, 255 ) -- grayify --> rbg( 105, 105, 105 )

* rbg( 255, 255, 0 ) -- grayify --> rbg( 225, 225, 225 )

* rbg( 21, 11, 11 ) -- grayify --> rbg( 13, 13, 13 )

*

* @param image - the image to be converted to grayscale, do not

modify!

* @return a new image that displays in shades of gray

*/

public WritableImage grayifyImage(WritableImage image);

/**

* Pixelates the image by dividing it into 5x5 regions, then

assigning

* all pixels in the region the same color as the central pixel.

*

* For example:

* [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0]

* [0,0,0] [5,5,5] [5,5,5] [5,5,5] [0,0,0]

* [0,0,0] [5,5,5] [1,2,3] [5,5,5] [0,0,0]

* [0,0,0] [5,5,5] [5,5,5] [5,5,5] [0,0,0]

* [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0]

*

* is pixelated to

*

* [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3]

* [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3]

* [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3]

* [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3]

* [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3]

*

* @param image - the image to be converted to grayscale, do not

modify!

* @return a new image that displays in shades of gray

*/

public WritableImage pixelateImage(WritableImage image);

/**

* Flips the image vertically.

*

* For example:

* [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0]

* [0,0,0] [5,5,5] [1,4,7] [5,5,5] [0,0,0]

* [0,0,0] [1,2,3] [2,5,8] [1,2,3] [0,0,0]

* [0,0,0] [4,4,4] [3,6,9] [4,4,4] [0,0,0]

* [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0]

*

* is flipped to

*

* [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0]

* [0,0,0] [4,4,4] [3,6,9] [4,4,4] [0,0,0]

* [0,0,0] [1,2,3] [2,5,8] [1,2,3] [0,0,0]

* [0,0,0] [5,5,5] [1,4,7] [5,5,5] [0,0,0]

* [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0]

*

* @param image - the image to be flipped, do not modify!

* @return a new image that displays upside-down (but not rotated!)

*/

public WritableImage flipImage(WritableImage image);

} )

You may use and develop other classes as required.

Your program should gracefully recover from runtime errors. Do not use console input or output. No error messages or stack traces should show up in the console.

Problem 1: Write a method to load images in PPM P3 format.

Given a filename, load a PPM P3 formatted file into a WritableImage.

Use the following method header:

 /** * Load the specified PPM image file. * The image file must be in the PPM P3 format * @see http://netpbm.sourceforge.net/doc/ppm.html * * Don't forget to add a load button to the application! * * @param filename * @return WritableImage * @throws FileNotFoundException */ public WritableImage loadImage( String filename ) throws FileNotFoundException { // YOUR CODE HERE } 

Problem 2: Write a method to save images in PPM P3 format.

Given a filename and a WritableImage, save the image in a PPM P3 formatted file.

Use the following method header:

 /** * Save the specified image to a PPM file. * The image file must be in the PPM P3 format * @see http://netpbm.sourceforge.net/doc/ppm.html * * Don't forget to add a save button to the application! * * @param filename * @param WritableImage * @throws FileNotFoundException */ public void saveImage( String filename, WritableImage image ) throws FileNotFoundException { // YOUR CODE HERE } 

Problem 3: Flip an Image.

Given a WritableImage, create a new Image that is flipped vertically, such that the top and bottom rows are swapped, the second row and the second-last row are swapped, etc.

The code for this might look like the following:

 /** * Flips the image vertically. * * For example: * [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] * [0,0,0] [5,5,5] [1,4,7] [5,5,5] [0,0,0] * [0,0,0] [1,2,3] [2,5,8] [1,2,3] [0,0,0] * [0,0,0] [4,4,4] [3,6,9] [4,4,4] [0,0,0] * [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] * * is flipped to * * [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] * [0,0,0] [4,4,4] [3,6,9] [4,4,4] [0,0,0] * [0,0,0] [1,2,3] [2,5,8] [1,2,3] [0,0,0] * [0,0,0] [5,5,5] [1,4,7] [5,5,5] [0,0,0] * [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] * * @param image - the image to be flipped, do not modify! * @return a new image that displays upside-down (but not rotated!) */ public WritableImage flipImage(WritableImage image) { PixelReader pxlrdr = image.getPixelReader(); WritableImage newImg = new WritableImage((int) image.getWidth(), (int) image.getHeight()); PixelWriter pxlwtr = newImg.getPixelWriter(); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { pxlwtr.setColor(x, y, pxlrdr.getColor(x, ((int) image.getHeight()) - 1 - y)); } } return newImg; } 

Problem 4: Invert an Image.

Given a WritableImage, create a new Image that is its inverse.

Invert an image by subtracting each RGB component from its max value

For example:

rbg( 255, 255, 255 ) -- invert --> rbg( 0, 0, 0 )

rbg( 0, 0, 0 ) -- invert --> rbg( 255, 255, 255 )

rbg( 255, 110, 63 ) -- invert --> rbg( 0, 145, 192 )

rbg( 0, 145, 192 ) -- invert --> rbg( 255, 110, 63 )

Use the following method header:

 /** * Invert an image by subtracting each RGB component from its max value * * For example: * rbg( 255, 255, 255 ) -- invert --> rbg( 0, 0, 0 ) * rbg( 0, 0, 0 ) -- invert --> rbg( 255, 255, 255 ) * rbg( 255, 110, 63 ) -- invert --> rbg( 0, 145, 192 ) * rbg( 0, 145, 192 ) -- invert --> rbg( 255, 110, 63 ) * * @param image - the image to be inverted, do not modify! * @return a new inverted image */ public WritableImage invertImage( WritableImage image ) { // YOUR CODE HERE } 

Problem 5: Convert an Image to grayscale.

Given an WritableImage, create a new WritableImage that is the grayscale equivalent.

Convert an image grayscale using the following method.

Calculate the luminescence of a pixel using the following NTSC formula:

luminescence = 0.2989*red + 0.5870*green + 0.1140*blue

Set the grayscale color of the pixel to rgb( luminescence, luminescence, luminescence )

Note: Here red, green, and blue are in the range 0 to 255, inclusive.

For example:

rbg( 0, 255, 255 ) -- grayify --> rbg( 178, 178, 178 )

rbg( 255, 0, 255 ) -- grayify --> rbg( 105, 105, 105 )

rbg( 255, 255, 0 ) -- grayify --> rbg( 225, 225, 225 )

rbg( 21, 11, 11 ) -- grayify --> rbg( 13, 13, 13 )

Use the following method header:

 /** * Convert an image to grayscale using the following formula: * intensity = 0.2989*red + 0.5870*green + 0.1140*blue * new rgb( intensity, intensity, intensity ); * * For example: * rbg( 0, 255, 255 ) -- grayify --> rbg( 178, 178, 178 ) * rbg( 255, 0, 255 ) -- grayify --> rbg( 105, 105, 105 ) * rbg( 255, 255, 0 ) -- grayify --> rbg( 225, 225, 225 ) * rbg( 21, 11, 11 ) -- grayify --> rbg( 13, 13, 13 ) * * @param image - the image to be converted to grayscale, do not modify! * @return a new image that displays in shades of gray */ public WritableImage grayifyImage( WritableImage image ) { // YOUR CODE HERE } 

Problem 6: pixelate an Image.

Given an WritableImage, create a new WritableImage that is the pixelated equivalent.

Pixelate the image by dividing it into 5x5 regions, then assigning all pixels in the region the same color as the central pixel.

For example, the following 5x5 image:

[0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] 
[0,0,0] [5,5,5] [5,5,5] [5,5,5] [0,0,0] 
[0,0,0] [5,5,5] [1,2,3] [5,5,5] [0,0,0] 
[0,0,0] [5,5,5] [5,5,5] [5,5,5] [0,0,0] 
[0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] 

is pixelated to

[1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] 
[1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] 
[1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] 
[1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] 
[1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] 

Use the following method header:

 /** * Pixelates the image by dividing it into 5x5 regions, then assigning * all pixels in the region the same color as the central pixel. * * For example: * [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] * [0,0,0] [5,5,5] [5,5,5] [5,5,5] [0,0,0] * [0,0,0] [5,5,5] [1,2,3] [5,5,5] [0,0,0] * [0,0,0] [5,5,5] [5,5,5] [5,5,5] [0,0,0] * [0,0,0] [0,0,0] [0,0,0] [0,0,0] [0,0,0] * * is pixelated to * * [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] * [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] * [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] * [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] * [1,2,3] [1,2,3] [1,2,3] [1,2,3] [1,2,3] * * @param image - the image to be converted to grayscale, do not modify! * @return a new image that displays in shades of gray */ public WritableImage pixelateImage( WritableImage image ) { // YOUR CODE HERE } 

Problem 7: Wire it all up.

Your Program3 class should extend Application.

You will need to:

Create a Scene (Links to an external site.)Links to an external site. for your primary stage.

Use a BorderPane (Links to an external site.)Links to an external site. as the root display in the Scene.

Add a Label (Links to an external site.)Links to an external site. to the CENTER of the BorderPane.

Set the Label graphic to be an ImageView (Links to an external site.)Links to an external site..

You can set the image of ImageView to be a WritableImage (Links to an external site.)Links to an external site..

Add an HBox (Links to an external site.)Links to an external site. for the buttons to the SOUTH of the BorderPane. The HBox should center its contents, pad its contents with insets of 10 pixels, and separate its contents by 10 pixels.

Make and add Button (Links to an external site.)Links to an external site.s (load, save, flip, invert, grayscale, and pixelate) to the HBox.

Don't forget to create button event handlers.

 // Create and register the button event handler for the flip button // Something like... Button flip = new Button("Flip"); flip.setOnAction(event -> { view.setImage(flipImage((WritableImage) view.getImage())); label.setText(""); label.setGraphic(view); }); 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!