JavaFX – InnerShadow Effect

JavaFX – InnerShadow Effect ”; Previous Next While the drop shadow effect creates a duplicate no behind the original node by blurring its edges, the inner shadow effect will be create a shadow inside the edges of the node. They are both similar to shadow effect. The class named InnerShadow of the package javafx.scene.effect represents the inner shadow effect. This class contains ten properties, which are − color − This property is of Color type representing the color of the shadow. blur type − This property is of BlurType and it represents the type of blur effect used to blur the shadow. radius − This property is of the type double and it represents the radius of the shadow blur kernel. width − This property is of the type double and it represents the width of the shadow blur kernel. height − This property is of the type double and it represents the height of the shadow blur kernel. input − This property is of the type Effect and it represents an input to the shadow effect. spread − This property is of the type double; it represents the spread of the shadow. offsetX − This property is of the type double, it represents the shadow offset in the x direction, in pixels. offsetY − This property is of the type double, it represents the shadow offset in the y direction in pixels. choke − This property is of double type; it represents the choke of the shadow. Example The following program is an example demonstrating the inner shadow effect of JavaFX. In here, we are drawing a text “Welcome to Tutorialspoint”, and a circle in a scene. To these, we are applying the inner shadow effect. Save this code in a file with the name InnerShadowEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.InnerShadow; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class InnerShadowEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(50); //Setting the text to be embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.RED); //Drawing a Circle Circle circle = new Circle(); //Setting the center of the circle circle.setCenterX(300.0f); circle.setCenterY(160.0f); //Setting the radius of the circle circle.setRadius(100.0f); //setting the fill color of the circle circle.setFill(Color.CORNFLOWERBLUE); //Instantiating the InnerShadow class InnerShadow innerShadow = new InnerShadow(); //Setting the offset values of the inner shadow innerShadow.setOffsetX(4); innerShadow.setOffsetY(4); //Setting the color of the inner shadow innerShadow.setColor(Color.GRAY); //Applying inner shadow effect to the text text.setEffect(innerShadow); //Applying inner shadow effect to the circle circle.setEffect(innerShadow); //Creating a Group object Group root = new Group(text,circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Inner shadow effect 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 InnerShadowEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls InnerShadowEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Shadow Effect

JavaFX – Shadow Effect ”; Previous Next The Shadow effect in JavaFX creates a duplicate of the specified node with blurry edges. The class named Shadow of the package javafx.scene.effect represents the sepia tone effect. This class contains six properties, which are − color − This property is of Color type represents the color of the shadow. blur type − This property is of the BlurType and it represents the type of the blur effect used to blur the shadow. radius − This property is of the type double and it represents the radius of the shadow blur kernel. width − This property is of the type double and it represents the width of the shadow blur kernel. height − This property is of the type double and it represents the height of the shadow blur kernel. input − This property is of the type Effect and it represents an input to the shadow effect. Example The following program is an example demonstrating the shadow effect of JavaFX. In here, we are drawing the text “Welcome to Tutorialspoint”, and a circle in a scene. We are applying the shadow effect with the Blur Type Gaussian with the Color Rosy Brown and Height, Width, Radius as 5. Save this code in a file with the name ShadowEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.BlurType; import javafx.scene.effect.Shadow; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class ShadowEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(50); //Setting the text to be embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Drawing a Circle Circle circle = new Circle(); //Setting the center of the circle circle.setCenterX(300.0f); circle.setCenterY(160.0f); //Setting the radius of the circle circle.setRadius(100.0f); //Instantiating the Shadow class Shadow shadow = new Shadow(); //setting the type of blur for the shadow shadow.setBlurType(BlurType.GAUSSIAN); //Setting color of the shadow shadow.setColor(Color.ROSYBROWN); //Setting the height of the shadow shadow.setHeight(5); //Setting the width of the shadow shadow.setWidth(5); //Setting the radius of the shadow shadow.setRadius(5); //Applying shadow effect to the text text.setEffect(shadow); //Applying shadow effect to the circle circle.setEffect(shadow); //Creating a Group object Group root = new Group(circle, text); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Bloom effect 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 ShadowEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls ShadowEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Installation Using Netbeans

