【发布时间】:2016-11-17 19:08:22
【问题描述】:
我在搞清楚三件事时遇到了问题。 (使用绘图面板创建:http://www.buildingjavaprograms.com/DrawingPanel.java)
问题 #1:绘制多边形,使其居中且不弯曲。绘制更多点时不会引起注意。
问题 #2:将星星的所有点连接在一起,形成一个巨大的圆圈(虚线)。我不明白为什么会这样,除非方法不是最好的。
问题 #3:用少量点绘制时,我注意到它没有正确绘制点,而且它看起来像一个正方形。
非常感谢您的帮助!
import java.awt.*;
public class StarSampler {
public static void main(String[] args)
{
DrawingPanel panel = new DrawingPanel(500, 500);
Graphics2D g = panel.getGraphics();
g.setColor(Color.BLUE);
fillStar(g, 250, 250, 150, 5, 1);
}
public static void fillStar(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints, double spikiness)
{
double xDouble[] = new double[2*nPoints];
double yDouble[] = new double[2*nPoints];
int xPoint[] = new int[100];
int yPoint[] = new int[100];
for (int i = 0; i < 2*nPoints; i++)
{
double iRadius = (i % 2 == 0) ? radius : (radius * spikiness);
double angle = (i * 720.0) / (2*nPoints);
xDouble[i] = ctrX + iRadius * Math.cos(Math.toRadians(angle));
yDouble[i] = ctrY + iRadius * Math.sin(Math.toRadians(angle));
for (int j = 0; j < nPoints; j++) // Casts for ints and doubles
{
xPoint[j] = (int) xDouble[j];
yPoint[j] = (int) yDouble[j];
}
}
g.fillPolygon(xPoint, yPoint, nPoints); // Creates polygon
// Polygon gets drawn crookedly
g.drawPolyline(xPoint, yPoint, nPoints); // Draws lines to connect points
// Two lines go straight to (0,0) when nPonts*2 and nothing without *2?
}
}
我的输出:
我的目标输出(没有标记点,例如两颗星):
【问题讨论】:
标签: java loops drawing polygon