Question: Intro Java Computer Science and Programming. I need help with debugging my code. The Assignment: Complete the copyTo method in the Picture class [2 points]

Intro Java Computer Science and Programming.

I need help with debugging my code.

The Assignment:

Complete the copyTo method in the Picture class [2 points] public void copyTo(Picture target, int startX, int startY) This method copies the calling object Picture onto the parameter Picture target, placing the upper-left corner of the calling object image (i.e. coordinate (0, 0)) at coordinate (startX, startY) in the parameter Picture target. It does NOT modify the calling object. This method is similar to the copyPictureTo method defined in your book, with two important exceptions: First, instead of copying from the parameter to the calling object, it copies the picture from the calling object to the Picture passed as a parameter (target). It copies the whole calling object picture to the target, placing its upper left corner at position startX, startY in the parameter Picture. That is, your method must not modify the calling object, but should modify the Picture referenced by the parameter. Also unlike the method defined in the book, it must handle the case where the target Picture is not large enough to hold the calling object picture in in the location specified. If the calling object image will go out of bounds, this method simply copies that part that will fit in the specified position, and ignore the rest. You may assume that startX and startY will be within the bounds of the calling object.

My code (the for loops are nested):

public void copyTo(Picture target, int startX, int startY)

{

Pixel sourcePixel = null;

Pixel targetPixel = null;

int limitx = target.getWidth();

int limity = target.getHeight();

for (int sx = 0, tx = startX; sx < limitx && tx < this.getWidth(); sx++, tx++)

{

for (int sy = 0, ty = startY; sy < limity; sy++, ty++)

{

sourcePixel = target.getPixel(sx, sy);

targetPixel = this.getPixel(tx, ty);

targetPixel.setColor(sourcePixel.getColor());

}

}

}

------------------ TEST 1 -----------------------

Testing copyTo() Method : Source Image : yellowFlowers.jpg Target Image : caterpillar.jpg startX : 0 startY : 0 Test Passed!! Good Job!!

------------------ TEST 2 -----------------------

Testing copyTo() Method : Source Image : caterpillar.jpg Target Image : yellowFlowers.jpg startX : 100 startY : 100 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318) at java.awt.image.BufferedImage.getRGB(BufferedImage.java:917) at SimplePicture.getBasicPixel(SimplePicture.java:300) at Pixel.getAlpha(Pixel.java:72) at Pixel.setColor(Pixel.java:212) at Picture.copyTo(Picture.java:201) at ImageTransformationTester.testcopyTo(ImageTransformationTester.java:39) at ImageTransformationTester.main(ImageTransformationTester.java:291)

Update:

https://gist.github.com/Camicam311/e82597de79670139a25f05ef3dd4d0b8

This link shows the code formatted properly.

We are only supposed to write the method. We cannot access or alter the testers.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!