【问题标题】:Making nested polygons制作嵌套多边形
【发布时间】:2013-03-29 08:02:26
【问题描述】:

我正在尝试使用循环,但不知道如何使其在循环中工作。这就是我对同心圆的看法,我想要同样的想法,但有三角形。

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);    

【问题讨论】:

  • x = x 5; y = y 5; 运算符丢失??在 x 和 5 之间?
  • 这可能是help
  • 谢谢!但我应该提到我知道如何绘制多边形。我在更改值时遇到问题,因此它们会在彼此之间变小,并使用类似于示例的循环。

标签: java loops geometry do-while


【解决方案1】:

首先,do-while 循环并不常见(尽管它们有时确实有用)- 最好熟悉 whilefor

由于这似乎是一个家庭作业问题,我要做的是向您展示一些使用多边形绘制同心正方形的代码(这不是正常的方式,您通常只使用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;
}

看看你是否可以修改它来绘制三角形。

【讨论】:

  • 非常感谢,这使我完全按照我的需要走上了这条路。太糟糕了,我的老师不能这么有帮助,他对所有事情的回答都是谷歌。我已经被困了好几个星期了。我应该在某处发布我的固定代码吗?
  • 你不需要在任何地方发布你的代码,除非你真的想要
猜你喜欢
  • 1970-01-01
  • 2013-02-02
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2014-02-28
  • 2011-07-20
  • 1970-01-01
相关资源
最近更新 更多