2017-03-18 276 views
0

我想绘制一个边框,使用setStroke(),stroke()但是由于某些原因,边框根本没有出现。 我需要帮助的代码在drawHexagon()方法内部。绘制边框形状javafx

Here`s我的代码

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

stroke油漆根据当前路径。

fillPolygon不会影响因此保持空的当前路径。

而不是使用stroke的,你可以简单地使用strokePolygon

context.strokePolygon(xPoints, yPoints, 6);