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

Java: A digital image is made up of pixels. Each pixel is a tiny square of a giv

ID: 3594739 • Letter: J

Question

Java:

A digital image is made up of pixels. Each pixel is a tiny square of a given color. The Picture class has methods to manipulate pixels. Each pixel has x, y coordinates. You can think of the image as being rows of pixels. The x coordinate is the row and the y coordinate is the position within the row. The x coordinate varies from 0 to the width of the image, and the y coordinate varies from 0 to the height of the coordinate. You can use nested loops to pick up each pixel in the image and then do something with it. Worked Example 6.2 gives an example of iterating through all the pixels in an image.

Your task is to write an application Framer that can put a round frame around a Picture. See the output below. You should set all the pixels outside the circle to black. The center of the circle is the center of the image, and the radius is 40 percent of the width or height, whichever is smaller. Find the center by dividing the width and the height of the image by 2. Get the width and height by using methods so that the code will work if you use any image. That is, do not assume that the dimensions of the image are 200 x 180.

You will need to import the Udacity graphics (https://s3.amazonaws.com/udacity-hosted-downloads/graphics.zip) package into your project.

Hint: If the distance of the pixel from the center of the circle is greater than the radius, set the pixel color at that x, y to Color.BLACK. Think Pythagorean Theorem

Here is the original image of oliver_bed.jpg

Here is the a screenshot of the output

Explanation / Answer

To find whether a point resides outside the circle, you’ll have to evaluate the basic formula :-

(x-centerX)^2 –(y-centerY)^2 > radius^2; where x and y are the coordinates of the point to be checked, centerX is the center x coordinate of the circle and centerY is the center y coordinate of the circle.

I have created a class called Framer.java , created a constructor with string parameter which holds the filename of the image. A Picture object is created with passing filename as a constructor parameter. height,width,radius and other parameters of the image are calculated and then looped through the coordinates; modified the image, finally displayed on the screen by calling draw() of the Picture object. Check javadoc comments included for more info.

//Framer.java file

import java.awt.BorderLayout;

import java.awt.Canvas;

import java.io.File;

import javax.swing.JFrame;

import javax.swing.JPanel;

import udacitylibrary.Color; /*I have imported udacity graphics library into a package called udacitylibrary,you can use yours*/

import udacitylibrary.Picture;

public class Framer{

      Framer(String source) {

            Picture pic = new Picture(source); /*fetching the picture from the file url*/

            int height=pic.getHeight(); /*getting the height of the image*/

            int width=pic.getWidth(); /*getting the width of the image*/

            int centerX=width/2; /*finding the center x coordinate of the image*/

            int centerY=height/2; /*finding the center y coordinate of the image*/

            int radius=(int) Math.min(width*(.4), height*(.4)); /*radius = 40% of the height or width; whichever is smaller*/                    

            for(int x=0;x<width;x++){

                  for(int y=0;y<height;y++){

                        int dx=Math.abs(x-centerX); /*distance between the current x point and the center x point*/

                        int dy=Math.abs(y-centerY); /*distance between the current y point and the center y point*/

                        if(dx*dx + dy*dy > radius*radius){ /*applying the formula to find whether the point resides outside circle

                                                                              (x-centerX)^2 + (y-centerY)^2 must be greater than radius^2*/

                              pic.setColorAt(x, y, Color.BLACK); /*changing the color of that point to black*/

                        }

                  }

            }

            pic.draw(); /*drawing the image*/

      }

      public static void main(String[] args) {

            String file_source = "oliver_bed.jpg";

            new Framer(file_source);/*passing the file name (String) as url*/

      }

}

//Output image

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