problems getting past the out of bounds error
I know this is basic but I am in an into class doing this...I have a method to copy a picture onto a white background. I need to be able to make it so that when I put in coordinates it will not go out of bounds and will just cut part of the picture off and show the rest any help would be great. I know I need an if loop or two in there I just can not get it to work Here is the code I have: I am also using dr java to write everything
Code Java:
public void copyPicture(Picture picToCopy, int startX, int startY){
Pixel pSource = null;
Pixel pTarget = null;
int offsetX = startX, offsetY=startY;
for (int x=0; x<picToCopy.getWidth(); x++){
for ( int y = 0; y<picToCopy.getHeight(); y++){
pSource= picToCopy.getPixel(x,y);
pTarget= this.getPixel(offsetX+x,offsetY+y);
pTarget.setColor(pSource.getColor());
}//end y loop
}//end x loop
return;
}
//end copyPicture
Re: problems getting past the out of bounds error
Carefully go through the logic of your loops. If you loop through the entire width/height, adding an offset other than 0 will at some point take you out of bounds. Either loop from offset forward, or from 0 to offset, depending upon your needs.
Re: problems getting past the out of bounds error
I must have worded my first thread wrong. I am sorry. I want to be able to go out of bounds. The best way I can think of describing what I want to do is if you click on one of your windows and move it partially off your screen. You can only see part of the window. Thats what I want to be able to do with a picture. I hope I cleared things up and I thank you for your help.
Re: problems getting past the out of bounds error
Maybe a long shot but...this isn't for Troy's class is it?
Anways, do you want to just copy a part of the picture, and have white space next to it? Or do you want nothing next to it at all, and only have the piece of the window you want, which essentially is "cropping".
Re: problems getting past the out of bounds error
this is what the instructions said, I wasn't able to paste the picture
Extend the copyPicture method so that if there isn’t enough room in the target picture for the source picture the method simply returns.
Some examples:
Assume blankPic is the 7inX95in picture and shopsPic is the shops picture we have been using these past few weeks. Further let blankPicWidth and blankPicHeight be the blank picture’s width and height and likewise for shops, then
blankPic.copyPicture( shopsPic, blankPicWidth - shopsPicWidth/2, blankPicHeight/2) and blankPic.copyPicture( shopsPic, 0, blankPicHeight - shopsPicHeight/2) would produce: