Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Simple loop within a loop not working.

    It's supposed to create a 7(width)x5(height) square grid but it only creates a 1x5 square grid for some reason. Please help.

    Java Code:
    public class Game extends JPanel {
    	
    	private int x;
    	private int y;
    
    	public void paint(Graphics g) {
    		Graphics2D g2d = (Graphics2D) g;
    		
    		while (x < 7){
    			while (y < 5){
    				g2d.setColor(Color.BLACK);
    				g2d.fillRect(x * 75, y * 75, 75, 75);
    				g2d.setColor(Color.GREEN);
    				g2d.fillRect(x * 75, y * 75, 71, 71);
    				
    				y += 1;
    			}
    			
    			x += 1;
    		}
    	}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Simple loop within a loop not working.

    You should reset variable 'y' to its original value after the inner loop is done; otherwise the next time the loop is to be run, it terminates immediately.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Simple loop within a loop not working.

    EDIT: Found the solution. Thanks Jos. Solution below:

    Java Code:
    public class Game extends JPanel {
    		
    		private int x;
    		private int y;
    
    		public void paint(Graphics g) {
    			Graphics2D g2d = (Graphics2D) g;
    			
    			while (x < 7){
    				while (y < 5){
    					g2d.setColor(Color.BLACK);
    					g2d.fillRect(x * 75, y * 75, 75, 75);
    					g2d.setColor(Color.GREEN);
    					g2d.fillRect(x * 75, y * 75, 71, 71);
    					
    					y += 1;
    				}
    
    				y = 0;
    				x += 1;
    			}
    		}
    Last edited by supremegrandruler; 03-21-2014 at 09:24 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Simple loop within a loop not working.

    In the inner loop, the x coordinate is wrong; i.e. don't draw your rectangles at coordinate 'x', but at coordinate '75*x' instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 10
    Last Post: 11-23-2013, 06:47 PM
  2. while loop not working
    By Christa912 in forum New To Java
    Replies: 6
    Last Post: 09-10-2013, 08:17 PM
  3. My while loop is not working, why??
    By esined93 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 01:05 AM
  4. for loop not working?
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 07-24-2012, 02:55 PM
  5. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •