1. Engineering
  2. Computer Science
  3. a3loopsandimageprocessing the goal of this assignment is to use loop...

Question: a3loopsandimageprocessing the goal of this assignment is to use loop...

Question details

A3-Loops-and-Image-Processing The goal of this assignment is to use loop patterns to solve a variety of problems. Starting 1. In Eclipse, create a new project. Add a package a3, and a class LoopsAndlmages to that package. 2. Add this Picture.java file to the a3 package. You should not modify this file 3. Add this Arches.jpg e file to the a3 package. You can use other images of your choice as well. 4. In your LoopsAndlmages class, implement the static methods specified below 5. All methods you write should have a Javadoc comment with a description of what the method does and a @param for each parameter anda ribing what information is returned. @return desc 6. Add a main method with tests for each method. Your tests should cover possible return values (such as true or false) as well as special cases (for example, empty strings). Your tests should print what is being tested, what was expected, and the actual result from the method in a neatly formatted way. For the image processing methods below that work on an image, you should load an image, call the method, and show the result, but you do not need to describe it or have an expected result. 7. You do not need to test for cases that are prohibited in the description. If the description says that there is at least X in the parameter, then you do not need to test for the truth of that constraint. 8. None of your methods except for main may print anything to the console. While developing your solution, you are encouraged to print values to the console to help debug your code. Those print statements should be removed before submission. 9. Ensure you have consistent spacing and indenting in your file, proper and consistent indenting, and a Javadoc comment above the start of the class definition with your name, assignment title, and this class number. Required Methods You must implement each of the described methods with method name, return type and parameters carefully checked against the specification. When parameters are specified you must use the order they are listed in. You may implement additional methods if they aid your problem-solving In the following methods, part of your job is to consider what Java construct best suits the problem for example, a for-loop, a while-loop, or a nested loop Finally, you should only use constructs we have learned in this course to solve these problems. You should not search for how do I replace characters in a Java String and copy what you find. That is not getting clarification on how some small part of Java works, that is looking for a solution to the problem, which is plagiarism. 1. Method name: hideLetterA Parameter(s): A String. An empty string is allowed. Return value: A String that has the same characters as the String parameter except every letter a has been changed to an . Example: hideLetterA( A rabbit has a carrot) would return Ar bbit hscrrot Note: The letter A is not a2. Method name: hasMoreEvenThanOdd Parameter(s): A String containing only integer numbers separated by spaces. An empty string is allowed. Return value: A boolean value true if the number of even numbers in the String is greater than the number of odd numbers and false otherwise. Example: hasMoreEvenThanOdd1 346-8) would return true. Note: The % operator can be used to check if a number is evenly divisible by 2. which is what being even means second note. 이, no spec al case for being even or odd. 3. Method name: makeTextTriangle Parameter(s): An int value that can be assumed to be greater than 0 and less than 20 (this is really just a constraint for formatting) Return value: A String value that makes a text triangle. The first line should have a single *. Any following line, if needed, should have 1 more than the line above it, until there are as many lines as the parameter value. Example: makeTextTriangle(4) would return with each line ending in a new line. For the following methods, please review the slides and examples from lecture 8. In particular, follow the pattern of making a copy of an image and applying changes to that copy rather than to the Picture object sent as a parameter. 4. Method name: makeGrey Parameter(s): A Picture object. This is the source image Return value: A new Picture object. Each pixel in the returned Picture should have a color that is the grey scale equivalent of the corresponding pixel (the pixel with the same x and y coordinate) in the source image. In general, a grey scale color has different ways of being calculated - you must calculate it by averaging the red, green, and blue values from the pixel in the source image and then setting each of the red, green, and blue components of the pixel in the new image to this grey intensity. Example: A single pixel image with a color (100, 200, 50) would have a grey intensity of (100+200+50)/3- 116 and the new image would be a single pixel with color (116, 116, 116). An example of what a result might look like is the following (please dont treat this as a reference image as I am unsure of what compression or changes might be applied by Canvas).5. Method name: makeNegative Parameter(s): A Picture object. This is the source image. Return value: A new Picture object with the colors changed to a photonegative style. Each pixel in the returned Picture should have a color that is a negative of the corresponding pixel (the pixel with the same x and y coordinate) in the source image. You must calculate this by taking each red, green, and blue value in the source and setting it to 255 - each value in the returned image. Example: A single pixel image with a color (100, 200, 50) would make a new image with a single pixel with color (155, 55, 205) An example of what a result might look like is the following (please dont treat this as a reference image as I am unsure of what compression or changes might be applied by Canvas). 6. Method name: safeColor Parameter(s): A single int value representing one of a red, green, or blue value. Return value: An int that is the same as the parameter, except that it is O if the original value is less than zero and it is 255 if the original value is greater than 255 Example: If the parameter is 100, then the return value should be 100. If the parameter is 300, then the return value should be 255 7. Method name: makeBrighter Parameter(s): A Picture object. This is the source image. Return value: A new Picture object with the color values doubled. Each pixel in the returned Picture should have a color that has each red green, and blue component twice that of the corresponding pixel (the pixel with the same x and y coordinate) in the source image. Unthinkingly applied, this doubling will yield color values outside the allowed 0-255 range, which crashes the program. Clamp each calculated red, green, and blue value to a safe range by applying the safeColor method written above to the colors you calculate. Example: A single pixel image with a color (100, 200, 50) would make a new image with a single pixel with color (200, 255, 100) An example of what a result might look like is the following (please dont treat this as a reference image as I am unsure of what compression or changes might be applied by Canvas)package a3; *Compilation: javac Picture.java *Execution: java Picture imagename *Dependencies: none Data type for manipulating individual pixels of an image. The original * image can be read from a file in JPG, GIF, or PNG format, or the user can create a blank image of a given dimension. Includes methods for displaying the image in a window on the screen or saving to a file * % java Picture mandrill.jpg * Remarks *pixel (x, y) is column x and row y, where (0, 0) is upper left import java. awt.Color; import java.awt.FileDialog; import java.awt.Toolkit; import java.awt.event.ActionEvent import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL import javax.imageio.ImageIO; import javax.swing. ImageIcon; import javax. swing.JFrame; import javax. swing.JLabel: import javax. swing.JMenu; import javax.swing. JMenuBar; import javax.swing. JMenuItem; import javax.swing. JPanel; import javax.swing.KeyStroke; * This class provides methods for manipulating individual pixels of * an image using the RGB color format. The alpha component (for transparency) * is not currently supported *The original image can be read from a (ecode PNG), (ecode GIF) *or @code JPEG) file or the user can create a blank image of a given dimension. *This class includes methods for displaying the image in a window on *the screen or saving it to a file Pixel (<em> co1</em> , <em>row</em>) column <em> col</em> <em>row</em> * is and row By default, the origin (O, 0) is the pixel in the top-left corner, * which is a common convention in image processing * The method {@link setoriginLowerLeft()} change the origin to the lower left ap> * The tecode get and code set methods use link Color objects to get *or set the color of the specified pixel. * The @code getRGB and @code setRGB ) methods use a 32-bit 1@code inth *to encode the color, thereby avoiding the need to create temporary code Color objects. The red (R), green (G), and blue (B) components * are encoded using the least significant 24 bits *Given a 32-bit 1@code intl encoding the color, the following code extracts the RGB components: <blockquote><pre> *int r - (rgb >> 16) & 0xFF * int g-(rgb >> 8) &0xFF * int b-(rgb >> 0) &0xFF *</pre></blockquote> *Given the RGB components (8-bits each of a color, * the following statement packs it into a 32-bit fecode int: * <blockquote><pre> * int rgb= (r << 16) + (g << 8) + (b << 0); * </pre></blockquote:>// now try to read from file in same directory as this .class file else URL url-getcClass).getResource (filename); if (url null) uri = new URL (filename) ; imageImageIO.read (url); if (imagenull) throw new IllegalArgumentException (could not read image file: + filename); width -image.getWidth (null); height image.get Height (null); catch (IOException ioe) throw new IllegalArgumentException (could not open image file: + filename, ioe); *Creates a picture by reading the image from a PNG, GIF, or JPEG file @param file the file *@throws IllegalArgumentException if cannot read image *throws IllegalArgumentException if (@code file is (@code null) public Picture (File file) if (fileull) throw new IllegalArgumentException (constructor argument is null try imageImageIO.read (file) catch (IOException oe) [ throw new IllegalArgumentException (could not open file: + file, ioe) if (imagenull) throw new IllegalArgumentException (could not read file: + file) width -image.getWidth (null); height- image.getHeight (null); filename file.getName ) *Returns a elink JLabel) containing this picture, for embedding in a (e1ink JPanel), * [elink JFrame or other GUI widget. *@return the (@code JLabel h public JLabel getJLabel // no image available if (imagenull) return null; ImageIcon icon - new ImageIcon (image); return new JLabel (icon); * Sets the origin to be the upper left pixel. This is the default. public void setOriginUpperLeft) isoriginUpperLefttrue *Sets the origin to be the lower left pixel public void setoriginLowerLeft) isoriginUpperLeft -false;Displays the picture in a window on the screen public void show 1 // create the GUI for viewing the image if needed if (frame == null) Erame-new JFrame ) JMenuBar menuBar new JMenuBar JMenu menu = new JMenu (File); menuBar.add (menu) MenuItem menuIteml -new JMenuItem Save. ..); menuIteml.addActionListener (this); // use getMenuShortcutKeyMaskEx ) in Java 10 (getMenuShortcutKeyMask menuItem1.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_S, deprecated) Toolkit.getDefaultToolkit) getMenuShortcutKeyMask)) menu.add (menuItemi); Erame.setJMenuBar (menuBar) frame.setContentPane (getJLabel )); // f.setDefaultCloseoperation (JFrame.EXIT_ON CLOSE) frame.setDefaultCloseOperation (JFrame. DISPOSE_ON_CLOSE) if (filenamenull) frame.setTitle (width -by- + height) else frame. setResizable (false) frame. pack ); Erame. setVisible (true); frame.setTitle filename) /I draw frame.repaintO *Returns the height of the picture return the height of the picture (in pixels) public int height return height; Returns the width of the picture *return the width of the picture (in pixels) public int width) return width,; private void validateRowIndex (int row) if (row<01 row heightO) throw new IllegalArgumentException (row index must be between 0 and + (height1) + : + row) private void validateColumnIndex (int col) if (col<0 colwidth)) throw new IllegalArgumentException (column index must be between 0 and + (width 1 + : +col); Returns the color of pixel ((ecode col), ecode ro)) as a (elink java.awt.Color param col the column index @param row the row index *@return the color of pixel (ecode col, ecode row)) * @throws IllegalArgumentException unless both [ecode 0-col< width and fecode 0< row< heighth public Color get (int col, int row)validateColumnIndex (col); validateRowIndex (row) int rgb- getRGB (col, row) return new Color (rgb); *Returns the color of pixel (code col), @code row) as an code int). * Using this method can be more efficient than {@link #get (int, int)) because * it does not create a @code Color) object. eparam col the column index *eparam row the row index *return the integer representation of the color of pixel ((@code col, ecode row) *throws IllegalArgumentException unless both I@code 0col < width) and @code 0 <row <heighth public int getRGB (int col, int row) validateColumnIndex (col) validateRowIndex (row); if (isOriginUpperLeft) return image.getRGB (col, row); else return image.getRGB (col, heightrow - 1); *Sets the color of pixel (@code col), code row)) to given color. *param col the column index *eparam row the ro index *eparam color the color * @throws IllegalArgumentException unless both {@code 0 <= col < width) and {ecode 0 <= row < height) *ethrows IllegalArgumentException if (ecode color) is ecode null) public void set (int col, int row, Color color) [ validateColumnIndex (col); validateRowIndex (row) if (colornull) throw new IllegalArgumentException (color argument is null int rgb-color.getRGB setRGB(col, row, rgb); * Sets the color of pixel (@code col), (ecode row)) to given color. * eparam col the column index *eparam row the row index *eparam rgb the integer representation of the color *throws IllegalArgumentException unless both I@code 0 <-col< width and @code 0<-row< heighth public void setRGB (int col, int row, int rgb) validateColumnIndex (col) validateRowIndex (row); if (isOriginUpperLeft) image.setRGB (col, row, rgb); else image.setRGB (col, height- row - 1, rgb); Returns true if this picture is equal to the argument picture eparam other the other picture *return (@code true if this picture is the same dimension as (@code otherh and if all pixels have the same color @code false otherwise public boolean equals (Object other) if (other this) return true; if (other-= null) return false; if (other.getClass() != this.getClass()) return false; Picture that-(Picture) other; if (this.width ( ) !-that.width ()) return false; if (this.heighto !- that.height return false; for (int col-0 col < width; col++) for (int row- 0; row < height(); row++) if (this.getRGB (col, row) !- that.getRGB (col, row)) return false; return true*Returns a string representation of this picture * The result is a <code>width< /code>-by-<code>height</code> matrix of pixels, *where the color of a pixel is represented using 6 hex digits to encode *the red, green, and blue components *return a string representation of this picture public String toString) StringBuilder sbnew StringBuilder sb.append (width +-by-+ height picture (RGB values given in hex) In) for (int row = 0; row < height; row++) { for int col0 colwidth; col+ int rgb-0; if (isoriginUpperLeft) rgb- image.getRGB (col, row) else sb.append (String . format (#306x , rgb & 0xFFFFFF)); rgb -image.getRGB (col, height row- 1) sb.append ( return sb.toString ).trim) * This operation is not supported because pictures are mutable *return does not return a value throws UnsupportedoperationException if called public int hashCode throw new UnsupportedoperationException (hashCode ) is not supported because pictures are mutable); Saves the picture to a file in either PNG or JPEG format. * The filetype extension must be either png or .jpg. * @param name the name of the file @throws IllegalArgumentException if 1@code name is tecode null) public void save (String name) if (nameull) throw new IllegalArgumentException (argument to save is null save (new File (name); filename-name; *Saves the picture to a file in a PNG or JPEG image format. @param file the file throws IllegalArgumentException if 1@code file) is fecode null) public void save (File file) if (file null) throw new IllegalArgumentException (argument to save() is null); filename = file . getName ( ) ; if (frame != null) frame . setTitle (filename); String suffix-filename.substring (filename.lastIndexof (.) + 1) if (jpg.equalsIgnoreCase (suffix) II png.equalsIgnoreCase (suffix)) i tryt ImageIO.write(image, suffix, file) catch (IOException e) I e.printStackTrace ) else System.out.println (Error: filename must end in .jpg or .png)*Opens a save dialog box when the user selects Save As from the menu. @Override public void actionPerformed (ActionEvent e) FileDialog choosernew FileDialog (frame, Use a png or .jpg extension, FileDialog.SAVE); chooser.setVisible (true) if (chooser.getFile) !- null) save (chooser.getDirectory) + File.separator+ chooser.getFile O) * Unit tests this (@code Picture) data type. * Reads a picture specified by the command-line argument, * and shows it in a window on the screen. *@param args the command-line arguments public static void main (String[l args)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution