使用Java FX绘制具有x和y轴的多边形

时间:2018-12-07 18:49:56

标签: java javafx graph polygon stage

我正在尝试使用Java FX绘制带有x和y轴的多边形。我花了12个小时在网上搜索,没有运气!!! :(

I want to draw using Java FX something like this

多边形

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Polygon; 
import javafx.stage.Stage;  

public class PolygonExample extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Creating a Polygon 
      Polygon polygon = new Polygon();  

      //Adding coordinates to the polygon 
      polygon.getPoints().addAll(new Double[]{ 
         300.0, 50.0, 
         450.0, 150.0, 
         300.0, 250.0, 
         150.0, 150.0, 
      }); 

      //Creating a Group object  
      Group root = new Group(polygon); 

      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  

      //Setting title to the Stage 
      stage.setTitle("Drawing a Polygon"); 

      //Adding scene to the stage 
      stage.setScene(scene); 

      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

我有这个但没有轴!

2 个答案:

答案 0 :(得分:0)

您可以通过将polygon.getPoints().addAll()语句替换为..

来绘制多边形。
//Adding coordinates to the polygon 
polygon.getPoints().addAll(new Double[]{ 
      100.0, 250.0,
      200.0, 100.0,
      400.0, 100.0,
      500.0, 250.0,
});

您的分数由于某种原因而被关闭,仅此而已。

尝试思考如何手工绘制。我只是挑选了最左边的点,并按照我在一张纸上用手画的顺序来添加点。

由以下代码显示:

enter image description here

答案 1 :(得分:0)

我们一直在使用Emxsys的“ JavaFX Chart Extensions”软件包。

从以下位置下载jfxchartext-1.1.ziphttps://bitbucket.org/emxsys/javafx-chart-extensions/downloads/

它包含jar文件jfxchartext-1.1.jar,您可以将其包含在项目中。

然后您可以使用其增强的图表之一-如EnhancedScatterChart。 您可以添加一个XYPolygonAnnotation并带有示例代码中所示的多边形坐标(请参见下面的链接-向下滚动至多边形注释部分。

https://bitbucket.org/emxsys/javafx-chart-extensions/wiki/XYAnnotation%20Extension

有关点坐标,您可以使用XYTextAnnotation-请参阅页面底部。