Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with the question below and add explanations for the lines of cod

ID: 3864488 • Letter: P

Question

Please help me with the question below and add explanations for the lines of code please.

Write a program that subtracts the red, blue, and green components of all pixels of a Picture by specified values. To do this, you will write one method in Picture.java and a main program to test your method by showing and exploring the original and changed pictures.

Method: subtractColor

Add a method to Picture.java called subtractColor (be sure to use PSA3/bookClasses/Picture.java). Your method takes three parameters. These parameters specify how much the color components of all pixels of the picture should be reduced (2 pts).

o The order in which the color values are passed to the method should be red, green, and then blue.

o For example, if you pass (100, 20, 45) as parameters to your method and the color values of one of the pixels of the picture is (150, 200, 50), then these color values should be reduced to (50, 180, 5) after your method is called. Note that a negative color value is automatically counted as 0 as long as you are using setRed(), setGreen(), or setBlue(). If you try to use setColor() with a new Color() as a parameter, you will get an error when one of the color components is negative. The method should reduce the color values of ALL the pixels in the picture (2 pts).

Testing subtractColor:

Download PSA3A.java from the course website and save this file in PSA3/bookClasses.

PSA3A.java is worth 4 points:

o (2 pts) for proper execution and calling the new method in Picture.java

o (2 pts) for proper commenting of PSA3A.java and method in Picture.java

Fill in the statements (inside the main method) you need to test and show the results of your methods:

o Use the FileChooser.pickAFile() command to pick a picture. You can choose a file from the mediaSources directory from your book CD. However, it is more fun to use your own pictures to make it more personal.

o Before you modify this picture, we want to make a COPY of it so that we

can "see" the difference after it is changed. To make a copy of the

original, use the copy constructor that looks like this:

Picture copy = new Picture(original);
o Next explore the original picture you picked. Note that you can explore a

picture by calling the predefined explore() method on that picture.
o Then call your method to change the COPY of the picture you made.
o Then explore the copy of the picture after you called your method on it to change it.

o Mouse around to explore the original and changed picture to make sure

your method works correctly. In other words, find a specific pixel and

make sure that it has been reduced by the correct amount of red, green, and blue in the copied picture.

****Code for PSA3:

Explanation / Answer

ExtraCreditPSA3.java

public class ExtraCreditPSA3
{
    //The line below is magic, you don't have to understand it (yet)
    public static void main (String[] args)
    {
       Picture copy= new Picture(FileChooser.pickAFile());
       copy.explore();
       copy.extraCreditFilter(1.2);
       copy.explore();
    }
}

PSA3A.java


public class PSA3A
{
    //The line below is magic, you don't have to understand it (yet)
    public static void main (String[] args)
    {
       Picture copy= new Picture(FileChooser.pickAFile());
       copy.explore();
       copy.subtractColor(55,0,23);
       copy.explore();
    }
}

PSA3B.java

public class PSA3B
{
    //The line below is magic, you don't have to understand it (yet)
    public static void main (String[] args)
    {
       Picture copy= new Picture(FileChooser.pickAFile());
       int height=copy.getHeight();
       int width=copy.getWidth();
       copy.explore();

     
       /*subtractColor
        copy.subtractColor(50,150,200);
        copy.explore();
        */
    
       /*complement
       copy.complement(0,height*width/3);
       copy.explore();
        */
     
       /*grayscale
        copy.grayscale((height*width/3),(height*width*2/3));
        copy.explore();
        */
     
       /*myFilter
        copy.myFilter(height*width*2/3,height*width);
        copy.explore();
      */
     
       //tricolor
       /* copy.complement(0,height*width/3);
        copy.grayscale((height*width/3),(height*width*2/3));
        copy.myFilter(height*width*2/3,height*width);
        copy.explore();
       */
    }
}

Picture.java

/

/*--------PROGRAM DESCRIPTION----------
subtractColor: makes a red, blue, green color darker. the first number, darkens the red,
the second darkens green, and the third darkens blue
complement:creates the opposite color of a picture from a point to another point
on a picture
grayscale: creates black and white version of a picture from a point to another point
on a picture
myFilter: creates a greenish yellow version of the opposite color of a picture from a point
to another point on a picture
extraCreditFilter:makes picture more colorful
*/

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;
import java.util.List; // resolves problem with java.awt.List and java.util.List


