How to create geometric shapes in JavaFX 2.0?

I have a project that I am working on JavaFX 2.0, and this is a drawing application. So far I have created the Pen and pen size slider, color picker, eraser and Undo. I don’t know yet how to create basic shapes like Rectangle, Circles or Polygons. The shapes should fit in a custom size, and I need to draw them in my scene. Can anyone help me out?

I would really appreciate any help.

Thank you so much!

+3
source share
3 answers

API: javafx.scene.shape.Shape.
: . Circle Line.

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

     }

}

+1

You will need to get the graphic component of the component you want to paste into.

if you have a panel, it will be something like:

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

Graphics2D offers you all the ways to draw what you are looking for. For a list of methods, it finishes checking documents in oracle:

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

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

0
source

All Articles