Java 尝试使用GraphicsContext方法Public void fill和strokepolygon绘制正多边形

Java 尝试使用GraphicsContext方法Public void fill和strokepolygon绘制正多边形,java,polygon,graphicscontext,Java,Polygon,Graphicscontext,为了使用java中的fillPolygon和strokePolygon方法绘制正六边形,我创建了一个draw方法。目前,我一直在尝试如何获得绘制正六边形所需的六个x和y坐标。有人能帮我吗? 以下是填充多边形的结构: Public void fillPolygon(double[] xPoints, double[] yPoints, int nPoints) 使用当前设置的“填充绘制”使用给定点填充

为了使用java中的fillPolygon和strokePolygon方法绘制正六边形,我创建了一个draw方法。目前,我一直在尝试如何获得绘制正六边形所需的六个x和y坐标。有人能帮我吗? 以下是填充多边形的结构:

Public void fillPolygon(double[] xPoints,
                        double[] yPoints,
                        int nPoints)
使用当前设置的“填充绘制”使用给定点填充多边形。任何数组的空值都将被忽略,并且不会绘制任何内容。 此方法将受“渲染属性”表中指定的任何全局公用、填充或填充规则属性的影响

参数: xPoints-包含多边形点或null的x坐标的数组。 yPoints—包含多边形点或空点的y坐标的数组。
nPoints-构成多边形的点数。

我编写了一个类来完成您需要的操作:

public class Coordinate {

    double x;
    double y;

    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }

    private static Coordinate fromPolar(double magnitude, double angle) {
        double flippedAngle = Math.PI/2 - angle;
        return new Coordinate(magnitude * Math.cos(flippedAngle),
                magnitude * Math.sin(flippedAngle));
    }

    public static Coordinate[] regularPolygonCoordinates(int sides, double radius) {
        Coordinate[] r = new Coordinate[sides];
        for (int i = 0 ; i < sides ; i++)
            r[i] = fromPolar(radius, 2.0 * Math.PI * i / sides);
        return r;
    }

    public static void main(String[] args) {
        Coordinate[] hexagon = regularPolygonCoordinates(6, 1);
        for (Coordinate coord : hexagon) {
            System.out.printf("%f,%f\n", coord.x, coord.y);
        }
    }
}
您可以将此代码放入代码中,并在每次需要坐标时调用它,也可以将上述单位六边形的坐标硬编码到代码中,然后按希望六边形具有的半径缩放所有坐标

如果你想做后者,你甚至不需要这个代码。我在网上找到了这个很酷的工具,它可以为你提供任何“agon”的坐标

如果您确实希望直接使用此代码,可以获得两个分开的顶点数组,如下所示:

double[] xPoints = new double[6];
double[] yPoints = new double[6];
for (int i = 0 ; i < 6 ; i++) {
    xPoints[i] = hexagon[i].x;
    yPoints[i] = hexagon[i].y;
}
double[]xPoints=新的双精度[6];
double[]yPoints=新的双精度[6];
对于(int i=0;i<6;i++){
xPoints[i]=六边形[i].x;
点[i]=六边形[i].y;
}

我为您编写了一个类来满足您的需要:

public class Coordinate {

    double x;
    double y;

    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }

    private static Coordinate fromPolar(double magnitude, double angle) {
        double flippedAngle = Math.PI/2 - angle;
        return new Coordinate(magnitude * Math.cos(flippedAngle),
                magnitude * Math.sin(flippedAngle));
    }

    public static Coordinate[] regularPolygonCoordinates(int sides, double radius) {
        Coordinate[] r = new Coordinate[sides];
        for (int i = 0 ; i < sides ; i++)
            r[i] = fromPolar(radius, 2.0 * Math.PI * i / sides);
        return r;
    }

    public static void main(String[] args) {
        Coordinate[] hexagon = regularPolygonCoordinates(6, 1);
        for (Coordinate coord : hexagon) {
            System.out.printf("%f,%f\n", coord.x, coord.y);
        }
    }
}
您可以将此代码放入代码中,并在每次需要坐标时调用它,也可以将上述单位六边形的坐标硬编码到代码中,然后按希望六边形具有的半径缩放所有坐标

如果你想做后者,你甚至不需要这个代码。我在网上找到了这个很酷的工具,它可以为你提供任何“agon”的坐标

如果您确实希望直接使用此代码,可以获得两个分开的顶点数组,如下所示:

double[] xPoints = new double[6];
double[] yPoints = new double[6];
for (int i = 0 ; i < 6 ; i++) {
    xPoints[i] = hexagon[i].x;
    yPoints[i] = hexagon[i].y;
}
double[]xPoints=新的双精度[6];
double[]yPoints=新的双精度[6];
对于(int i=0;i<6;i++){
xPoints[i]=六边形[i].x;
点[i]=六边形[i].y;
}

请回答此问题并格式化代码,使其可识别为代码且易于阅读。绘制正多边形的最简单方法是使用并计算每个点的距离。对于六边形,可以使用角度60、120、180、240、300。及360(0)。你可以使用你想要的任何长度(半径)。请回答这个问题并格式化代码,使其可以识别为代码并易于阅读。绘制正多边形的最简单方法是使用并计算每个点的半径。对于六边形,可以使用角度60、120、180、240、300。及360(0)。您可以使用任意长度(半径)。