In: Computer Science
//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)
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. :)