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 6 of 6

Thread: Help with simple applet.

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with simple applet.

    All the applet does is print a brick wall. I've got it made with using for loops, but now I'd like to get it done using while and do while loops, but I can't seem to get it working using while loops, and I haven't even attempted doing it using do while loops. Any suggestions? All it does is print the first column for some reason.

    This is my fully working "for loop" version:

    package ton_of_bricks;
     
    import java.applet.Applet;
    import java.awt.*;
     
    public class Ton_of_bricks_for extends Applet{
     
       private static final long serialVersionUID = 1L;
     
       public static final int START_X = 0;
       public static final int START_Y = 0;
       public static final int BRICK_WIDTH = 60;
       public static final int BRICK_HEIGHT = 30;
       public static final int DISPLAY_WIDTH = 600;
       public static final int DISPLAY_HEIGHT = 600;
       public static final int GAP = 10;
     
       public void init(){
    	   resize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
    	   setBackground(Color.darkGray);
       }
     
       public void paint(Graphics g){
     
       	   int x = START_X;
       	   int y = START_Y;
     
       	   g.setColor(Color.RED); 
     
       	   for (int row = 1; row <= DISPLAY_WIDTH; row++){ //while row is less than windows width, increase rows
     
       			for (int col = 1; col <= DISPLAY_HEIGHT; col++){ //while col is less than windows height, increase cols 
     
       				g.fillRect (x, y, BRICK_WIDTH, BRICK_HEIGHT); //drawing bricks
     
       				x += BRICK_WIDTH + GAP; //generates columns
       			}
     
       			if (row%2 == 0)
       				x = START_X;
       			else
       				x = START_X - BRICK_WIDTH/2 - GAP/2; //offsets bricks  
     
       				y += BRICK_HEIGHT + GAP; //generates rows
     
       	   } //end for(rows)
       } //end pain
    } //public class ton_of_bricks_for

    This is my broken "while loop" version, where only the first column is working:

    package ton_of_bricks;
     
    import java.applet.Applet;
    import java.awt.*;
     
    public class Ton_of_bricks_while extends Applet{
     
       private static final long serialVersionUID = 1L;
     
       public static final int START_X = 0;
       public static final int START_Y = 0;
       public static final int BRICK_WIDTH = 30;
       public static final int BRICK_HEIGHT = 15;
       public static final int DISPLAY_WIDTH = 600;
       public static final int DISPLAY_HEIGHT = 600;
       public static final int GAP = 5;
     
       public void init(){
    	   resize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
    	   setBackground(Color.darkGray);
       }	
     
       public void paint(Graphics g){
     
       	   int x = START_X;
       	   int y = START_Y;
     
       	   g.setColor(Color.RED); 
     
       	   int row = 1;
       	   int col = 1;
     
       	   while (row <= DISPLAY_WIDTH){ //while row is less than windows width, increase rows
       		   row++;
     
       	   		while (col <= DISPLAY_HEIGHT) //while col is less than windows height, increase cols 
       	   			col++;         	 
     
       	   			g.fillRect (x, y, BRICK_WIDTH, BRICK_HEIGHT); //drawing bricks
     
       				x += BRICK_WIDTH + GAP; //generates columns
     
     
       			if (row%2 == 0)
       				x = START_X;
       			else
       				x = START_X - BRICK_WIDTH/2 - GAP/2; //offsets bricks  
     
       				y += BRICK_HEIGHT + GAP; //generates rows
     
       	   } //end for(rows)
       } //end pain
    }//public class ton_of_bricks_for

    Any suggestions on fixing the problem? I would also appreciate it if anyone could point me in the right direction of starting the "do while" version as well. Thank you!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with simple applet.

    After the inner (col) loop finishes what value will col have? Will it ever be less than DISPLAY_HEIGHT again?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple applet.

    Sorry I forgot a closing brace for the while. Only the first row is displayed, rather than first column. Still can't figure it out

    while (row <= DISPLAY_WIDTH){ //while row is less than windows width, increase rows
       		   row++;
     
       	   		while (col <= DISPLAY_HEIGHT) //while col is less than windows height, increase cols 
       	   			col++;         	 
     
       	   			g.fillRect (x, y, BRICK_WIDTH, BRICK_HEIGHT); //drawing bricks
     
       				x += BRICK_WIDTH + GAP; //generates columns
       	   }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with simple applet.

    Did you read my post?
    Did you answer my questions?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple applet.

    Quote Originally Posted by Junky View Post
    Did you read my post?
    Did you answer my questions?
    Well, it will have the value of +1 since it's incrementing, right? and it's supposed to loop only if it's less than the height, so I would hope it would be less again. Sorry, really bad at while loops

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with simple applet.

    Why would it be less again? Values of variables do not magically change. Run this code and see if you can understand the problem.
    int max = 5;
    int row = 0;
    int col = 0;
    while(row < max) {
            while(col < max) {
                System.out.print("*");
                col++;
            }
            System.out.println();
            System.out.println(col);
            system.out.print("Is col less than max: ");
            System.out.println(col < max);
    }
    How many rows get printed?
    Improving the world one idiot at a time!

Similar Threads

  1. Simple Balloon Applet [Help!!!]
    By GMPoison in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2013, 06:46 PM
  2. Replies: 3
    Last Post: July 3rd, 2013, 08:25 AM
  3. [SOLVED] Simple applet, but wrong
    By infamous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2013, 06:38 PM
  4. Simple Applet Troubles
    By Veldimare in forum Java Applets
    Replies: 7
    Last Post: September 22nd, 2012, 05:19 PM
  5. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM