Shapes Intersection, Subtract and Union in Javafx 2.0

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)

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.

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





Circle c2 = new Circle();





Circle c3 = new Circle();





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



return scene;


Shapes Intersect

Feel free to comment.

Have a 🙂 good day!

8 thoughts on “Shapes Intersection, Subtract and Union in Javafx 2.0”

  1. Narayan Gopal Maharjan

    Yes you can easily union the Rectangle and Line as Shape is the parent class of both of them.

  2. I see we can do boolean operations on shapes. Still, are there official ways to only test if two shapes intersect?
    I guess it might be possible by firstly creating an intersection shape then check to see if it’s empty; but i would question the efficiency of such “workaround”.
    Your idea?

  3. Narayan Gopal Maharjan

    I think you are trying to figure out if the shapes does intersects(overlaps) or not? If you are think of that feature then I think you probably need to see ‘blockInMouse’ property of JavaFX which I’ve already blogged about at here.

  4. hey hi..
    How can we position shapes inside a pane??
    If i use it directly in scene it works
    Circle c=new Circle(10,10,15);
    Group g=new Group(c);
    Scene s=new Scene(g,800,600);
    ((This positons circle at (10 10)))

    Circle c=new Circle(10,10,15);
    Group g=new Group(c);
    BorderPane b=new BorderPane();
    Scene s=new Scene(b,800,600);
    ((This doesn’t work))

    Any ideas??

  5. Hi vini,
    The position you gave doesn’t work for the BorderPane because BorderPane is different than Group. The BorderPane automatically resize the children according to it’s master location like center,top,bottom,left,right. Currently you have placed the circle inside Group and that Group is kept in the Center of BorderPane so the Group is then resized and layouts at exactly center.
    If you want to always display your circle just (x,y) 10px,10px of your BorderPane center point then you need to use the translate x,y cordinates.

    Circle c = new Circle(10,10,20);
    Group g = new Group(c);
    BorderPane b= new BorderPane();
    Scene scene = new Scene(pane);

    FYI (Layout of JavaFX):


Leave a Reply