JavaFX – Installation Using Netbeans ”; Previous Next Apache Netbeans is a free and open source integrated development environment (IDE) to develop applications using Java. And it allows you to create applications using JavaFX. This chapter demonstrates how to create a JavaFX application in Netbeans. We will be installing the latest versions of both JavaFX SDK and Apache Netbeans. Setting NetBeans Environment of JavaFX NetBeans18 provides inbuilt support for JavaFX. On installing this, you can create a JavaFX application without any additional plugins or JAR files. To set up the NetBeans environment, you will need to follow the steps that are given below. Step 1 − Visit the NetBeans website NetBeans website and click the Download button in order to download the NetBeans software. Step 2 − On clicking this button, a file named Apache-NetBeans-18-bin-windows-x64.exe will be downloaded onto your system. Run this file in order to install it. On running this file, a NetBeans installer will start as shown in the following screenshot. After completion of the configuration, you will see the Welcome Page of the installer. Step 3 − Click the Next button and proceed with the installation. Step 4 − The next window holds the NETBEANS IDE 18 license agreement. Read it carefully and accept the agreement by checking the checkbox at “I accept the terms in the license agreement” and then click the Next button. Step 5 − Choose the destination directory where you need the Netbeans 18 to be installed. Furthermore, you can also browse through the directory where Java Development Kit is installed in your system and click on the Next button. Step 6 − Check the Check for Updates box for automatic updates and click the Install button to start the installation. Step 7 − The wizard will then start preparing the installation data. Step 8 − This step starts the installation of NetBeans IDE 18 and it may take a while. Step 9 − Once the process is complete, click the Finish button to finish the installation. Step 10 − Once you launch the NetBeans IDE, you will see the start page as shown in the following screenshot. Step 11 − In the file menu, select New Project… to open the New project wizard as shown in the following screenshot. Step 12 − In the New Project wizard, select Java with Ant and click on Next. It starts creating a new Java Application for you. Step 13 − Select the name of the project and location of the project in the New Java Application window and then click Finish. It creates a sample application with the given name. In this instance, an application with a name SampleJavaApplication is created. Within this application, the NetBeans IDE will generate a Java program with the name SampleJavaApplication.java. As shown in the following screenshot, this program will be created inside NetBeans Source Packages → samplejavaapplication. Step 14 − Right-click on the file and select Run Project to run this code as shown in the following screenshot. This automatically created program contains the code which generates a simple JavaFX window having a button with the label Say ‘Hello World’ in it. Every time you click on this button, the string Hello World will be displayed on the console as shown below. Note − We will learn about the code in further chapters. Print Page Previous Next Advertisements ”;

JavaFX – Transformations

