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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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; } |
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}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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; } |
//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;
}
Feel free to comment.
Have a 🙂 good day!
Can we union rectangle and line using union function of the javafx
Yes you can easily union the Rectangle and Line as Shape is the parent class of both of them.
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?
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.
Do u mean this?
https://blog.ngopal.com.np/2011/07/29/where-is-blockinmouse-property-in-javafx-2-0/
It seems unrelated.
All i want is an efficient and lightweight way to test if two shapes overlap/intersect. Currently what i can do is to create an intersection shape and check if that shape is empty. Worse, prior to this step, you have to convert the shape into an ancestor’s coordinate system. These make the simple overlap testing very expensive.
hey hi..
How can we position shapes inside a pane??
If i use it directly in scene it works
eg:
Circle c=new Circle(10,10,15);
Group g=new Group(c);
Scene s=new Scene(g,800,600);
stage.setScene(s);
stage.show();
((This positons circle at (10 10)))
Eg2:
Circle c=new Circle(10,10,15);
Group g=new Group(c);
BorderPane b=new BorderPane();
b.setCenter(g);
Scene s=new Scene(b,800,600);
stage.setScene(s);
stage.show();
((This doesn’t work))
Any ideas??
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);
g.setTranslateX(10);
g.setTranslateY(10);
BorderPane b= new BorderPane();
b.setCenter(g);
Scene scene = new Scene(pane);
FYI (Layout of JavaFX):http://www.oracle.com/technetwork/articles/java/layoutfx-1536156.html
Thanks
Narayan
Can you union text aswell