Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: problems getting past the out of bounds error

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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
    Last edited by helloworld922; April 28th, 2011 at 01:30 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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".
    Last edited by vanDarg; April 28th, 2011 at 05:52 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

Similar Threads

  1. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  2. Error Page in JSP Having Problems.
    By goldest in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 15th, 2010, 09:03 AM
  3. Getting date based on days past year
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 14th, 2010, 04:06 PM
  4. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 02:47 PM
  5. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM