For this project, you will write a program named ImageViewer that allows the use
ID: 3913326 • Letter: F
Question
For this project, you will write a program named ImageViewer that allows the user to choose an image from a list, which is displayed normally. The user is then given the option to see the same image displayed in grey scale, high contrast, or as a negative.
Images are made up of pixels, each of which has an RGB value, which specifies the amount of Red, Green, and Blue used to display the pixel. These range from 0 - 255.
Grey scale: An image is converted to grey scale by replacing the RGB values for each pixel with the average of the R, G, and B values. For example, if the RGB values for a pixel are 25, 75, 250, the grey scale RGB values would be 116, 116, 116 because (25 + 75 + 250) / 3 = 350 / 3 = 116. Note that this calculation should use integer division.
High contrast: An image is converted to high contrast by changing each color value less than 128 to 0 and each color values greater than or equal to 128 to 255. For example if the R, G, and B values of a pixel are 95, 128, and 240, respectively, converting to high contrast would result in R, G, and B values of 0, 255, and 255.
Negative: An image is converted to a negative by negating each color, that is, subtracting its value from 255. For example, if the R, G, and B values of a pixel are 95, 215, and 0, respectively, negating the values would result in R, G, and B values of 160, 40, and 255.
Information about the images is stored in a file. Each line of the file contains the following information:
Filename
Width
Height
Title
Each image shall be displayed surrounded by a border of 10 pixels. Thus, an image whose width is 225 and height is 330 shall be displayed in a DrawingPanel with a width of 245 and a height of 350. The upper left hand point of the image shall have coordinates (10, 10).
The file containing the data shall be specified as a command-line argument to the program.
If there is not exactly one argument on the command line, then the following usage message shall be displayed and the program will immediately exit.
If the file on the command line cannot be accessed, then following message shall be displayed and the program will immediately exit.
The program shall continually prompt the user for the image to display and then the new format in which to display it, until they desire to quit. Note that the list of images is populated based on the images in the file. The number used to quit will be one greater than the number of images in the file. The program shall accept and use both upper and lower case letters for the options. If the user enters an invalid image number, an invalid option, or if the file needed to display the image does not exist, the program shall output an error message and redisplay the menu beginning with the list of images. Below is an example of running the program without any user errors followed by an example which shows how to handle errors that may occur.
An example of an error handling situation:
Your program will create and use an array of ImageInfo class objects. The ImageInfo class is provided for you in the file, ImageInfo.java. See the ImageInfo API for information about the ImageInfo constructor and methods. Do not change this file, but use it as is. Place it in the same directory as your ImageViewer.javafile. You will also use the DrawingPanel.java file provided by the textbook - be sure to use this version, which contains support for image manipulation. You will need to compile all three files.
The program must contain and use the completed version of the following methods: getImageList(), getImageNumber(), displayImage(), convertToGreyScale(), convertToHighContrast(), and convertToNegative(). The method headers are listed below. You MUST use these exact method headers in your program so that we can automate the testing of your program.
//Create a Scanner for the given file and read through the file
//once counting the number of lines (images)
//Create an array of ImageInfo objects of the correct size
//Create another Scanner for the file and read through the file again
//reading the information about each image and storing it in the ImageInfo array
//Return the ImageInfo array
//If a FileNotFoundException occurs, throw an IllegalArgumentException
//with the message, "Unable to access input file: <filename>", for example,
//"Unable to access input file: junk.txt"
//The IllegalArgumentException should be caught by the calling method --
public static ImageInfo[] getImageList(String filename) { }
//Display the image menu and prompt the user for the Image, as shown above
//If the user enters a valid image number (number of images + 1),
//return the number
//If the user enters an invalid image number or something other than
//an integer, output an error message as shown above and return -1
//DO NOT reprompt the user for another image number
public static int getImageNumber(Scanner console, ImageInfo[] images) { }
//Display the image on a DrawingPanel
//Display the option menu and prompt the user for the option, as shown above
//Depending on the option, use one of the conversion methods to convert
//the pixels associated with the image to a different format
//Redisplay the converted image on the same DrawingPanel
//If the user enters an invalid option, output an error message as shown
//above, and return - DO NOT reprompt the user for another option
//If the image file does not exist, output an error message as shown above
//and return
public static void displayImage(Scanner console, ImageInfo image) { }
//The Color pixels in the array are converted to grey scale
//See the information above as to how this is done
public static void convertToGreyScale(Color[][] pixels) { }
//The Color pixels in the array are converted to high contrast
//See the information above as to how this is done
public static void convertToHighContrast(Color[][] pixels) { }
//The Color pixels in the array are negated
//See the information above as to how this is done
public static void convertToNegative(Color[][] pixels) { }
I have the list of images on my computer, but I am not sure how to attach them here. You can use any image from a directory, like "images/example1.jpg" (can also be .jpeg or .gif), but the user input should be able to select which image, and what effect to apply to it. You should import java.io, java.awt, and jave.util. Thank you in advance. The ImageInfo.java code is
Explanation / Answer
here is your class : --------->>>>>>>>>>
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Scanner;
import javax.imageio.*;
import javax.swing.*;
public class ImageViewer{
public static ImageInfo[] getImageList(String filename)throws IllegalArgumentException{
int n = 0;
try{
Scanner sc = new Scanner(new File(filename));
String line;
while(sc.hasNextLine()){
line = sc.nextLine();
n++;
}
sc.close();
}catch(Exception e){
throw new IllegalArgumentException("Unable to access input file: "+filename);
}
ImageInfo[] images = new ImageInfo[n];
n = 0;
try{
Scanner sc = new Scanner(new File(filename));
String[] line;
while(sc.hasNextLine()){
line = sc.nextLine().split(" ");
images[n++] = new ImageInfo(line[0],Integer.parseInt(line[1]),Integer.parseInt(line[2]),line[3]);
}
sc.close();
}catch(Exception e){
throw new IllegalArgumentException("Unable to access input file: "+filename);
}
return images;
}
public static int getImageNumber(Scanner console,ImageInfo[] images){
for(int i = 0;i<images.length;i++){
System.out.println((i+1)+". "+images[i]);
}
System.out.println((images.length+1)+". Quit the program ");
System.out.println("Choose a File number from above : ");
int n = console.nextInt();
if(n < 1 && n > images.length){
System.out.println("Invalid image Number");
return -1;
}
return n;
}
public static void displayImage(Scanner console, ImageInfo image){
JFrame editorFrame = new JFrame("Image Demo");
editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
String filename = image.getFilename();
editorFrame.setSize(image.getWidth()+10,image.getHeight()+10);
BufferedImage image1 = null;
try
{
image1 = ImageIO.read(new File(filename));
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
ImageIcon imageIcon = new ImageIcon(image1);
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);
editorFrame.pack();
editorFrame.setLocationRelativeTo(null);
editorFrame.setVisible(true);
int w = image1.getWidth();
int h = image1.getHeight();
Color[][] pixels = new Color[w][h];
for(int i = 0;i<w;i++){
for(int j = 0;j<h;j++){
pixels[i][j] = new Color(image1.getRGB(i,j),true);
}
}
System.out.println("Please enter an option below. ");
System.out.println("G-reyScale");
System.out.println("H-igh contrast");
System.out.println("N-egative");
System.out.println("Option:");
char ch = console.next().charAt(0);
switch(ch){
case 'g':
case 'G':
{
convertToGreyScale(pixels);
}
break;
case 'h':
case 'H':
{
convertToHighContrast(pixels);
}
break;
case 'n':
case 'N':
{
convertToNegative(pixels);
}
break;
default:
System.out.println("Invalid Option ");
return;
}
for(int i = 0;i<w;i++){
for(int j = 0;j<h;j++){
image1.setRGB(i,j,pixels[i][j].getRGB());
}
}
imageIcon = new ImageIcon(image1);
jLabel.setIcon(imageIcon);
}
public static void convertToGreyScale(Color[][] pixels){
int r,g,b;
for(int i = 0;i<pixels.length;i++){
for(int j = 0;j<pixels[0].length;j++){
r = pixels[i][j].getRed();
g = pixels[i][j].getGreen();
b = pixels[i][j].getBlue();
int a = (r+g+b)/3;
pixels[i][j] = new Color(a,a,a);
}
}
}
public static void convertToHighContrast(Color[][] pixels){
int r,g,b;
for(int i = 0;i<pixels.length;i++){
for(int j = 0;j<pixels[0].length;j++){
r = pixels[i][j].getRed();
g = pixels[i][j].getGreen();
b = pixels[i][j].getBlue();
if(r < 128){
r = 0;
}else{
r = 255;
}
if(g < 128){
g = 0;
}else{
g = 255;
}
if(b < 128){
b = 0;
}else{
b = 255;
}
pixels[i][j] = new Color(r,g,b);
}
}
}
public static void convertToNegative(Color[][] pixels){
int r,g,b;
for(int i = 0;i<pixels.length;i++){
for(int j = 0;j<pixels[0].length;j++){
r = pixels[i][j].getRed();
g = pixels[i][j].getGreen();
b = pixels[i][j].getBlue();
r = 255-r;
g = 255-g;
b = 255-b;
pixels[i][j] = new Color(r,g,b);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.