• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another nested loop question...

 
Adam Polak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create an aplet wich will show rows of 5 boxes underneath eachother...I have been trying to do this with a nested loop but somehow the second set of five boxes doesn't start a new row...can someone please help me out with this?
Here is the script:
import java.awt.*;
import java.applet.Applet;
public class Tekenvelden extends Applet {

final int veldBreedte = 30;
final int veldHoogte = 30;
public void paint(Graphics g) {

tekenVeld(g, 10, 10, 15);
}
//---------------------------------------------------------------------------------------
private void tekenVeld(Graphics g, int x, int y, int aantal){

int box= 0;
int row= 0;
int rows= (aantal/ 5);

for (row = 0; row < rows; row++) {

for (box= 0; box < aantal; box++) {
g.drawRect(x+(veldBreedte*box), y, veldBreedte, veldHoogte);
g.drawString(" "+(box+1), x+(veldBreedte*box)+veldBreedte/2, y+veldHoogte/2);

}

y= y + veldHoogte;

}

}

}
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

aantal = 15 so in your inner for loop you are telling it to print 15 boxes per line. Change it to box < 5 and you'll get what you want.
[ December 01, 2002: Message edited by: Marilyn de Queiroz ]
 
Adam Polak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes 3 rows of 5 boxes now, but I have another problem, the numbers in the boxes don't count through. Each row has numbers one 1-5, while row one should have 1-5, row two should have 6-10 and row three should have 10-15.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a separate counter which is incremented inside the second loop. That counter will contain the value you are looking for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic