JavaFX – Material Property

JavaFX – Material Property ”; Previous Next Until now, when we addressed the properties of a JavaFX shape object, it always only included the color of shape fill and type of shape strokes. However, JavaFX also allows you to assign the type of material for a 3D shape. Material of a 3D object is nothing but the type of texture used to create a 3D object. For example, a box in real world can be made of different materials; like a cardboard, a metal, wood, or just any other solid. All of these form the same shape but with a different material. Similarly, JavaFX allows you to choose the type of material used to create a 3D object. Material Property The Material property is of the type Material and it is used to choose the surface of the material of a 3D shape. You can set the material of a 3D shape using the method setMaterial() as follows − cylinder.setMaterial(material); As mentioned above for this method, you need to pass an object of the type Material. The PhongMaterial class of the package javafx.scene.paint is a sub class of this class and provides 7 properties that represent a Phong shaded material. You can apply all these type of materials to the surface of a 3D shape using the setter methods of these properties. Following are the type of materials that are available in JavaFX − bumpMap − This represents a normal map stored as a RGB Image. diffuseMap − This represents a diffuse map. selfIlluminationMap − This represents a self-illumination map of this PhongMaterial. specularMap − This represents a specular map of this PhongMaterial. diffuseColor − This represents a diffuse color of this PhongMaterial. specularColor − This represents a specular color of this PhongMaterial. specularPower − This represents a specular power of this PhongMaterial. By default, the material of a 3-Dimensional shape is a PhongMaterial with a diffuse color of light gray. Example Following is an example which displays various materials on the cylinder. Save this code in a file with the name CylinderMaterials.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.Cylinder; import javafx.stage.Stage; public class CylinderMaterials extends Application { @Override public void start(Stage stage) { //Drawing Cylinder1 Cylinder cylinder1 = new Cylinder(); //Setting the properties of the Cylinder cylinder1.setHeight(130.0f); cylinder1.setRadius(30.0f); //Setting the position of the Cylinder cylinder1.setTranslateX(100); cylinder1.setTranslateY(75); //Preparing the phong material of type bump map PhongMaterial material1 = new PhongMaterial(); material1.setBumpMap(new Image (“http://www.tutorialspoint.com/images/tplogo.gif”)); //Setting the bump map material to Cylinder1 cylinder1.setMaterial(material1); //Drawing Cylinder2 Cylinder cylinder2 = new Cylinder(); //Setting the properties of the Cylinder cylinder2.setHeight(130.0f); cylinder2.setRadius(30.0f); //Setting the position of the Cylinder cylinder2.setTranslateX(200); cylinder2.setTranslateY(75); //Preparing the phong material of type diffuse map PhongMaterial material2 = new PhongMaterial(); material2.setDiffuseMap(new Image (“http://www.tutorialspoint.com/images/tp-logo.gif”)); //Setting the diffuse map material to Cylinder2 cylinder2.setMaterial(material2); //Drawing Cylinder3 Cylinder cylinder3 = new Cylinder(); //Setting the properties of the Cylinder cylinder3.setHeight(130.0f); cylinder3.setRadius(30.0f); //Setting the position of the Cylinder cylinder3.setTranslateX(300); cylinder3.setTranslateY(75); //Preparing the phong material of type Self Illumination Map PhongMaterial material3 = new PhongMaterial(); material3.setSelfIlluminationMap(new Image (“http://www.tutorialspoint.com/images/tp-logo.gif”)); //Setting the Self Illumination Map material to Cylinder3 cylinder3.setMaterial(material3); //Drawing Cylinder4 Cylinder cylinder4 = new Cylinder(); //Setting the properties of the Cylinder cylinder4.setHeight(130.0f); cylinder4.setRadius(30.0f); //Setting the position of the Cylinder cylinder4.setTranslateX(400); cylinder4.setTranslateY(75); //Preparing the phong material of type Specular Map PhongMaterial material4 = new PhongMaterial(); material4.setSpecularMap(new Image (“http://www.tutorialspoint.com/images/tp-logo.gif”)); //Setting the Specular Map material to Cylinder4 cylinder4.setMaterial(material4); //Drawing Cylinder5 Cylinder cylinder5 = new Cylinder(); //Setting the properties of the Cylinder cylinder5.setHeight(130.0f); cylinder5.setRadius(30.0f); //Setting the position of the Cylinder cylinder5.setTranslateX(100); cylinder5.setTranslateY(300); //Preparing the phong material of type diffuse color PhongMaterial material5 = new PhongMaterial(); material5.setDiffuseColor(Color.BLANCHEDALMOND); //Setting the diffuse color material to Cylinder5 cylinder5.setMaterial(material5); //Drawing Cylinder6 Cylinder cylinder6 = new Cylinder(); //Setting the properties of the Cylinder cylinder6.setHeight(130.0f); cylinder6.setRadius(30.0f); //Setting the position of the Cylinder cylinder6.setTranslateX(200); cylinder6.setTranslateY(300); //Preparing the phong material of type specular color PhongMaterial material6 = new PhongMaterial(); //setting the specular color map to the material material6.setSpecularColor(Color.BLANCHEDALMOND); //Setting the specular color material to Cylinder6 cylinder6.setMaterial(material6); //Drawing Cylinder7 Cylinder cylinder7 = new Cylinder(); //Setting the properties of the Cylinder cylinder7.setHeight(130.0f); cylinder7.setRadius(30.0f); //Setting the position of the Cylinder cylinder7.setTranslateX(300); cylinder7.setTranslateY(300); //Preparing the phong material of type Specular Power PhongMaterial material7 = new PhongMaterial(); material7.setSpecularPower(0.1); //Setting the Specular Power material to the Cylinder cylinder7.setMaterial(material7); //Creating a Group object Group root = new Group(cylinder1 ,cylinder2, cylinder3, cylinder4, cylinder5, cylinder6, cylinder7); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting camera PerspectiveCamera camera = new PerspectiveCamera(false); camera.setTranslateX(0); camera.setTranslateY(0); camera.setTranslateZ(-10); scene.setCamera(camera); //Setting title to the Stage stage.setTitle(“Drawing a cylinder”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls CylinderMaterials.java java –module-path %PATH_TO_FX% –add-modules javafx.controls CylinderMaterials Output

JavaFX – Stroke Property

JavaFX – Stroke Property ”; Previous Next The Stroke Fill property is used to change the colours of 2D object itself. However, we can also change the colour of a 2D object”s boundary. In JavaFX, while creating a 2D object, there are only two parts you can work with, if you want to improve the quality of the shape; i.e., the enclosed area of the shape and the boundary of the shape. Hence, the properties provided by JavaFX include designing both these parts. In this chapter, we will learn about the Stroke property in detail. Stroke Property The Stroke property in JavaFX is used to change colours of the shape boundary. This property is of the type Paint and it represents the color of the boundary line of the shape. You can set a value to this property using the method setStroke(). This method is a part of javafx.scene.paint package and takes the Color object as a parameter value as shown below − path.setStroke(Color.RED) By default, the color of the stroke is black. Following is a diagram of a triangle with different stroke colors. Example We will see an example demonstrating how to colour the boundary of a 2D shape in JavaFX. In here, we are drawing a 2D shape, say a rectangle. By default, the rectangle will be coloured black; we will try to change its boundary colour to Violet. Save the file with the name StrokeExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.scene.paint.Color; import javafx.stage.Stage; public class StrokeExample extends Application { @Override public void start(Stage stage) { //Creating a Rectangle Rectangle rectangle = new Rectangle(50.0f, 50.0f, 200.0f, 100.0f); rectangle.setStroke(Color.VIOLET); rectangle.setStrokeWidth(7.0); //Creating a Group object Group root = new Group(rectangle); //Creating a scene object Scene scene = new Scene(root, 300, 300); //Setting title to the Stage stage.setTitle(“Drawing a Rectangle”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls StrokeExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls StrokeExample Output On executing, the above program generates a JavaFX window displaying a rectangle with a stroke coloured violet as shown below. Example In this example, we are drawing a polygon and change its boundary colour to RED. Save the file with the name StrokePolygonExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Polygon; import javafx.scene.shape.Shape; import javafx.scene.paint.Color; import javafx.stage.Stage; public class StrokePolygonExample extends Application { @Override public void start(Stage stage) { //Creating a Polygon Polygon polygon = new Polygon(); //Adding coordinates to the polygon polygon.getPoints().addAll(new Double[]{ 100.0, 50.0, 200.0, 50.0, 250.0, 150.0, 200.0, 250.0, 100.0, 250.0, }); polygon.setStroke(Color.RED); polygon.setStrokeWidth(5.0); //Creating a Group object Group root = new Group(polygon); //Creating a scene object Scene scene = new Scene(root, 300, 300); //Setting title to the Stage stage.setTitle(“Drawing a Polygon”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls StrokePolygonExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls StrokePolygonExample Output On executing, the above program generates a JavaFX window displaying a rectangle with a stroke coloured violet as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Drawing an SVGPath

JavaFX – Drawing an SVGPath ”; Previous Next SVG (Scalable Vector Graphics) is an XML based language to define vector based graphics. The <path> element in the SVG library is the most powerful while drawing basic shapes. Using paths, you can draw lines, curves, arcs, and also various complex shapes including them. Even though a path is similar to the polyline element while creating complex shapes, the scale of complex shapes drawn using a polyline element is not larger than shapes drawn using path element. A path in SVG is defined by only one parameter. This parameter holds series of commands, like line, curve or arc commands. And each of these commands are instantiated using a single letter; for example, the letter ”M” calls the “Move To” command, the letter ”L” calls the “line” command and ”C” calls “Curve” command. And these letters can either be specified as either a lowercase or an uppercase letter. The lowercase letter specifies relative coordinates, while the uppercase letter specifies absolute coordinates. The same concept of SVGPath is adopted by JavaFX, in order to create objects. SVG Path in JavaFX In JavaFX we can construct images by parsing SVG paths. Such shapes are represented by the class named SVGPath. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a node which is created by parsing an SVG path in JavaFX. This class has a property named content of String datatype. This represents the SVG Path encoded string, from which the image should be drawn. To draw a shape by parsing an SVG path, you need to pass values to this property, using the method named setContent() of this class as follows − setContent(value); Steps to Draw SVGPath To Draw a shape by parsing an SVGPath in JavaFX, follow the steps given below. Step 1: Creating an Object of the SVGPath Class You can create a required shape in JavaFX by parsing an SVGPath. To do so, instantiate the class named SVGPath which belongs to a package javafx.scene.shape. You can instantiate this class inside the start() method as follows. public class ClassName extends Application { public void start(Stage primaryStage) throws Exception { //Creating an object of the class SVGPath SVGPath svgpath = new SVGPath(); } } Step 2: Setting the SVGPath Set the content for the SVG object using the method setContent(). To this method, you need to pass the SVGPath. Using which, a shape should be drawn in the form of a string as shown in the following code block. String path = “M 100 100 L 300 100 L 200 300 z”; //Setting the SVGPath in the form of string svgPath.setContent(path); Step 3: Adding SVGPath to Group In the start() method, instantiate the Group class by passing the SVGPath object as a parameter value to its constructor − Group root = new Group(svgpath); Step 4: Launching Application Once the 2D object is created, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is a program which generates a 2D shape constructed with lines by parsing SVG path using JavaFX. Save this code in a file with the name SVGExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.SVGPath; import javafx.stage.Stage; public class SVGExample extends Application { @Override public void start(Stage stage) { //Creating a SVGPath object SVGPath svgPath = new SVGPath(); String path = “M 100 100 L 300 100 L 200 300 z”; //Setting the SVGPath in the form of string svgPath.setContent(path); //Creating a Group object Group root = new Group(svgPath); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a Triangle”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls SVGExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SVGExample Output On executing, the above program generates a JavaFX window displaying a triangle, which is drawn by parsing the SVG path as shown below. Example You can also construct complex shapes from curves and arcs using an SVG path. Following example creates a cubic curve using an SVG Path. Save this code in a file with the name SVGCurveExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.SVGPath; import javafx.stage.Stage; public class SVGCurveExample extends Application { @Override public void start(Stage stage) { //Creating a SVGPath object SVGPath svgPath = new SVGPath(); String path = “M 70 110 C 70 180, 210 180, 210 110”; //Setting the SVGPath in the form of string svgPath.setContent(path); // Setting the stroke and fill of the path svgPath.setStroke(Color.BLACK); svgPath.setFill(Color.ORANGE); //Creating a Group object Group root = new Group(svgPath); //Creating a scene object Scene scene = new Scene(root, 300, 300); //Setting title to the Stage stage.setTitle(“Drawing a Bezier Curve”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls SVGCurveExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SVGCurveExample Output On executing, the above program generates a JavaFX window displaying a triangle, which is drawn by parsing the SVG path as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Drawing Modes Property

JavaFX – Drawing Modes Property ”; Previous Next You can draw 3D shapes in a JavaFX application using the Shape3D class. This class allows you construct three types of 3D shapes: box, cylinder, sphere as its direct subclasses. However, according to the nature of a JavaFX application you are trying to create, you can also enhance the look of a 3D shape using the properties the Shape3D class provides. There are three such properties that can be applied on 3D shapes of a JavaFX application. They are listed as follows − Cull Face Property Drawing Modes Property Material Property In this chapter, we will be learning about the Drawing Modes Property in JavaFX. Drawing Modes Property Drawing Modes is the property that belongs to the DrawMode enum type and it represents the type of drawing mode used to draw the current 3D shape. In JavaFX, you can choose two draw modes to draw a 3D shape, which are − Fill − This mode draws and fills a 2D shape (DrawMode.FILL). Line − This mode draws a 3D shape using lines (DrawMode.LINE). By default, the drawing mode of a 3Dimensional shape is fill. But you can still choose the draw mode to draw a 3D shape using the method setDrawMode() as follows − box.setDrawMode(DrawMode.FILL); Example The following program is an example which demonstrates the LINE draw mode on a 3D box. Save this code in a file with the name BoxDrawModeLine.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Box; import javafx.scene.shape.DrawMode; import javafx.stage.Stage; public class BoxDrawModeLine extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box1 = new Box(); //Setting the properties of the Box box1.setWidth(100.0); box1.setHeight(100.0); box1.setDepth(100.0); //Setting the position of the box box1.setTranslateX(200); box1.setTranslateY(150); box1.setTranslateZ(0); //Setting the drawing mode of the box box1.setDrawMode(DrawMode.LINE); //Creating a Group object Group root = new Group(box1); //Creating a scene object Scene scene = new Scene(root, 300, 300); //Setting title to the Stage stage.setTitle(“Drawing a Box”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BoxDrawModeLine.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BoxDrawModeLine Output On executing, the above program generates a JavaFX window displaying a box with draw mode LINE, as follows − Example Let us now see another example that shows the usage of FILL draw mode on a 3D shape. To show the difference of these modes properly, we will again apply this mode on a 3D box. Save this code in a file with the name BoxDrawModeFill.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.shape.Box; import javafx.scene.shape.DrawMode; import javafx.stage.Stage; public class BoxDrawModeFill extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box1 = new Box(); //Setting the properties of the Box box1.setWidth(100.0); box1.setHeight(100.0); box1.setDepth(100.0); //Setting the position of the box box1.setTranslateX(200); box1.setTranslateY(150); box1.setTranslateZ(0); //Setting the drawing mode of the box box1.setDrawMode(DrawMode.FILL); //Creating a Group object Group root = new Group(box1); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting camera PerspectiveCamera camera = new PerspectiveCamera(false); camera.setTranslateX(100); camera.setTranslateY(50); camera.setTranslateZ(0); scene.setCamera(camera); //Setting title to the Stage stage.setTitle(“Drawing a Box”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BoxDrawModeFill.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BoxDrawModeFill Output On executing, the above program generates a JavaFX window displaying a box with draw mode FILL, as follows − Print Page Previous Next Advertisements ”;

JavaFX – ButtonBar

JavaFX – Button Bar ”; Previous Next A ButtonBar is a type of container that arranges buttons in a horizontal layout. The arrangement or positions of these buttons depend on the type of operating system we are working on. Generally, all the buttons placed inside a ButtonBar are uniformly sized. However, it also allows us to customize the size as well as positions of the buttons. A typical button bar looks like the below figure. It contains two buttons namely “Yes” and “No”. ButtonBar in JavaFX In JavaFX, the class named ButtonBar represents a button bar. This class belongs to the package javafx.scene.control. We can create a button bar node in JavaFX by instantiating the ButtonBar class. There are two constructors available for this class, and they are as follows − ButtonBar() − It is used for creating a button bar with default properties that will be specific to the operating system. ButtonBar(String buttonOrder) − It will create a button bar with the specified button order. Steps to create a Button Bar in JavaFX To create a Button Bar in JavaFX, follow the steps given below. Step 1: Create two or more Buttons In JavaFX, the buttons are created by instantiating the class named Button which belongs to a package javafx.scene.control. Instantiate this class as shown below − //Creating required buttons Button buttonOne = new Button(“Back”); Button buttonTwo = new Button(“Accept”); Button buttonThree = new Button(“Cancel”); Similarly, create the required number of buttons for the project. Step 2: Instantiate ButtonBar class Instantiate the ButtonBar class of package javafx.scene.control without passing any parameter value to its constructor and add all the buttons using the getButtons() method. //Creating a ButtonBar ButtonBar newButtonbar = new ButtonBar(); // Adding buttons to the ButtonBar newButtonbar.getButtons().addAll(buttonOne, buttonTwo, buttonThree); Step 3: Launching Application After creating the button bar, follow the given steps below to launch the application properly − Firstly, instantiate the class named HBox and add the button bar using getChildren() method. Then, instantiate the class named Scene by passing the HBox object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is the program which will create a button bar in JavaFX. Save this code in a file with the name JavafxButtonBar.java. import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ButtonBar; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class JavafxButtonBar extends Application { @Override public void start(Stage stage) { //Creating required buttons Button buttonOne = new Button(“Back”); Button buttonTwo = new Button(“Accept”); Button buttonThree = new Button(“Cancel”); //Creating a ButtonBar ButtonBar newButtonbar = new ButtonBar(); // Adding buttons to the ButtonBar newButtonbar.getButtons().addAll(buttonOne, buttonTwo, buttonThree); newButtonbar.setPadding(new Insets(10)); HBox box = new HBox(); box.getChildren().addAll(newButtonbar); //Setting the stage Scene scene = new Scene(box, 500, 250); stage.setTitle(“ButtonBar in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } } Compile and execute the above Java file using the command prompt with the help of following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxButtonBar.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxButtonBar Output On executing, the above program generates a JavaFX window displaying a ButtonBar as shown below. Creating a ButtonBar with customized Button orders In most cases, the order of the Buttons is determined by the operating system. But, if a custom layout is required, then, the setButtonOrder() method of the ButtonBar class can be used. It takes button order property as a parameter and arranges the buttons accordingly. The button order properties of different OS are BUTTON_ORDER_WINDOWS, BUTTON_ORDER_MAC_OS, and BUTTON_ORDER_LINUX. Example In the following JavaFX program, we will create a ButtonBar by setting the button order property of MAC. Save this code in a file with the name JavafxButtonBar.java. import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonBar.ButtonData; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class JavafxButtonBar extends Application { @Override public void start(Stage stage) { //Creating required buttons Button buttonTwo = new Button(“Yes”); Button buttonOne = new Button(“No”); //Creating a ButtonBar ButtonBar newButtonbar = new ButtonBar(); // Setting the order of Buttons newButtonbar.setButtonOrder(“BUTTON_ORDER_MAC_OS”); // Adding buttons to the ButtonBar newButtonbar.getButtons().addAll(buttonOne, buttonTwo); newButtonbar.setPadding(new Insets(10)); HBox box = new HBox(); box.getChildren().addAll(newButtonbar); //Setting the stage Scene scene = new Scene(box, 500, 250); stage.setTitle(“ButtonBar in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxButtonBar.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxButtonBar Output When we execute the above code, it will generate the following output. Print Page Previous Next Advertisements ”;

JavaFX – Creating a Sphere

JavaFX – Creating a Sphere ”; Previous Next A sphere is a perfectly round geometrical object in a three-dimensional space that is the surface of a completely round shaped ball. A sphere is defined as the set of points that are all at the same distance r from a given point in a 3D space. This distance r is the radius of the sphere and the given point is the centre of the sphere. Sphere in JavaFX In JavaFX, a sphere is represented by a class named Sphere. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a sphere node in JavaFX. This class has a property named radiusof double datatype. It represents the radius of a Sphere. To draw a Sphere, you need to set values to this property by passing it to the constructor of this class at the time of instantiation; Or, by using a setter method named setRadius(). Steps to Draw 3D Sphere Follow the steps given below to Draw a Sphere (3D) in JavaFX. Step 1: Creating a Sphere Create a Sphere in JavaFX by instantiating the class named Sphere, which belongs to a package javafx.scene.shape. You can instantiate this class in the start() method as follows. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Creating an object of the class Sphere Sphere sphere = new Sphere(); } } Step 2: Setting Properties to the Sphere Set the radius of the Sphere using the method named setRadius() as shown below. //Setting the radius of the Sphere sphere.setRadius(300.0); Step 3: Creating a Group Object Instantiate the Group class by passing sphere object as a parameter to its constructor, as shown below − Group root = new Group(sphere); Step 4: Launching an Application Once the 3D object is created, launch the JavaFX application by following the steps below − Instantiate the class named Scene by passing the Group object as a parameter value to its constructor. You can also pass dimensions of the application screen as optional parameters to the constructor. Set the title to the stage using the setTitle() method of the Stage class. Add a scene object to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method within the Application class. Example The following program shows how to generate a Sphere using JavaFX. Save this code in a file with the name SphereExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.Sphere; public class SphereExample extends Application { @Override public void start(Stage stage) { //Drawing a Sphere Sphere sphere = new Sphere(); //Setting the properties of the Sphere sphere.setRadius(50.0); sphere.setTranslateX(200); sphere.setTranslateY(150); //Creating a Group object Group root = new Group(sphere); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a Sphere – draw fill”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls SphereExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SphereExample Output On executing, the above program generates a JavaFX window displaying a Sphere as shown below. Example In the following program, we are applying some CSS in JavaFX by colouring the scene of JavaFX application. Save this code in a file with the name CSSSphereExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.paint.Color; import javafx.scene.shape.Sphere; public class CSSSphereExample extends Application { @Override public void start(Stage stage) { //Drawing a Sphere Sphere sphere = new Sphere(); //Setting the properties of the Sphere sphere.setRadius(50.0); sphere.setTranslateX(100); sphere.setTranslateY(150); //Creating a Group object Group root = new Group(sphere); //Creating a scene object Scene scene = new Scene(root, 300, 300); scene.setFill(Color.ORANGE); //Setting title to the Stage stage.setTitle(“Drawing a Sphere”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls CSSSphereExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls CSSSphereExample Output On executing, the above program generates a JavaFX window displaying a Sphere as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Drawing a Polyline

JavaFX – Drawing a Polyline ”; Previous Next Polyline is defined as a continuous structure that is formed by combining multiple line segments with some vertices. These vertices are addressed as endpoints. Thus, a polyline can be constructed by specifying these endpoints through which these line segments are to be drawn. It consists of various properties like Points, width, color, start and end caps, type of join, stroke pattern etc. A Polyline is same as a polygon except that a polyline is not closed in the end. Or, continuous line composed of one or more line segments. In short, we can say a polygon is an open figure formed by coplanar line segments. Polyline in JavaFX In JavaFX, a Polyline is represented by a class named Polygon. This class belongs to the package javafx.scene.shape.. By instantiating this class, you can create polyline node in JavaFX. You need to pass the x, y coordinates of the points by which the polyline should be defined in the form of a double array. You can pass the double array as a parameter of the constructor of this class as shown below − Polyline polyline = new Polyline(doubleArray); Or, by using the getPoints() method as follows − polyline.getPoints().addAll(new Double[]{List of XY coordinates separated by commas }); Steps to Draw Polyline To Draw a Polyline in JavaFX, follow the steps given below. Step 1: Creating a Polyline You can create a line in JavaFX by instantiating the class named Line which belongs to a package javafx.scene.shape. You can instantiate this class in start() method as follows. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Creating an object of the class Polyline Polyline polyline = new Polyline(); } } Step 2: Setting Properties to the Polyline Specify a double array holding the XY coordinates of the points of the required polyline (hexagon in this example) separated by commas. You can do this by using the getPoints() method of the Polyline class as shown in the following code block. //Adding coordinates to the hexagon polyline.getPoints().addAll(new Double[]{ 200.0, 50.0, 400.0, 50.0, 450.0, 150.0, 400.0, 250.0, 200.0, 250.0, 150.0, 150.0, }); Step 3: Adding Polyline Object to Group In the start() method, instantiate the class named Group, which belongs to the package javafx.scene, by passing the Polyline object, created in the previous step, as a parameter value to its constructor. Group root = new Group(polyline); Step 4: Launching Application Once the 2D object is created, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is a program which generates a polyline using JavaFX. Save this code in a file with the name PolylineExample1.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.Polyline public class PolylineExample1 extends Application { @Override public void start(Stage stage) { //Creating a polyline Polyline polyline = new Polyline(); //Adding coordinates to the polygon polyline.getPoints().addAll(new Double[]{ 200.0, 50.0, 400.0, 50.0, 450.0, 150.0, 400.0, 250.0, 200.0, 250.0, 150.0, 150.0, }); //Creating a Group object Group root = new Group(polyline); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a Polyline”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls PolylineExample1.java java –module-path %PATH_TO_FX% –add-modules javafx.controls PolylineExample1 Output On executing, the above program generates a JavaFX window displaying a polyline as shown below. Example In this example, let us try to draw a polyline of 4 vertices. You can save the file name as PolylineExample2.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Polyline; import javafx.stage.Stage; public class PolylineExample2 extends Application { @Override public void start(Stage stage) { //Creating a Polyline Polyline polyline = new Polyline(); //Adding coordinates to the polygon polyline.getPoints().addAll(new Double[]{ 300.0, 50.0, 450.0, 150.0, 300.0, 250.0, 150.0, 150.0, }); //Creating a Group object Group root = new Group(polyline); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a Polyline”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls PolylineExample2.java java –module-path %PATH_TO_FX% –add-modules javafx.controls PolylineExample2 Output On executing, the above program generates a JavaFX window displaying a 4 vertices Polyline as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Path Transition

JavaFX – Path Transition ”; Previous Next A path transition in JavaFX is used to move a JavaFX node (or object) around a specific path. This is similar to translate transition, as it also moves the object from one position to another. However, the translate transition does not provide a continuous path through which the object moves; which path transition does. Any JavaFX node, like a 2D or 3D shape, text, image etc., can be moved along any path: straight or curved. Path Transition in JavaFX JavaFX offers a PathTransition class to apply the path transition on a JavaFX node. This class belongs to the javafx.animation package. This Transition creates a path animation that moves an object in a certain path and spans its duration. Path transition does nothing but translate an object in order to relocate it. This translation along a path is done by updating the coordinates of the node in both X and Y directions, and by updating the orientation to OrientationType.ORTHOGONAL_TO_TANGENT, at regular interval. The animated path is defined by the outline of any JavaFX shape. The PathTransition class offers the following properties to be applied on a node − duration − The duration of this Transition. node − The target node of this PathTransition. orientation − Specifies the upright orientation of node along the path. path − The shape on which outline the node should be animated. Example 1 Following is the program which demonstrates Path Transition in JavaFX. Save this code in a file with the name PathTransitionExample.java. import javafx.animation.PathTransition; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.CubicCurveTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.util.Duration; public class PathTransitionExample extends Application { @Override public void start(Stage stage) { //Drawing a Circle Circle circle = new Circle(); //Setting the position of the circle circle.setCenterX(300.0f); circle.setCenterY(135.0f); //Setting the radius of the circle circle.setRadius(25.0f); //Setting the color of the circle circle.setFill(Color.BROWN); //Setting the stroke width of the circle circle.setStrokeWidth(20); //Instantiating the path class Path path = new Path(); //Creating the MoveTo path element MoveTo moveTo = new MoveTo(100, 150); //Creating the Cubic curve path element CubicCurveTo cubicCurveTo = new CubicCurveTo(400, 40, 175, 250, 500, 150); //Adding the path elements to Observable list of the Path class path.getElements().add(moveTo); path.getElements().add(cubicCurveTo); //Creating a path transition PathTransition pathTransition = new PathTransition(); //Setting the duration of the path transition pathTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition pathTransition.setNode(circle); //Setting the path pathTransition.setPath(path); //Setting the orientation of the path pathTransition.setOrientation(PathTransition.OrientationType. ORTHOGONAL_TO_TAN GENT); //Setting the cycle count for the transition pathTransition.setCycleCount(50); //Setting auto reverse value to false pathTransition.setAutoReverse(false); //Playing the animation pathTransition.play(); //Creating a Group object Group root = new Group(circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Path transition example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls PathTransitionExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls PathTransitionExample Output On executing, the above program generates a JavaFX window as shown below. Example 2 Following is an example which transforms a circle along a complex path. Save this code in a file with name PathTransitionExample2.java. import javafx.animation.PathTransition; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.util.Duration; public class PathTransitionExample2 extends Application { @Override public void start(Stage stage) { //Drawing a Circle Circle circle = new Circle(); //Setting the position of the circle circle.setCenterX(300.0f); circle.setCenterY(135.0f); //Setting the radius of the circle circle.setRadius(25.0f); //Setting the color of the circle circle.setFill(Color.BROWN); //Setting the stroke width of the circle circle.setStrokeWidth(20); //Creating a Path Path path = new Path(); //Moving to the starting point MoveTo moveTo = new MoveTo(108, 71); //Creating 1st line LineTo line1 = new LineTo(321, 161); //Creating 2nd line LineTo line2 = new LineTo(126,232); //Creating 3rd line LineTo line3 = new LineTo(232,52); //Creating 4th line LineTo line4 = new LineTo(269, 250); //Creating 5th line LineTo line5 = new LineTo(108, 71); //Adding all the elements to the path path.getElements().add(moveTo); path.getElements().addAll(line1, line2, line3, line4, line5); //Creating the path transition PathTransition pathTransition = new PathTransition(); //Setting the duration of the transition pathTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition pathTransition.setNode(circle); //Setting the path for the transition pathTransition.setPath(path); //Setting the orientation of the path pathTransition.setOrientation( PathTransition.OrientationType.ORTHOGONAL_TO_TAN GENT); //Setting the cycle count for the transition pathTransition.setCycleCount(50); //Setting auto reverse value to true pathTransition.setAutoReverse(false); //Playing the animation pathTransition.play(); //Creating a Group object Group root = new Group(circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Path transition example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls PathTransitionExample2.java java –module-path %PATH_TO_FX% –add-modules javafx.controls PathTransitionExample2 Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Drawing a Quad Curve

JavaFX – Drawing a Quad Curve ”; Previous Next Mathematically, a quadratic curve is one that is described by a quadratic function like − y = ax2 + bx + c. In computer graphics Bezier curves are used. These are parametric curves which appear reasonably smooth at all scales. These Bezier curves are drawn based on points on an XY plane. A quadratic curve is a Bezier parametric curve in the XY plane which is a curve of degree 2. It is drawn using three points: start point, end point and control point as shown in the following diagram Quad Curve in JavaFX In JavaFX, a Quad Curve is represented by a class named QuadCurve. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a QuadCurve node in JavaFX. This class has 6 properties of the double datatype namely − startX − The x coordinate of the starting point of the curve. startY − The y coordinate of the starting point of the curve. controlX − The x coordinate of the control point of the curve. controlY − The y coordinate of the control point of the curve. endX − The x coordinate of the end point of the curve. endY − The y coordinate of the end point of the curve. To draw a Quad Curve, you need to pass values to these properties. This can be done either by passing them to the constructor of this class, in the same order, at the time of instantiation; or by using appropriate setter methods. Steps to Draw Quad Curve To Draw a Bezier Quadrilateral Curve in JavaFX, follow the steps given below. Step 1: Creating a Quad Curve You can create a Quad Curve in JavaFX by instantiating the class named QuadCurve which belongs to a package javafx.scene.shape. You can then instantiate this class in start() method of Application class as shown in the following code block. public class ClassName extends Application { public void start(Stage primaryStage) throws Exception { //Creating an object of the class QuadCurve QuadCurve quadcurve = new QuadCurve(); } } Step 2: Setting Properties to the Quad Curve Specify the x, y coordinates of the three points: start point, end point and control points, of the required curve, using their respective setter methods as shown in the following code block. //Adding properties to the Quad Curve quadCurve.setStartX(100.0); quadCurve.setStartY(220.0f); quadCurve.setEndX(500.0f); quadCurve.setEndY(220.0f); quadCurve.setControlX(250.0f); quadCurve.setControlY(0.0f); Or, by using their respective setter methods as follows − QuadCurve quadcurve = new QuadCurve(startX, startY, controlX, controlY, endX, endY); Step 3: Adding Quad Curve Object to Group In the start() method, instantiate a Group class by passing the previously created QuadCurve object as a parameter value to its constructor − Group root = new Group(quadcurve); Step 4: Launching Application Once the 2D object is created, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is a program which generates a quadrilateral curve using JavaFX. Save this code in a file with the name QuadCurveExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.QuadCurve; public class QuadCurveExample extends Application { @Override public void start(Stage stage) { //Creating a QuadCurve QuadCurve quadCurve = new QuadCurve(); //Adding properties to the Quad Curve quadCurve.setStartX(100.0); quadCurve.setStartY(220.0f); quadCurve.setEndX(500.0f); quadCurve.setEndY(220.0f); quadCurve.setControlX(250.0f); quadCurve.setControlY(0.0f); //Creating a Group object Group root = new Group(quadCurve); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a Quad curve”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls QuadCurveExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls QuadCurveExample Output On executing, the above program generates a JavaFX window displaying a Bezier quadrilateral curve as shown in the following screenshot. Example Now, you can also draw this quadrilateral curve by applying any effect, say bloom effect, as shown in the example below. Save the code in a file named QuadCurveEffect.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Bloom; import javafx.stage.Stage; import javafx.scene.paint.Color; import javafx.scene.shape.QuadCurve; public class QuadCurveEffect extends Application { @Override public void start(Stage stage) { //Creating a QuadCurve QuadCurve quadCurve = new QuadCurve(); //Adding properties to the Quad Curve quadCurve.setStartX(100.0); quadCurve.setStartY(220.0f); quadCurve.setEndX(300.0f); quadCurve.setEndY(220.0f); quadCurve.setControlX(250.0f); quadCurve.setControlY(0.0f); quadCurve.setFill(Color.RED); //Instantiating the Bloom class Bloom bloom = new Bloom(); //setting threshold for bloom bloom.setThreshold(0.1); //Applying bloom effect to quadCurve quadCurve.setEffect(bloom); //Creating a Group object Group root = new Group(quadCurve); //Creating a scene object Scene scene = new Scene(root, 400, 300); //Setting title to the Stage stage.setTitle(“Drawing a Quad curve”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls QuadCurveEffect.java java –module-path %PATH_TO_FX% –add-modules javafx.controls QuadCurveEffect Output On executing, the above program generates a JavaFX window displaying a Bezier quadrilateral curve as shown in the following screenshot. Print Page Previous Next Advertisements ”;

JavaFX – Translation Transformation

JavaFX – Translation Transformation ”; Previous Next A translation transformation simply moves an object to a different position on the same plane. You can translate a point in a 2D plane by adding translation coordinate (tx, ty) to the original coordinate (X, Y); this will get you the new coordinate (X’, Y’) where the point is relocated to. Translation Transformation in JavaFX In JavaFX, using the Translation transformation, a node can be shifted from one position to another position in the same application. This transformation is applied on a JavaFX node with the help of Translate class in javafx.scene.transform package. This class represents an Affine object that relocates the coordinates within the JavaFX application. Example 1 Following is the program which demonstrates translation in JavaFX. Here, we are creating 2 circles (nodes) at the same location with the same dimensions, but with different colors (Brown and Cadetblue). We are also applying translation on the circle with a cadetblue color. Save this code in a file with the name TranslationExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.transform.Translate; import javafx.stage.Stage; public class TranslationExample extends Application { @Override public void start(Stage stage) { //Drawing Circle1 Circle circle = new Circle(); //Setting the position of the circle circle.setCenterX(150.0f); circle.setCenterY(135.0f); //Setting the radius of the circle circle.setRadius(100.0f); //Setting the color of the circle circle.setFill(Color.BROWN); //Setting the stroke width of the circle circle.setStrokeWidth(20); //Drawing Circle2 Circle circle2 = new Circle(); //Setting the position of the circle circle2.setCenterX(150.0f); circle2.setCenterY(135.0f); //Setting the radius of the circle circle2.setRadius(100.0f); //Setting the color of the circle circle2.setFill(Color.CADETBLUE); //Setting the stroke width of the circle circle2.setStrokeWidth(20); //Creating the translation transformation Translate translate = new Translate(); //Setting the X,Y,Z coordinates to apply the translation translate.setX(300); translate.setY(50); translate.setZ(100); //Adding transformation to circle2 circle2.getTransforms().addAll(translate); //Creating a Group object Group root = new Group(circle,circle2); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Translation transformation example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls TranslationExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls TranslationExample Output On executing, the above program generates a JavaFX window as shown below. Example 2 In the following example, we are demonstrating translation on 3D shapes. Save this code in a file with the name TranslationExample3D.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Cylinder; import javafx.scene.transform.Translate; import javafx.stage.Stage; public class TranslationExample3D extends Application { @Override public void start(Stage stage) { Cylinder cy1 = new Cylinder(50, 100); Cylinder cy2 = new Cylinder(50, 100); //Creating the translation transformation Translate translate = new Translate(); //Setting the X,Y,Z coordinates to apply the translation translate.setX(200); translate.setY(150); translate.setZ(100); //Adding transformation to circle2 cy2.getTransforms().addAll(translate); //Creating a Group object Group root = new Group(cy1,cy2); //Creating a scene object Scene scene = new Scene(root, 400, 300); //Setting title to the Stage stage.setTitle(“Translation transformation example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls TranslationExample3D.java java –module-path %PATH_TO_FX% –add-modules javafx.controls TranslationExample3D Output On executing, the above program generates a JavaFX window as shown below. As we can see, with the original position of the cylinder we are not able to clearly see the full image like we do in the translated image. Therefore, translation transformation becomes necessary in cases like these. Print Page Previous Next Advertisements ”;