java 如何在 JavaFX 2.0 中创建几何形状?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10315252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:31:39  来源:igfitidea点击:

How to create Geometric shapes in JavaFX 2.0?

javashapegeometryjavafx-2

提问by Valentin Vrinceanu

I have a project i am working on JavaFX 2.0, and it is a Drawing Application. I created so far a Pen, and a pen size slider, color picker, eraser and Undo functions. I do not know yet how to create basic Shapes like Rectangle, Circles or Polygons. The shapes must hape custom dimension and i need to draw them into my scene. Can anyone help me out?

我有一个项目正在开发 JavaFX 2.0,它是一个绘图应用程序。到目前为止,我创建了一个 Pen、一个笔大小滑块、颜色选择器、橡皮擦和撤消功能。我还不知道如何创建基本形状,如矩形、圆形或多边形。形状必须具有自定义尺寸,我需要将它们绘制到我的场景中。谁能帮我吗?

I would really appreciate any help.

我真的很感激任何帮助。

Thanks a lot!

非常感谢!

采纳答案by Uluk Biy

Check out the API Docs: javafx.scene.shape.Shape.
Sample usage: Draw Rectangle. Circle and Line examples also exist there.

查看 API 文档:javafx.scene.shape.Shape
示例用法:绘制矩形。Circle 和 Line 示例也存在于此。

回答by valdow valdowb

public class MyCanvas extends Application {

    @Override
    public void start(Stage primaryStage) {
         primaryStage.setTitle(MyCanvas.class.getSimpleName());
         Group root = new Group();
         final Canvas canvas = new Canvas(300, 250);
         GraphicsContext gc = canvas.getGraphicsContext2D();
         drawShapes(gc);
         final Text text = new Text("X =    Y =   ");
         text.setTranslateX(100);
         text.setTranslateY(40);
         text.setFont(new Font(20));
         canvas.setOnMouseMoved(new EventHandler<MouseEvent>() {

             @Override
             public void handle(MouseEvent t) {
                 text.setText("X = " + t.getX() + "  Y = " + t.getY());
             }
         });

         root.getChildren().addAll(canvas, text);
         primaryStage.setScene(new Scene(root));
         primaryStage.getScene().setFill(Color.AQUA);
         primaryStage.show();

     }

     /**
      * The main() method is ignored in correctly deployed JavaFX application.
      * main() serves only as fallback in case the application can not be
      * launched through deployment artifacts, e.g., in IDEs with limited FX
      * support. NetBeans ignores main().
      *
      * @param args the command line arguments
      */
     public static void main(String[] args) {
         launch(args);
     }

     private void drawShapes(GraphicsContext gc) {
         gc.setFill(Color.WHITESMOKE);
         gc.fillRect(gc.getCanvas().getLayoutX(),      
                     gc.getCanvas().getLayoutY(), 
                     gc.getCanvas().getWidth(), 
                     gc.getCanvas().getHeight());
         gc.setFill(Color.GREEN);
         gc.setStroke(Color.BLUE);

         gc.setLineWidth(5);
         gc.strokeLine(40, 10, 10, 40);
         gc.fillOval(10, 60, 30, 30);
         gc.strokeOval(60, 60, 30, 30);
         gc.fillRoundRect(110, 60, 30, 30, 10, 10);
         gc.strokeRoundRect(160, 60, 30, 30, 10, 10);
         gc.fillArc(10, 110, 30, 30, 45, 240, ArcType.OPEN);
         gc.fillArc(60, 110, 30, 30, 45, 240, ArcType.CHORD);
         gc.strokeArc(10, 160, 30, 30, 45, 240, ArcType.OPEN);

     }

}

}

回答by Andres Perez

You will need to get the graphic component of the component where you want to draw in.

您将需要获取要绘制的组件的图形组件。

if you have a panel it will be something like:

如果你有一个面板,它会是这样的:

 Graphics g = jPanel1.getGraphics();
 Graphics2D g2d = (Graphics2D)g;

Graphics2D offers you all the methods to draw what you are looking for. For a list off the completes methods check the docs at oracle:

Graphics2D 为您提供了绘制所需内容的所有方法。有关完整方法的列表,请查看 oracle 上的文档:

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

Edit: Confused JavaSE. For JavaFX you can read a little how-to here: http://docs.oracle.com/javafx/1.3/howto/Shapes-Tutorial.html

编辑:困惑的 JavaSE。对于 JavaFX,您可以在此处阅读一些操作方法:http: //docs.oracle.com/javafx/1.3/howto/Shapes-Tutorial.html