Question

In: Computer Science

//in Picture.java public Picture copyRegionToNew(int xSource, int ySource, int xTarget, int yTarget ) { Picture newCanvas...

//in Picture.java

public Picture copyRegionToNew(int xSource, int ySource, int xTarget, int yTarget )

{

Picture newCanvas = new Picture();

Pixel sPixel, tPixel = null;

for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)

{

for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)

{

sPixel = this.getPixel(sX,sY);

tPixel = newCanvas.getPixel(tX,tY);

tPixel.setColor(sPixel.getColor());

}

}

return newCanvas;

}

//in main

Picture fish = new Picture( "fish.jpg" );

fish.copyRegionToNew(10, 30, 50, 50);

newCanvas.show();

----------------------------------------------------------------------------------------------------------------------------------------------------

Describe what this code will do. Describe why.

please, help me! Thanks in advance! (JAVA PROGRAMMING)

Solutions

Expert Solution

This method is basically copying the image from one location to another location on the canvas or you can say on the Picture class defined.

See, in the main(), you are creating an object giving an image file to Picture class constructor.

The class definition is not provided over here, but from given code, it seems that it takes the image given in the constructor, opens it and stores it in the array or list of the pixels.

The object fish is created in this way. Now, its copyRegionToNew() method is called, here the method is returning an object of class Picture, but in main() method, the result is not assigned to any object. So, the next line newCanvas.show() will give an error as newCanvas is not defined in the main().

So, first change the line to

Picture newCanvas = fish.copyRegionToNew(10, 30, 50, 50);

newCanvas.show();

Now coming to the copyRegionToNew() method.

It is taking 4 parameters: first 2 are the source (x, y) and the next two are target(x, y)

Now, 2 object of Pixel class are defined from which xPixel gets the source pixel and tPixel deals with target pixel.

Next comes the for loops. The work of these loops can be identified as:

They start from (xSource, ySource) pixel. Till (xSource+99, ySource+99) pixel, [because <100 + xSource written in the for loop condition so..] , each pixel is taken from the source by using sPixel, and its color is set to the pixel of newCanvas. Starting pixel of newCanvas will be (tx, ty) and ending one will be (tx+99, ty+99).

e.g. copyRegionToNew(10, 30, 50, 50) works as copying the pixel from (10, 30) to (109, 129) in the target from (50, 50) pixel to (149, 149).

The copy using 2 for loop is done in row-wise. i.e. copy (10, 30) color in (50, 50) then (10, 31) in (50, 51) .... at last (10, 129) in (50, 149). Then this row is completed. Now, go to row having xPixel = 11 in source and 51 in target.

So, for the second line copy (11, 30) in (51, 50), .........

Similarly this is done for every row and inside the row, every column is processed in the same way.

So, the reason of using this function is : it takes a picture, along with starting pixel, and copies its region with distance 100 to the new picture and the new picture's starting location is provided. The offset for copying image is 100.

Please comment for any query. Thank you. :)


Related Solutions

import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero or the initial reward. Functionality: After the result of the reward function is stored, this function should be called. The goal of this function is to help the robot in case it faced a lot of damage in the current step. However, this function will help the robot only if the robot’s reward was negative and the direction that the user inputted was up....
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT