6 Replies - 1040 Views - Last Post: 06 April 2013 - 07:04 PM Rate Topic: -----

#1 sojo99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-July 12

Need Java Help (for nested loops)

Posted 04 April 2013 - 06:47 PM

I am having difficulty creating the loop for drawing a pyramid of rectangles from top row to the bottom row. Each row has one more rectangle than the last as you go down. The colors of each added rectangle changes from red to black respectively as the pyramid grows down. Currently I created one rectangle and it's stuck on the side of the screen and I need to create ten rows... Any hint will help.


I am posting the portion of my code where I am stuck. I don't know how I place it on top and expand down the pyramid accordingly.

 	public void paint( Graphics g ) 
			{
				
				super.paint(g);	
				int currentX = startX;
				int currentY = startY;
				for (int row = 1; row <baseLength; row++)
				{
					for (int j = 1; j <row;  j++)
						g.setColor(Color.red);
				}
					g.fillRect(currentX, currentY, legoWidth, legoHeight);
						currentY += 20 ;
						currentX -=  50;
				}
			
			
			


Is This A Good Question/Topic? 0
  • +

Replies To: Need Java Help (for nested loops)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Need Java Help (for nested loops)

Posted 04 April 2013 - 07:13 PM

don' really know what you try to do here


  for (int j = 1; j <row;  j++)
              g.setColor(Color.red);


doing a
g.setColor(Color.red);
once before entering the master loop would have done the same thing
Was This Post Helpful? 0
  • +
  • -

#3 sojo99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-July 12

Re: Need Java Help (for nested loops)

Posted 04 April 2013 - 07:32 PM

You're right. I realized that after the fact...
Was This Post Helpful? 0
  • +
  • -

#4 sojo99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-July 12

Re: Need Java Help (for nested loops)

Posted 04 April 2013 - 08:40 PM

Not sure if I am on the right track.. it's not printing like I want it to.
super.paint(g);	
				int currentX = startX;
				int currentY = startY;
			for (int row1 = 1; row1 <baseLength; row1++)
				{
					
					for (int j = 1; j <= row1; j++)
						g.setColor(Color.red);
						
g.fillRect(currentX, currentY, legoWidth, legoHeight);
						currentY += 20;
						currentX -= 25;	
						
			
			for (int row2 = 2; row2 <baseLength; row2++)
				{
					
				
					g.setColor(Color.red);
	g.fillRect(currentX, currentY, legoWidth, legoHeight);
					currentY += 20 ;
					currentX -=  25;
					g.setColor(Color.blue);
	g.fillRoundRect(currentX, currentY, legoWidth, legoHeight);
					currentY += 20;
					currentX += 25;
					
				}



Was This Post Helpful? 0
  • +
  • -

#5 Revanyk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 11

Re: Need Java Help (for nested loops)

Posted 06 April 2013 - 04:32 PM

I'm a bit lost on this one also. Anyone else see the problem?
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Need Java Help (for nested loops)

Posted 06 April 2013 - 07:01 PM

This is your first post with the indentation corrected. It may help you see why you're only printing one rectangle.
public void paint( Graphics g ) {
	super.paint(g);	
	int currentX = startX;
	int currentY = startY;

	for (int row = 1; row <baseLength; row++) {
		for (int j = 1; j <row;  j++) g.setColor(Color.red);
	}

	g.fillRect(currentX, currentY, legoWidth, legoHeight);
	currentY += 20 ;
	currentX -=  50;
}

Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Need Java Help (for nested loops)

Posted 06 April 2013 - 07:04 PM

It actually does not do much... beside setting the Color to RED many times for nothing :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1