好吧,所以我在编写这个程序时遇到了问题。我完成了第一部分,但我不知道如何完成它。我尝试了不同的解决方案和一切,但我仍然没有任何线索。这是我到目前为止所拥有的。我需要做的是将它从四个角落开始。
public void paintComponent( Graphics g )
{
super.paintComponent( g );
int width = getWidth();
int height = getHeight();
int number, x, y, dx, dy;
x = 0;
y = height;
number = 15;
dx = width / number;
dy = height / number;
for ( int i = 1; i < number; i++ )
{
x += dx;
y -= dy;
g.drawLine( 0, 0, x, y );
}
}
答案 0 :(得分:2)
我认为你想画出从每个角落到对角线的15行的扇形。我建议编写一个例程来将一个扇形从一个点绘制到一个任意的线段然后使用它:
drawFan(Graphics g,
int number, // number of fan lines
int x0, int y0, // coordinates of the point
int sx, int sy, // coordinates of the line segment start
int ex, int ey) // coordinates of the line segment end
{
int x = sx,
y = sy,
dx = (ex - sx) / number,
dy = (ey - sy) / number;
for (int i = 1; i < number; ++i) {
x += dx;
y += dy;
g.drawLine(x0, y0, x, y);
}
}
然后,您可以使用每个角和对角线的相应值来调用它。
public void paintComponent( Graphics g )
{
super.paintComponent( g );
int width = getWidth();
int height = getHeight();
drawFan(g, 15, 0, 0, 0, height, width, 0); // top left corner
drawFan(g, 15, 0, height, 0, 0, width, height); // bottom left corner
drawFan(g, 15, width, height, 0, height, width, 0); // bottom right corner
drawFan(g, 15, width, 0, 0, 0, width, height); // top right corner
}
答案 1 :(得分:0)
这是解决方案,我的建议是理解坐标系如何在Java中工作然后它很容易。
public void paintComponent( Graphics g ){
super.paintComponent(g);
int widthX = getWidth();
int heightY = getHeight();
int num, i, j;
num = 15;
i = 0;
j = 15;
while( i != 16 && j != -1 ){
g.drawLine( 0, 0, widthX*i/num, heightY*j/num );
g.drawLine( widthX*i/num, heightY*j/num, widthX, heightY );
g.drawLine( widthX*i/num, heightY*i/num, widthX, 0);
g.drawLine( widthX*j/num, heightY*j/num, 0, heightY);
i++;
j--;
}//end while
}//end method paintComponent