2017-03-18 275 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);