How to create Geometric shapes in JavaFX 2.0?

16,561

Solution 1

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

Solution 2

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);

     }

}

Share:
16,561
Valentin Vrinceanu
Author by

Valentin Vrinceanu

PHP Junior Developer and Senior Web Designer. JavaSE and JavaFX Junior Developer!

Updated on June 07, 2022

Comments

  • Valentin Vrinceanu
    Valentin Vrinceanu about 2 years

    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?

    I would really appreciate any help.

    Thanks a lot!