在形状javafx附近的图画边界

时间:2017-03-18 13:15:13

标签: java javafx

我正在尝试使用setStroke(),stroke()在我的形状周围画一个边框,但由于某种原因,边框根本没有显示出来。 我需要帮助的代码在方法drawHexagon()。

这是我的代码

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Arrays;

/**
 * Created by Robin on 18.03.2017.
 */
public class Gui extends Application{


    private Canvas canvas;
    private Group mainLayout;

    public Gui(){
        canvas = new Canvas(800,600);
        mainLayout = new Group();
        mainLayout.getChildren().add(canvas);
        GraphicsContext context =canvas.getGraphicsContext2D();

        drawHexagon(new double[]{250,250},50,Color.GREEN,context);

    }

    public void drawHexagon(double[] centerPoint,double size,Color color,GraphicsContext context){


        context.setFill(color);
        context.setStroke(Color.BLACK);
        double[][]myHexa =getHexagon(centerPoint,size);

        double[]xPoints = new double[]{myHexa[0][0],myHexa[1][0],myHexa[2][0],myHexa[3][0],myHexa[4][0],myHexa[5][0]};
        double[]yPoints =new double[]{myHexa[0][1],myHexa[1][1],myHexa[2][1],myHexa[3][1],myHexa[4][1],myHexa[5][1]};

        context.fillPolygon(xPoints,yPoints,6);
        context.stroke();
    }

    private static double[][]getHexagon(double[] centerPoint,double size){
        double[][]points = new double[6][2];
        for(int i=0;i<6;i++){
            double angle =degreeToRad(60*i+30);
            double x =(centerPoint[0]+size*Math.cos(angle));
            double y =(centerPoint[1]+size*Math.sin(angle));
            points[i]=new double[]{x,y};
        }
        return points;
    }

    private static double degreeToRad(double degree){
        return (Math.PI/180)*degree;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(mainLayout,800,600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[]args){
    launch(args);
    }
}

我可能在那里犯了一个非常愚蠢的错误,但我无法弄清楚为什么这对我不起作用。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

stroke根据当前的路径进行绘画。

fillPolygon不影响当前路径,因此保持为空。

您可以简单地使用stroke

,而不是使用strokePolygon
context.strokePolygon(xPoints, yPoints, 6);
相关问题