public class Picture extends SimplePicture
{
///////////////////// constructors //////////////////////////////////

/**
   * Constructor that takes no arguments
   */
public Picture ()
{
    /* not needed but use it to show students the implicit call to super()
     * child constructors always call a parent constructor
     */
    super();
}

/**
   * Constructor that takes a file name and creates the picture
   * @param fileName the name of the file to create the picture from
   */
public Picture(String fileName)
{
    // let the parent class handle this fileName
    super(fileName);
}

/**
   * Constructor that takes the width and height
   * @param width the width of the desired picture
   * @param height the height of the desired picture
   */
public Picture(int width, int height)
{
    // let the parent class handle this width and height
    super(width,height);
}

/**
   * Constructor that takes a picture and creates a
   * copy of that picture
   */
public Picture(Picture copyPicture)
{
    // let the parent class do the copy
    super(copyPicture);
}

/**
   * Constructor that takes a buffered image
   * @param image the buffered image to use
   */
public Picture(BufferedImage image)
{
    super(image);
}

////////////////////// methods ///////////////////////////////////////

/**
   * Method to return a string with information about this picture.
   * @return a string with information about the picture such as fileName,
   * height and width.
   */
public String toString()
{
    String output = "Picture, filename " + getFileName() +
      " height " + getHeight()
      + " width " + getWidth();
    return output;
  
}

   /*
    * Method Header goes here (see writeup)
    */
public void subtractColor(int rSub, int gSub, int bSub)
    {
      Pixel [] pixelArray=this.getPixels();
      int value=0;
      for (Pixel pixelObj:pixelArray)
           {
             value=pixelObj.getRed();
             value=value-rSub;
             pixelObj.setRed(value);
             value=pixelObj.getGreen();
             value=value-gSub;
             pixelObj.setGreen(value);
             value=pixelObj.getBlue();
             value=value-bSub;
             pixelObj.setBlue(value);
           }
    }

   /*
    * Method Header goes here (see writeup)
    */
public void complement(int start, int end)
    {
      Pixel [] pixelArray=this.getPixels();
      Pixel pixel=null;
      int value=0;
      int index=start;
      while (index<end)
           {
             pixel=pixelArray[index];
             value=pixel.getRed();
             value=255-value;
             pixel.setRed(value);
             value=pixel.getGreen();
             value=255-value;
             pixel.setGreen(value);
             value=pixel.getBlue();
             value=255-value;
             pixel.setBlue(value);
             index=index+1;
           }
    }

   /*
    * Method Header goes here (see writeup)
    */
public void grayscale(int start, int end)
    {
      Pixel [] pixelArray=this.getPixels();
      Pixel pixel=null;
      int value=0;
      int index=start;
      while (index<end)
           {
             pixel=pixelArray[index];
             //get grayscale value
             value=(pixel.getRed()+pixel.getBlue()+pixel.getGreen())/3;          
             pixel.setRed(value);
             pixel.setGreen(value);
             pixel.setBlue(value);
             index=index+1;
           }
    }
  

    /*
     * Method Header goes here (see writeup)
     * green scales the negative of a picture
     */
public void myFilter(int start, int end)
     {
      Pixel [] pixelArray=this.getPixels();
      Pixel pixel=null;
      int value=0;
      int index=start;
      while (index<end)
           {
             pixel=pixelArray[index];
             value=pixel.getRed();
             value=(int)((255-value)*(double)1.5);
             pixel.setRed(value);
             value=pixel.getGreen();
             value=(int)((255-value)*(double)1.7);
             pixel.setGreen(value);
             value=pixel.getBlue();
             value=(int)((255-value)*(double)0.5);
             pixel.setBlue(value);
             index=index+1;
           }
     }
     //extraCredit Filter
public void extraCreditFilter(double percent)
    {
      Pixel [] pixelArray=this.getPixels();
      int gl=0;
      //get average gray level value of entire picture
      for (Pixel pixelObj:pixelArray)
           {
             int r=pixelObj.getRed();
             int g=pixelObj.getGreen();
             int b=pixelObj.getBlue();
             gl=r+g+b+gl;
           }
       gl=gl/(3*pixelArray.length);
    
      //values that are above the gray level are scaled up and
      //values below the gray level are scaled down
      for (Pixel pixelObj:pixelArray)
           {
             int r=pixelObj.getRed();
             int g=pixelObj.getGreen();
             int b=pixelObj.getBlue();
             //red
             if(r>gl){
             r=(int)(r*percent);
             pixelObj.setRed(r);
             }
             else {
             r=(int)(r/percent);
             pixelObj.setRed(r);
             }
             //green
             if(g>gl){
             g=(int)(g*percent);
             pixelObj.setGreen(g);
             }
             else {
             g=(int)(g/percent);
             pixelObj.setGreen(g);
             //blue
             if(b>gl){
             b=(int)(b*percent);
             pixelObj.setBlue(b);
             }
             else {
             b=(int)(b/percent);
             pixelObj.setBlue(b);
             }
             }
           
           }
    }
} // this } is the end of class Picture, put all new methods before this

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote