Hello user, Javafx 2.0 has more efficient code because it has mother language of Java. Previous javafx was script language which has very easier coding steps but that script language has more performance issue so Oracle has planned to move it on Java as Swing 2.0. Ok now let’s move toward the new shapes feature of javafx 2.0 (in context of beta release).

 

Shapes Union

To make some kind of shapes union you can easily make them from the help of Shape and Path class. So lets have a small demo of Union in javafx. Math Sets: It is some thing like A={1,2,3,4} B={3,4,5,6} then if we use AUB = {1,2,3,4,5,6}. We ‘ll use the union between shapes from this function union(shape,shape)

 public Scene getUI(){
        //Main panel
        Group panel = new Group();

        //Main scene
        Scene scene = new Scene(panel,300,300);

        //Creating Three Circles Shapes
        Circle c1 = new Circle();
        c1.setRadius(50d);
        c1.setCenterX(100d);
        c1.setCenterY(70d);

        Circle c2 = new Circle();
        c2.setRadius(50d);
        c2.setCenterX(70d);
        c2.setCenterY(105d);

        Circle c3 = new Circle();
        c3.setRadius(50d);
        c3.setCenterX(140d);
        c3.setCenterY(105d);

        //union function which combines any two shapes
        Path path = Path.union(c1, c2);
        //again combile same path with shpapes
        //because Path is subclass of Shape as well
        path = Path.union(path, c3);
        path.setFill(Color.YELLOW);

        panel.getChildren().addAll(path);
        return scene;
    }

Union Output

 

 

Shapes Subtract

Now the Subtract shape is also some how same like of Union. It removes the part of any one of Shapes completely using function subtract(shape,shape). It complies the rule of sets. Like A={1,2,3,4} B={3,4,5,6} then if we use A-B = {1,2}. Lets have a look on subtract of Shapes example too.

public Scene getUI(){
        //Main panel
        Group panel = new Group();

        //Main scene
        Scene scene = new Scene(panel,300,300);

        //Creating Three Circles Shapes
        Circle c1 = new Circle();
        c1.setRadius(50d);
        c1.setCenterX(100d);
        c1.setCenterY(70d);

        Circle c2 = new Circle();
        c2.setRadius(50d);
        c2.setCenterX(70d);
        c2.setCenterY(105d);

        Circle c3 = new Circle();
        c3.setRadius(50d);
        c3.setCenterX(140d);
        c3.setCenterY(105d);

        //union function which combines any two shapes
        Path path = Path.union(c1, c2);
        //Now subract same path with c3 Circle
        //because Path is subclass of Shape as well
        path = Path.subtract(path, c3);
        path.setFill(Color.RED);

        panel.getChildren().addAll(path);
        return scene;
    }

Shape Subtract

 

Shapes Intersects

The intersects takes the common overlapping of all the circle(shapes). For shapes intersects we can use the intersect(shape,shape) function of Path class. It is some thing like A={1,2,3,4} B={3,4,5,6} then if we use A∩B = {3,4}

public Scene getUI(){
        //Main panel
        Group panel = new Group();

        //Main scene
        Scene scene = new Scene(panel,300,300);

        //Creating Three Circles Shapes
        Circle c1 = new Circle();
        c1.setRadius(50d);
        c1.setCenterX(100d);
        c1.setCenterY(70d);

        Circle c2 = new Circle();
        c2.setRadius(50d);
        c2.setCenterX(70d);
        c2.setCenterY(105d);

        Circle c3 = new Circle();
        c3.setRadius(50d);
        c3.setCenterX(140d);
        c3.setCenterY(105d);

        //intersect function which intersects any two shapes
        Path path = Path.intersect(c1, c2);
        //Now again intersect same path with c3 Circle
        //because Path is subclass of Shape as well
        path = Path.intersect(path, c3);
        path.setFill(Color.RED);

        panel.getChildren().addAll(path);
        return scene;
    }
public Scene getUI(){ 

//Main panel

Group panel = new Group();

//Main scene

Scene scene = new Scene(panel,300,300);

//Creating Three Circles Shapes

Circle c1 = new Circle();

c1.setFill(Color.BLUE);

c1.setRadius(50d);

c1.setCenterX(100d);

c1.setCenterY(70d);

Circle c2 = new Circle();

c2.setFill(Color.RED);

c2.setRadius(50d);

c2.setCenterX(70d);

c2.setCenterY(105d);

Circle c3 = new Circle();

c3.setFill(Color.GREEN);

c3.setRadius(50d);

c3.setCenterX(140d);

c3.setCenterY(105d);

//intersect function which intersects any two shapes

Path path = Path.intersect(c1, c2);

//Now again intersect same path with c3 Circle

//because Path is subclass of Shape as well

path = Path.intersect(path, c3);

path.setFill(Color.RED);

panel.getChildren().addAll(path);

return scene;

}

Shapes Intersect

Feel free to comment.

Have a 🙂 good day!