我正在开发一个项目,我必须在Applet中间绘制一个Star,这是我一直在处理的代码:
int[] xPoints = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int[] yPoints = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
Graphics2D g2d = ( Graphics2D ) g;
GeneralPath star = new GeneralPath();
star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
for ( int count = 1; count < xPoints.length; count++ )
star.lineTo( xPoints[ count ], yPoints[ count ] );
star.closePath();
g2d.setColor(color);
g2d.fill(star);
它在Applet的左侧绘制一个Star,我无法修改那些x,y点使其成为中心。这些点也不稳定,它们不会形成稳定的恒星。如果有人可以提供帮助,我会很高兴。
答案 0 :(得分:1)
要更改星形的位置,必须创建每个点的偏移量。
int centerX = 0;
int centerY = 0;
通过使用这些,您需要通过添加偏移量来更新点的位置:
star.moveTo(xPoints[0] + centerX, yPoints[0] + centerY);
和
star.lineTo(xPoints[count] + centerX, yPoints[count] + centerY);
最终代码为:
int centerX = 0;
int centerY = 0;
int[] xPoints = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int[] yPoints = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
Graphics2D g2d = (Graphics2D) g;
GeneralPath star = new GeneralPath();
star.moveTo(xPoints[0] + centerX, yPoints[0] + centerY);
for (int count = 1; count < xPoints.length; count++)
star.lineTo(xPoints[count] + centerX, yPoints[count] + centerY);
star.closePath();
g2d.setColor(color);
g2d.fill(star);