JavaFX – Transformations ”; Previous Next Transformation is nothing but changing graphics into something else by applying some rules. These rules allow you to apply various types of transformations such as shifting the position of the object by maintaining its shape, rotating the object based on an angle, changing the size of the object, etc. Using JavaFX, you can apply transformations on either a single node or group of nodes. You can also apply a single type of transformation or multiple transformations at a time to a JavaFX node. All these transformations are represented by various classes that are all subclasses of the Transform class and these belong to the package javafx.scene.transform. S.No Transformation & Description 1 Rotation In rotation, we rotate the object at a particular angle θ (theta) from its origin. 2 Scaling To change the size of an object, scaling transformation is used. 3 Translation Moves an object to a different position on the screen. 4 Shearing A transformation that slants the shape of an object is called the Shear Transformation. The Transform class implements affine transformations on JavaFX nodes. Affine transformations are nothing but the type of transformations that preserve the points, straight lines, and parallelism of these straight lines of the source object in the output object. These transformations can be applied on the JavaFX nodes with the help of Affine class extending the Transform class. Multiple Transformations You can apply multiple transformations on nodes in JavaFX, in two ways. You can apply one transformation at a time, or combine several transformations together and apply them at once. The following program is an example which performs Rotation, Scaling and Translation transformations on a rectangle simultaneously. Save this code in a file with the name − MultipleTransformationsExample.java. Example import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.transform.Rotate; import javafx.scene.transform.Scale; import javafx.scene.transform.Translate; import javafx.stage.Stage; public class MultipleTransformationsExample extends Application { @Override public void start(Stage stage) { //Drawing a Rectangle Rectangle rectangle = new Rectangle(50, 50, 100, 75); //Setting the color of the rectangle rectangle.setFill(Color.BURLYWOOD); //Setting the stroke color of the rectangle rectangle.setStroke(Color.BLACK); //creating the rotation transformation Rotate rotate = new Rotate(); //Setting the angle for the rotation rotate.setAngle(20); //Setting pivot points for the rotation rotate.setPivotX(150); rotate.setPivotY(225); //Creating the scale transformation Scale scale = new Scale(); //Setting the dimensions for the transformation scale.setX(1.5); scale.setY(1.5); //Setting the pivot point for the transformation scale.setPivotX(300); scale.setPivotY(135); //Creating the translation transformation Translate translate = new Translate(); //Setting the X,Y,Z coordinates to apply the translation translate.setX(250); translate.setY(0); translate.setZ(0); //Adding all the transformations to the rectangle rectangle.getTransforms().addAll(rotate, scale, translate); //Creating a Group object Group root = new Group(rectangle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Multiple transformations”); //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 MultipleTransformationsExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls MultipleTransformationsExample Output On executing, the above program generates a JavaFX window as shown below. Transformations on 3D Objects JavaFX allows you to perform transformations along three coordinates. However, to display objects with 3 dimensions (length, breadth and depth), JavaFX makes use of the concept called Z-buffering. Z-buffering, also known as depth buffering, is a type of buffer in computer graphics which is used to preserve the depth of a 3D object. This ensures that the perspective of a virtual object is the same as the real one: where the foreground surface blocks the view of the background surface (like it looks to an eye). If you want to create a 3-D effect transformation, specify all three coordinates x, y and z to the transformation constructors along with x-axis and y-axis. And, to be able to see the 3-D objects and transformation effects in JavaFX, users must enable the perspective camera. Example Following is an example which rotates and translates a 3-Dimensional box. Save this code in a file with the name RotationExample3D.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Box; import javafx.scene.transform.Rotate; import javafx.scene.transform.Translate; import javafx.stage.Stage; public class RotationExample3D extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box = new Box(); //Setting the properties of the Box box.setWidth(150.0); box.setHeight(150.0); box.setDepth(150.0); //Creating the translation transformation Translate translate = new Translate(); translate.setX(400); translate.setY(150); translate.setZ(25); Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS); Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS); Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS); rxBox.setAngle(30); ryBox.setAngle(50); rzBox.setAngle(30); box.getTransforms().addAll(translate,rxBox, ryBox, rzBox); //Creating a Group object Group root = new Group(box); //Creating a scene object Scene scene = new Scene(root, 600, 300); //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 RotationExample3D.java java –module-path %PATH_TO_FX% –add-modules javafx.controls RotationExample3D Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Rotation Transformation

JavaFX – Rotation Transformation ”; Previous Next In rotation, we rotate the object at a particular angle θ (theta) from its origin. From the following figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate with distance r from the origin. Rotation Transformation in JavaFX Rotation Transformation is applied on JavaFX nodes using the Rotate class of the javafx.scene.transform package. In this transformation, an affine object is rotated at a specified angle with certain coordinates set as anchor point. Example 1 Following is the program which demonstrates the rotation transformation in JavaFX. In here, we are creating 2 rectangular nodes at the same location, with the same dimensions but with different colors (Burlywood and Blue). We are also applying rotation transformation on the rectangle with Burlywood color. Save this code in a file with the name RotationExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.transform.Rotate; import javafx.stage.Stage; public class RotationExample extends Application { @Override public void start(Stage stage) { //Drawing Rectangle1 Rectangle rectangle1 = new Rectangle(150, 75, 200, 150); rectangle1.setFill(Color.BLUE); rectangle1.setStroke(Color.BLACK); //Drawing Rectangle2 Rectangle rectangle2 = new Rectangle(150, 75, 200, 150); //Setting the color of the rectangle rectangle2.setFill(Color.BURLYWOOD); //Setting the stroke color of the rectangle rectangle2.setStroke(Color.BLACK); //creating the rotation transformation Rotate rotate = new Rotate(); //Setting the angle for the rotation rotate.setAngle(20); //Setting pivot points for the rotation rotate.setPivotX(150); rotate.setPivotY(225); //Adding the transformation to rectangle2 rectangle2.getTransforms().addAll(rotate); //Creating a Group object Group root = new Group(rectangle1, rectangle2); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Rotation 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 RotationExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls RotationExample Output On executing, the above program generates a javaFx window as shown below. Example 2 In this example, we are trying to rotate a 3D object, say box. Here, let us create two boxes: one representing its original position, and the other displaying the transformed position. Save this code in a file with the name RotationExample3D.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Box; import javafx.scene.transform.Rotate; import javafx.scene.transform.Translate; import javafx.stage.Stage; public class RotationExample3D extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box1 = new Box(); Box box2 = new Box(); //Setting the properties of the Box box1.setWidth(150.0); box1.setHeight(150.0); box1.setDepth(150.0); //Setting the properties of the Box box2.setWidth(150.0); box2.setHeight(150.0); box2.setDepth(150.0); //Creating the translation transformation Translate translate = new Translate(); translate.setX(200); translate.setY(150); translate.setZ(25); Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS); Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS); Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS); rxBox.setAngle(30); ryBox.setAngle(50); rzBox.setAngle(30); box2.getTransforms().addAll(translate,rxBox, ryBox, rzBox); //Creating a Group object Group root = new Group(box1, box2); //Creating a scene object Scene scene = new Scene(root, 400, 300); //Setting title to the Stage stage.setTitle(“Transformation of 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 RotationExample3D.java java –module-path %PATH_TO_FX% –add-modules javafx.controls RotationExample3D Output On executing, the above program generates a JavaFX window as shown below. We can observe that the original position of the box is not completely within the application; hence, applying the transformation becomes necessary. Print Page Previous Next Advertisements ”;

JavaFX – DropShadow Effect

JavaFX – DropShadow Effect ”; Previous Next In general, a shadow effect duplicates a specified node and gives it blurry edges. The drop shadow effect is a type of a shadow effect; on applying this effect to a node, a shadow will be created behind the specified node. The class named DropShadow of the package javafx.scene.effect represents the drop shadow effect. This class contains nine properties, which are − color − This property is of Color type representing the color of the shadow. blur type − This property is of the type − BlurType and it represents the type of the blur effect used to blur the shadow. radius − This property is of the type double and it represents the radius of the shadow blur kernel. width − This property is of the type double and it represents the width of the shadow blur kernel. height − This property is of the type double and it represents the height of the shadow blur kernel. input − This property is of the type Effect and it represents an input to the shadow effect. spread − This property is of the type double; it represents the spread of the shadow. offsetX − This property is of the type double and it represents the shadow offset in the x direction in pixels. offset − This property is of the type double and it represents the shadow offset in the y direction in pixels. Example The following program is an example demonstrating the drop shadow effect of JavaFX. In here, we are drawing a text “Welcome to Tutorialspoint” and a circle in a scene. To these, we are applying the drop shadow effect. Save this code in a file with the name DropShadowEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.BlurType; import javafx.scene.effect.DropShadow; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class DropShadowEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(50); //Setting the text to be embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Drawing a Circle Circle circle = new Circle(); //Setting the centre of the circle circle.setCenterX(300.0f); circle.setCenterY(160.0f); //Setting the radius of the circle circle.setRadius(100.0f); //Instantiating the Shadow class DropShadow dropShadow = new DropShadow(); //setting the type of blur for the shadow dropShadow.setBlurType(BlurType.GAUSSIAN); //Setting color for the shadow dropShadow.setColor(Color.ROSYBROWN); //Setting the height of the shadow dropShadow.setHeight(5); //Setting the width of the shadow dropShadow.setWidth(5); //Setting the radius of the shadow dropShadow.setRadius(5); //setting the offset of the shadow dropShadow.setOffsetX(3); dropShadow.setOffsetY(2); //Setting the spread of the shadow dropShadow.setSpread(12); //Applying shadow effect to the text text.setEffect(dropShadow); //Applying shadow effect to the circle circle.setEffect(dropShadow); //Creating a Group object Group root = new Group(circle, text); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drop Shadow effect 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 DropShadowEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls DropShadowEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Box Blur Effect

JavaFX – Box Blur Effect ”; Previous Next In general, Blur means becoming unclear, on applying blur effect to a node it is made unclear. Box Blur is a kind of blur effect provided by JavaFX. In this effect, to apply blur to node, a simple box filter is used. The class named BoxBlur of the package javafx.scene.effect represents the BoxBlur effect, this class contains four properties, which are − height − This property is of double type representing the vertical size of the effect. width − This property is of double type representing the horizontal size of the effect. input − This property is of the type effect and it represents an input to the BoxBlur effect. iterations − This property is of an integer type representing the number of iterations of the effect, which are to be applied on the node. This is done to improve its quality or smoothness. Example Following is an example demonstrating the box blur effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the Box Blur effect to it. Save this code in a file with the name BoxBlurEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.BoxBlur; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class BoxBlurEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(150); //Setting the text to be added. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Instantiating the BoxBlur class BoxBlur boxblur = new BoxBlur(); //Setting the width of the box filter boxblur.setWidth(8.0f); //Setting the height of the box filter boxblur.setHeight(3.0f); //Setting the no of iterations boxblur.setIterations(3); //Applying BoxBlur effect to the text text.setEffect(boxblur); //Creating a Group object Group root = new Group(text); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Sample Application”); //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 BoxBlurEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BoxBlurEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – HLineTo Path Object

JavaFX – HLineTo Path Object ”; Previous Next The path element HLineTo is used to draw a horizontal line to a point in the specified coordinates from the current position. It is represented by a class named HLineTo. This class belongs to the package javafx.scene.shape. This class has a property of the double datatype namely − X − The x coordinate of the point to which a horizontal line is to be drawn from the current position. To draw a path element horizontal line, you need to pass a value to this property. This can be either done by passing it to the constructor of this class, at the time of instantiation; Or, by using their respective setter methods. Steps to draw PathElement Horizontal Line Follow the steps given below to draw a horizontal line to a specified point from the current position in JavaFX. Step 1: Creating a Path Object We first create a Path object by instantiating the Path class within the start() method of Application class as shown below − public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Creating a Path object Path path = new Path() } } Step 2: Setting the Initial Point Create the MoveTo path element and set XY coordinates to starting point of the line to the coordinates (100, 150). This can be done by using the methods setX() and setY() of the class MoveTo as shown below. //Moving to the starting point MoveTo moveTo = new MoveTo(); moveTo.setX(100.0f); moveTo.setY(150.0f); Step 3: Creating an Object of the Class HLineTo Create the path element horizontal line by instantiating the class named HLineTo which belongs to the package javafx.scene.shape as shown below. //Creating an object of the class HLineTo HLineTo hLineTo = new HLineTo(); Step 4: Setting Properties to the Horizontal Line Element Specify the x coordinate of the point to which a horizontal line is to be drawn from the current position. This can be done by setting the property x, using the method setX() of the HLineTo class as shown below. //Setting the Properties of the horizontal line element hlineTo.setX(400) Step 5: Adding Elements to the Observable List of Path Class Add the path elements MoveTo and HlineTo created in the previous steps to the observable list of the Path class as shown below − //Adding the path elements to Observable list of the Path class path.getElements().add(moveTo); path.getElements().add(hlineTo); Step 6: Launching Application Once the HLineTo path 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 draws a horizontal line from the current point to a specified position using the class Path of JavaFX. Save this code in a file with the name − HLineToExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.HLineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; public class HLineToExample extends Application { @Override public void start(Stage stage) { //Creating an object of the Path class Path path = new Path(); //Moving to the starting point MoveTo moveTo = new MoveTo(); moveTo.setX(100.0); moveTo.setY(150.0); //Instantiating the HLineTo class HLineTo hLineTo = new HLineTo(); //Setting the properties of the path element horizontal line hLineTo.setX(10.0); //Adding the path elements to Observable list of the Path class path.getElements().add(moveTo); path.getElements().add(hLineTo); //Creating a Group object Group root = new Group(path); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a horizontal line”); //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 HLineToExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls HLineToExample Output On executing, the above program generates a JavaFX window displaying a horizontal line, which is drawn from the current position to the specified point, as shown below. Example In this example, we are trying to draw a more complex path, i.e. triangle, using the horizontal line. Save this code in a file with the name − HLineToTriangle.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.HLineTo; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; public class HLineToTriangle extends Application { @Override public void start(Stage stage) { //Creating an object of the Path class Path path = new Path(); //Drawing a triangular path MoveTo moveTo = new MoveTo(); moveTo.setX(200.0); moveTo.setY(150.0); HLineTo hLineTo = new HLineTo(); hLineTo.setX(100.0); MoveTo moveTo2 = new MoveTo(); moveTo2.setX(100.0); moveTo2.setY(150.0); LineTo lineTo = new LineTo(); lineTo.setX(150.0); lineTo.setY(50.0); MoveTo moveTo3 = new MoveTo(); moveTo3.setX(150.0); moveTo3.setY(50.0); LineTo lineTo2 = new LineTo(); lineTo2.setX(200.0); lineTo2.setY(150.0); //Adding the path elements to Observable list of the Path class path.getElements().add(moveTo); path.getElements().add(hLineTo); path.getElements().add(moveTo2); path.getElements().add(lineTo); path.getElements().add(moveTo3); path.getElements().add(lineTo2); //Creating a Group object Group root = new Group(path); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Drawing a Triangular Path”); //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 HLineToTriangle.java java –module-path %PATH_TO_FX% –add-modules javafx.controls HLineToTriangle Output On executing, the above program generates a JavaFX window displaying a triangle. Print Page Previous Next Advertisements ”;

JavaFX – MotionBlur Effect

JavaFX – MotionBlur Effect ”; Previous Next Just like Gaussian Effect, Motion Blur is an effect to blur the nodes in JavaFX. It also uses a Gaussian Convolution Kernel that helps in producing the blurring effect. The only difference between Gaussian Effect and Motion Blur is that the Gaussian Convolution Kernel is used with a specified angle. As indicated by the name, on applying this effect by specifying some angle, the given input seems to you as if you are seeing it while it is in motion. The class named MotionBlur of the package javafx.scene.effect represents the Motion Blur effect. This class contains three properties, which include − input − This property is of the type Effect and it represents an input to the box blur effect. radius − This property is of double type representing the radius with which the Motion Blur Effect is to be applied. Angle − This is a property of double type and it represents the angle of the motion effect in degrees. Example The following program is an example demonstrating the Motion Blur Effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying Motion Blur Effect to it with an angle of 45 degrees. Save this code in a file with the name MotionBlurEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.scene.effect.MotionBlur; public class MotionBlurEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(150); //Setting the text to be added. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Instantiating the MotionBlur class MotionBlur motionBlur = new MotionBlur(); //Setting the radius to the effect motionBlur.setRadius(10.5); //Setting angle to the effect motionBlur.setAngle(45); //Applying MotionBlur effect to text text.setEffect(motionBlur); //Creating a Group object Group root = new Group(text); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Sample Application”); //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 MotionBlurEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls MotionBlurEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Effects

JavaFX – Effects ”; Previous Next An effect is any action that enhances the appearance of the graphics. In JavaFX, an effect is an algorithm that is applied on nodes to enhance their appearance visually. The effect property of the Node class is used to specify the effect. In JavaFX, you can set various effects to a node such as bloom, blur and glow. Each of these effects are represented by a class and all these classes are available in a package named javafx.scene.effect. Applying Effects to a Node You can apply an effect to a node using the setEffect() method. To this method, you need to pass the object of the effect. To apply an effect to a node, you need to − Create the node. Instantiate the respective class of the effect that is needed to be applied. Set the properties of the effect. Apply the effect to the node using the setEffect() method. Creating the Nodes First of all, create the nodes in a JavaFX application by instantiating their respective classes. For example, if you want to apply glow effect to an image in your application. Firstly, you need to create an image node by instantiating the Image class and set its view as shown below. Example //Creating an image Image image = new Image(“https://www.tutorialspoint.com/green/images/logo.png”); //Setting the image view ImageView imageView = new ImageView(image); //Setting the position of the image imageView.setX(100); imageView.setY(70); //setting the fit height and width of the image view imageView.setFitHeight(200); imageView.setFitWidth(400); //Setting the preserve ratio of the image view imageView.setPreserveRatio(true); Instantiating the Respective Class Instantiate the class representing the effect that is needed to be applied to the created node. For example − To apply the glow effect, you need to instantiate the Glow class as shown in the following code box − Glow glow = new Glow(); Setting the Properties of the Effect After instantiating the class, you need to set the properties for the effect using its setter methods. For example − To draw a 3-Dimensional box, you need to pass its width, height and depth. You can specify these values using their respective setter methods as shown below − //setting the level property glow.setLevel(0.9); Adding Effect to the Node Finally, you can apply the required effect to the node using the setEffect() method. For example: To set the glow effect to the image node, you need to pass the object of the Glow class to this method as follows − imageView.setEffect(glow); Types of JavaFX Effects The following table gives you the list of various effects (classes) provided by JavaFX. These classes exist in the package called javafx.scene.effect. S.No Effect and Description 1 Color Adjust You can adjust the color of an image by applying the color adjust effect to it. This includes the adjustment of the hue, saturation, brightness and contrast on each pixel The class named ColorAdjust of the package javafx.scene.effect represents the color adjust effect. 2 Color Input Color Input Effect gives the same output as drawing a rectangle and filling it with color. Unlike other effects, if this effect is applied to any node, it displays only a rectangular box (not the node). This effect is mostly used to pass as an input for other effects. The class named ColorInput of the package javafx.scene.effect represents the color input effect. 3 Image Input Image input effect in JavaFX just embeds an image to the JavaFX screen. Just like Color Input effect (It is used to pass the specified colored rectangular region as input to other effect), Image Input effect is used to pass the specified image as an input to another effect. The class named ImageInput of the package javafx.scene.effect represents the Image Input effect. 4 Blend In general, blend means mixture of two or more different things or substances. If we apply this blend effect, it takes the pixels of two different inputs, at the same location and it produces a combined output based on the blend mode. The class named Blend of the package javafx.scene.effect represents the blend effect. 5 Bloom On applying bloom effect, pixels in some portions of the node are made to glow. The class named Bloom of the package javafx.scene.effect represents the bloom effect. 6 Glow Just like bloom, the Glow effect makes the given input image to glow, this effect makes the bright pixels of the input brighter. The class named Glow of the package javafx.scene.effect represents the glow effect. 7 Box Blur On applying this blur effect to a node, it is made unclear. Box blur is a kind of blur effect provided by JavaFX. In this effect, when we apply blur to a node, a simple box filter is used. The class named BoxBlur of the package javafx.scene.effect represents the boxblur effect. 8 GaussianBlur Just like Box Blur Gaussian is an effect to blur the nodes in JavaFX. The only difference in the Gaussian Blur effect is that a Gaussian convolution kernel is used to produce a blurring effect. The class named GaussianBlur of the package javafx.scene.effect represents the Gaussian Blur effect. 9 MotionBlur Just like Gaussian Effects, Motion Blur is an effect to blur the nodes in JavaFX. It also uses a Gaussian convolution kernel to produce a blurring effect, but the difference is in this effect the Gaussian convolution kernel is used with a specified angle. The class named MotionBlur of the package javafx.scene.effect represents the Motion Blur effect. 10 Reflection On applying the reflection effect to a node in JavaFX, a reflection of it is added at the bottom of the node. The class named Reflection of the package javafx.scene.effect represents the reflection effect. 11 SepiaTone On applying the Sepia tone effect to a node in JavaFX (image in general), it is toned with a reddish brown color. The class named SepiaTone of the package javafx.scene.effect represents the sepia tone effect. 12 Shadow This effect creates a duplicate of the specified node with blurry edges. The class named Shadow of the package javafx.scene.effect