Question

Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage;...

Hi. I require your wonderful help with figuring out this challenging code.

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;


public class ImageLab {

  /* 
   * This is the grayscale example. Use it for inspiration / help, but you dont
   * need to change it.
   *
   * Creates and returns a new BufferedImage of the same size as the input
   * image. The new image is the grayscale version of the input (red, green, and
   * blue components averaged together).
   */
  public static BufferedImage imageToGrayscale(BufferedImage input) {
    // store the image width and height in variables for later use
    int img_width = input.getWidth();
    int img_height = input.getHeight();

    // create a new BufferedImage with the same width and height as the input
    // image. TYPE_INT_RGB means we want to specify pixel values using their
    // red, green, and blue components.
    BufferedImage output_img = new BufferedImage(
        img_width, img_height, BufferedImage.TYPE_INT_RGB);

    // Loop over all pixels
    for (int x = 0; x < img_width; x++) {
      for (int y = 0; y < img_height; y++) {

        // Get the color of the input image as a java.awt.Color object
        Color color_at_pos = new Color(input.getRGB(x, y));

        // Call the getRed(), getGreen(), and getBlue() methods on the color
        // object to store the red, green, and blue values into separate integer
        // variables.
        int red = color_at_pos.getRed();
        int green = color_at_pos.getGreen();
        int blue = color_at_pos.getBlue();

        // to get the grayscale version, we just average the red, green, and
        // blue channels, then use that average as the red, green, and blue
        // values in the output image.
        int average = (red + green + blue) / 3;

        // You'll get an IllegalArgumentException if you try to specify red,
        // green, or blue values greater than 255 or less than 0. While it's
        // mathematically impossible to average three values in that range and
        // get a value outside the range, the calculations in the other tasks
        // will sometimes produce values outside the range. This code "clamps"
        // the value to the range 0 - 255
        if (average < 0) {
          average = 0;
        } else if (average > 255) {
          average = 255;
        }

        // make a new Color object, which has the average intensity ((r+g+b)/3)
        // for each of its channels.
        Color average_color = new Color(average, average, average);

        // in the output image, set the color at position (x,y) to our
        // calculated average color
        output_img.setRGB(x, y, average_color.getRGB());
      }
    }

    // return the output image.
    return output_img;
  }

  public static BufferedImage threshold(BufferedImage input, int level) {
    // Replace this method body with your code
    return null;
  }

  public static BufferedImage quantize(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }

  public static BufferedImage colorRotate(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }

  public static BufferedImage warhol(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }

  public static BufferedImage sharpen(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }

  public static BufferedImage boxBlur(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }

  public static BufferedImage simpleEdgeDetect(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }
 
  public static BufferedImage sobelEdgeDetect(BufferedImage input) {
    // Replace this method body with your code
    return null;
  }


  /*=============== You don't need to change anything below this line =======*/     

  public static BufferedImage readImage(String filename) {
    BufferedImage img = null;
    try {
      img = ImageIO.read(new File(filename));
    } catch (IOException e) {
      System.out.println("Error - couldn't load " + filename);
      return null;
    }

    return img;
  }

  public static void writeImage(BufferedImage img, String filename) {
                try {
      System.out.printf("Writing %s...\n", filename);
      File ImageFile = new File(filename);
                        ImageIO.write(img, "jpg", ImageFile);
                } catch (IOException e) {
      System.out.printf("Error writing %s\n", filename);
                        e.printStackTrace();
                }
  }

  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("Usage: java ImageLab <filename>");
      System.exit(0);
    }

    BufferedImage img = readImage(args[0]);

    if(img == null) {
      System.out.println("Couldn't read image");
      System.exit(0);
    }

    System.out.println("Running imageToGrayscale()...");
    BufferedImage gray = imageToGrayscale(img);
    if (gray != null) {
      writeImage(gray, "grayscale.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running threshold()...");
    BufferedImage thresh = threshold(img, 128);
    if (thresh != null) {
      writeImage(thresh, "threshold.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running quantize()...");
    BufferedImage quant = quantize(img);
    if (quant != null) {
      writeImage(quant, "quant.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running colorRotate()...");
    BufferedImage rot = colorRotate(img);
    if (rot != null) {
      writeImage(rot, "colorrotate.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running warhol()...");
    BufferedImage war = warhol(img);
    if (war != null) {
      writeImage(war, "warhol.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running sharpen()...");
    BufferedImage sharp = sharpen(img);
    if (sharp != null) {
      writeImage(sharp, "sharpen.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running boxBlur()...");
    BufferedImage blur = boxBlur(img);
    if (blur != null) {
      writeImage(blur, "blur.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running simpleEdgeDetect()...");
    BufferedImage edge = simpleEdgeDetect(img);
    if (edge != null) {
      writeImage(edge, "edge.jpg");
    } else {
      System.out.println("Got null");
    }

    System.out.println("Running sobelEdgeDetect()...");
    BufferedImage sobel = sobelEdgeDetect(img);
    if (sobel != null) {
      writeImage(sobel, "sobel.jpg");
    } else {
      System.out.println("Got null");
    }

  }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The expression java.awt.Color refers to a file named Color.class.This is a class that has colors that we may use to change the apperance of the object int the interface which we are using.

In this,to change the apperance of area in browser while running program we use YELLOW color object.

Color begins with a capital letter that tells that it is a class file in java . Firstly note the path to the Color class.It is diffrent to the path of JApplet and JLabel classes.we all know that Classes in java are usually grouped together by functionality.

Many of the classes that control the apperance of GUI are contained in awt package. awt stands for "abstract windowing toolkit".

The classes in this package are contained in the version of java prior to java swing.That is the reason why the path is "java .awt.Color".

Add a comment
Know the answer?
Add Answer to:
Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public...

    I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public class Instruction { public static final int DW = 0x0000; public static final int ADD = 0x0001; public static final int SUB = 0x0002; public static final int LDA = 0x0003; public static final int LDI = 0x0004; public static final int STR = 0x0005; public static final int BRH = 0x0006; public static final int CBR = 0x0007; public static final int HLT...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

  • [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what...

    [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what is wrong with my code. Here'e the problem: Write a class SemiCircle that represents the northern half of a circle in 2D space. A SemiCircle has center coordinates and a radius. Define a constructor: public SemiCircle(int centerX, int centerY, int theRadius) Implement the following methods: public boolean contains(int otherX, int otherY) returns true if the point given by the coordinates is inside the SemiCircle....

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT