Let's say I have some shapes like:
Shape s1 = new Rectangle(10, 10);
Shape s2 = new Circle(10);
etc. I would like to draw them on canvas. In Swing it was possible through Graphics2D.draw(Shape shape) method, but I don't see equivalent in JavaFX GraphicsContext. Is there something like this in JavaFX?
Im not entirely sure its possible to draw the objects directly onto the canvas in the way you would expect, it is to draw it directly onto the stage node, such as the example from here :
package javafx8_shape;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
    /**
     *
     * @web java-buddy.blogspot.com
     */
    public class JavaFX8_Shape extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            Group root = new Group();
            Scene scene = new Scene(root, 500, 500, Color.BLACK);
    
            //Filled rectangle
            Rectangle rect1 = new Rectangle(10, 10, 200, 200);
            rect1.setFill(Color.BLUE);
            
            //Transparent rectangle with Stroke
            Rectangle rect2 = new Rectangle(60, 60, 200, 200);
            rect2.setFill(Color.TRANSPARENT);
            rect2.setStroke(Color.RED);
            rect2.setStrokeWidth(10);
            
            //Rectangle with Stroke, no Fill color specified
            Rectangle rect3 = new Rectangle(110, 110, 200, 200);
            rect3.setStroke(Color.GREEN);
            rect3.setStrokeWidth(10);
            
            root.getChildren().addAll(rect1, rect2, rect3);
    
            primaryStage.setTitle("java-buddy.blogspot.com");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
    }
however the canvas API is generally the way in which you draw the shapes, rectangle etc through method calls. So to recap you can even draw rectangle objects onto other nodes such as a HBOX:
HBox root =  new HBox(rectangle);  
But drawing it onto the canvas is usually done like this:
         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);
The best alternative would be to develop methods, which you pass your objects to and then use the API to draw using the dimensions of your object onto the canvas...
private void drawRectangle(GraphicsContext gc,Rectangle rect){
   gc.setFill(Color.WHITESMOKE);
   gc.fillRect(rect.getX(),      
               rect.getY(), 
               rect.getWidth(), 
               rect.getHeight());
   gc.setFill(Color.GREEN);
   gc.setStroke(Color.BLUE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With