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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: Image looping issue

  1. #1
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Image looping issue

    Hello everyone

    I have been trying to loop an image using the for loop within graphics 2D which consists of 17 images on the x axis and 17 images (across) on the y axis (down), creating a 'L' shape. When I compile and run the program however it prints 17 images on the y axis and none on the x. I have no idea what fault I have made as whatever I do it seems to ignore the x axis (or at least the way i interpret it to be printed.) How do I make it print along the x-axis? What exactly am I doing wrong? Here is my code:

      private void drawMaze(Graphics2D g){
        	Graphics2D g2 = (Graphics2D)g;
     
        	    int width = 17; //number of images drawn along the x-axis (none)
                int height = 17; //number of images drawn along the y axis
                int xOffset = 21; //width of image
                int yOffset = 21; //height of image
                int currentX = 10; //initial x coordinate of image
                int currentY = 10; //initial y coordinate of image
                for(int i = 0; i < width; i++) {
                     for(int j = 0; j < height; j++) {
                           g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                           currentY += yOffset;
                     }
                     currentX += xOffset;
                     currentY = 0;

    Any help would greatly be appreciated, thanks!
    Last edited by Montario; June 2nd, 2012 at 02:09 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    The nested loops look ok. The ending } for the outer loop is missing. Is there any code is missing?

    Add a call to print() that prints out the x and then all the y values on each column for each image that is drawn to show where the images are being drawn. Add a println() after the end of the inner loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    Thanks Norm, I was missing some little code and never ended the outer loop, a silly unnoticeable mistake. Thank you so much! How would I go about making the printed box of images hollow however? I have a 17x17 box of images, how would I program it to just print the outer edges?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    how would I program it to just print the outer edges?
    Control the values of x and y to place the images where you want them drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    So does that mean there is no way of making the box of 17x17 images 'hollow' through looping? Looping was what I was aiming for instead of manually printing out similar codes for ages.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    You should be able to use loops. There are two rows and two columns
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    How would I go about doing that? I am having issues as to how I can do that using as little code as possible, thanks for your help so far.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    Draw the grid of images. Write down the x,y value for each of the 4 sides and look at how the x,y values change for each of the 4 areas you are putting images into.

    The easiest solution looks like 3 to 4 loops
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    I have done what you said and tried changing the values of 'currentX' and 'currentY' within the new loops, but nothing is working. What should the loops consist of?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    There are 4 areas to put the images in: two rows and two columns
    To put images in a row, change the x value, leave the y value unchanged.
    To put images in a column, change the y value, leave the x value unchanged.

    The loops could be doubled up to do two rows (or two columns) at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    Ok, so now I have manged to create a 'L' shape using 2 loops, half way done now. However I am having slight troubles creating the other column and row. Here is the code so far, a few alterations have been made:

    private void drawMaze(Graphics2D g){
        	Graphics2D g2 = (Graphics2D)g;
     
                int length = 16; 
                int xOffset = 21; 
                int yOffset = 21; 
                int currentX = 10; 
                int currentY = 10; 
                     for(int j = 0; j < length; j++) {
                           g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                           currentY += yOffset;
                           }
                     for(int k = 0; k < length; k++) {
                    	   g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                    	   currentX += xOffset;
                    	   length = 27;
                           }
                }

    I tried to make the 3rd loop for my second column, though when I set the value of Current X within my loop to 540, it only printed out one image in my second column. Why is this?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    when I set the value of Current X within my loop to 540, it only printed out one image in my second column
    Can you post the code did that?
    Try debugging the code by adding printlns that print the values of x and y where the images are being drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    I do not recall the exact code as it was deleted; here is some of it though.
    for(int l = 0; l < length; l++) {
                    	   g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
    What should I do for this loop? Every variable I change ends up ruining the columns and rows.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    What prints out when you add the println to print the x,y locations for each of the images you are drawing?

    I assume that the x,y locations are the problem you are having, so if you print them out we can see where the problem is.
    Last edited by Norm; June 3rd, 2012 at 09:26 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    It prints out exactly the same thing, only one image on the column.

     for(int l = 0; l < length; l++) {
                    	   g2.drawImage(cobblestone.getImage(), 560, 10,this);
                    	}
                     }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    Please post the print outs that shows the x and y values of where the image is being draw and the code that generated the output.

    For testing you should reduce the width and height to a smaller value like 5 or 7 vs 17.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    I thought the x and y values of where the images were being drawn were in the currentX and currentY variables?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    Look at the code to get the names of the variables. You are probably correct with their names.

    Please post the print outs that shows the x and y values of where the image is being draw and the code that generated the output.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

     int length = 5; 
                int xOffset = 21; 
                int yOffset = 21; 
                int currentX = 10; 
                int currentY = 10; 
                     for(int j = 0; j < length; j++) {
                           g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                           currentY += yOffset;
                           }
                     for(int k = 0; k < length; k++) {
                    	   g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                    	   currentX += xOffset;
                    	   length = 7;
                     for(int l = 0; l < length; l++) {
                    	   g2.drawImage(cobblestone.getImage(), 560, 10,this);
                     }
                     }
        }

    My currentX and currentY variables determine the x and y positioning. The variable Length is just the number of images printed.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    Where is the print out of the values of the x and y locations where you are drawing the images?

    I don't see any println statements in the code you posted. Add some println statements to print out the values of where each image is being drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    I don't understand what you mean by println statements? Aren't those only for strings? Sorry, i'm still getting used to java!

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    To get debug output so you can see what the code is doing, print the values of the variables using:
    System.out.println("var=" + var + ", anotherVar=" 
                  + anotherVar + ", andMore=" + andMore);
    replace var, anotherVar and andMore with the names of the variables in your program
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    Quote Originally Posted by Norm View Post
    To get debug output so you can see what the code is doing, print the values of the variables using:
    System.out.println("var=" + var + ", anotherVar=" 
                  + andMore);
    I am a bit confused as to what is going on here, what are all the '=' and quotes at different places for?
    Last edited by Montario; June 3rd, 2012 at 10:27 AM.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image looping issue

    The = are part of the String to be printed. The above would print out something like this:
    var=234, anotherVar=34, andMore=333


    The "s define Strings. The +s concatenate all the Strings into a single String which is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image looping issue

    I get an infinite pattern of this:

    currentX= 31, currentY = 115, xOffset = 21yOffset = 21 and length = 7
    currentX= 52, currentY = 115, xOffset = 21yOffset = 21 and length = 7
    currentX= 73, currentY = 115, xOffset = 21yOffset = 21 and length = 7
    currentX= 94, currentY = 115, xOffset = 21yOffset = 21 and length = 7
    currentX= 115, currentY = 115, xOffset = 21yOffset = 21 and length = 7
    currentX= 136, currentY = 115, xOffset = 21yOffset = 21 and length = 7
    currentX= 157, currentY = 115, xOffset = 21yOffset = 21 and length = 7

    This is the code which executed this:
    System.out.println("currentX= " + currentX + ", currentY = " + currentY +
                     		", xOffset = " + xOffset + "yOffset = " + yOffset + " and length = " + length )

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: February 26th, 2012, 12:57 PM
  2. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 11:34 PM
  3. Re: Help with a looping issue for homework
    By miss doudy in forum Loops & Control Statements
    Replies: 2
    Last Post: December 14th, 2010, 04:07 PM
  4. Help with a looping issue for homework
    By constancez in forum Loops & Control Statements
    Replies: 5
    Last Post: September 20th, 2010, 07:32 AM
  5. Image transparency issue
    By Brt93yoda in forum Java Theory & Questions
    Replies: 6
    Last Post: August 31st, 2010, 02:25 PM