我正在开发一个项目,我必须在小程序中间画一个星星,这是我一直在开发的代码:
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);
它在小程序的左侧绘制了一个星形,我无法修改这些 x,y 点以使其位于中心。而且这些点也不稳定,它们不会画出稳定的星星。如果有人能提供帮助,我会很高兴。
最佳答案
要更改星星的位置,必须为每个点创建偏移量。
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);
关于java - 使用 JApplet 绘制星星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36666123/