0

I'm trying to use a loop but don't know how to make it work within a loop. Here's what I have for concentric circles, I would like the same idea but with triangles.

int x = 100;   
int y = 100;   
int width = 100;   
int height = 100;     
do{    
  g.drawOval(x, y, width, height);    
  x = x + 5;     
  y = y + 5;     
width = width - 10;     
height = height - 10; 
  } while (width>0 && height>0);    
user1118321
  • 25,567
  • 4
  • 55
  • 86

1 Answers1

0

First off, do-while loops aren't that common (though they do sometimes have uses) - it's better to be familiar with while and for.

Since this appears to be a homework question, what I'm going to do is show you some code for drawing concentric squares using polygons (which isn't the normal way, you would normally just use g.drawRect():

int width = 200;
int height = 200;
int xMid = width/2;
int yMid = width/2;
while(width > 0 && height > 0) {
    // Draw the square
    int xLeft = xMid - width/2;
    int xRight = xMid + width/2;
    int yTop = yMid - height/2;
    int yBottom = yMid + height/2;
    int[] xPoints = {xLeft, xRight, xRight, xLeft};
    int[] yPoints = {yTop, yTop, yBottom, yBottom};
    int nPoints = 4;
    g.drawPolygon(xPoints, yPoints, nPoints);

    // Change the dimensions
    width -= 20;
    height -= 20;
}

See if you can modify that to draw triangles.

Bertie Wheen
  • 1,215
  • 1
  • 13
  • 20
  • thank you so much, this led me down the path exactly as I needed. Too bad my teacher can't be this helpful his answer to everything is google it. I've been stuck for weeks now. Should I post my fixed code somewhere? – user2223612 Apr 01 '13 at 21:17
  • You don't need to post your code anywhere unless you really want to – Bertie Wheen Apr 01 '13 at 21:38