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 – Translate Transition

JavaFX – Translate Transition ”; Previous Next In general, the Translate transition simply moves an object from one location to another on an application. This is usually done by specifying the new coordinate points or the distance this object needs to be moved to. JavaFX allows you to apply the translate transition on various nodes. For instance, in few applications on important days, some animated characters are seen moving around the application screen; like Santa Claus on Christmas. That can be performed using translation transition. Translate Transition in JavaFX The Translate transition in JavaFX moves the JavaFX nodes for a specified duration using the TranslateTransition class of javafx.animation package. This class contains various properties which are listed below to improve the quality of the said animation. byX − Specifies the incremented stop X coordinate value, from the start, of this TranslateTransition. byY − Specifies the incremented stop Y coordinate value, from the start, of this TranslateTransition. byZ − Specifies the incremented stop Z coordinate value, from the start, of this TranslateTransition. duration − The duration of this TranslateTransition fromX − Specifies the start X coordinate value of this TranslateTransition. fromY − Specifies the start Y coordinate value of this TranslateTransition. fromZ − Specifies the start Z coordinate value of this TranslateTransition. node − The target node of this TranslateTransition. toX − Specifies the stop X coordinate value of this TranslateTransition. toY − The stop Y coordinate value of this TranslateTransition. toZ − The stop Z coordinate value of this TranslateTransition. Example Following is the program which demonstrates Translate Transition in JavaFX. Save this code in a file with the name TranslateTransitionExample.java. import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class TranslateTransitionExample extends Application { @Override public void start(Stage stage) { //Drawing a Circle 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); //Creating Translate Transition TranslateTransition translateTransition = new TranslateTransition(); //Setting the duration of the transition translateTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition translateTransition.setNode(circle); //Setting the value of the transition along the x axis. translateTransition.setByX(300); //Setting the cycle count for the transition translateTransition.setCycleCount(50); //Setting auto reverse value to false translateTransition.setAutoReverse(false); //Playing the animation translateTransition.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(“Translate 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 TranslateTransitionExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls TranslateTransitionExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Home

JavaFX Tutorial Table of content JavaFX Tutorial JavaFX Basic UI Controls JavaFX Shapes JavaFX Effects JavaFX Animations Advantages of JavaFX Why to Learn JavaFX? Who Should Learn JavaFX Prerequisites to Learn JavaFX JavaFX Jobs and Opportunities Frequently Asked Questions about JavaFX PDF Version Quick Guide Resources Job Search Discussion JavaFX Tutorial JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. To develop GUI Applications using Java programming language, the programmers rely on libraries such as Advanced Windowing Tool Kit and Swing. After the advent of JavaFX, these Java programmers can now develop GUI applications effectively with rich content. Our JavaFX tutorial helps you learn JavaFX in simple and easy steps so that you can start building the User Interface of your application quickly. It covers all the necessary UI elements of JavaFX for a basic to advanced understanding of JavaFX and to get a feel of how JavaFX works. JavaFX Basic UI Controls JavaFx provides a variety of UI controls that allow a smooth interaction between users and applications. These controls are listed below − S.No Control & Description 1 Label It is a component for displaying text. 2 Button It is a class used for creating buttons. 3 Menu Contains a list of commands or options. 4 ToolTip A pop-up window that displays some additional information about other UI elements. 5 TextField Accepts and displays user input. JavaFX Shapes Shapes are geometric figures that can be drawn on either the XY or XYZ plane. Those built on XY plane are called 2D shapes, while those drawn on XYZ plane are referred to as 3D shapes. JavaFx provides various pre-defined classes that represents different types of shapes. These classes are as follows − S.No Class & Description 1 Line It is a class that represents a line. In general, line is a two-dimensional geometrical shape consist of two points. 2 Rectangle It is a class used for creating a 2D rectangular shape. In mathematical terms, rectangle is a four-sided polygon. 3 Box This JavaFX class represents a three-dimensional shape having length, width and height. 4 Cylinder It is a JavaFX class used to create a Cylinder. In general, cylinder is a closed solid figure that has two properties namely radius and height. JavaFX Effects In JavaFx, effects are used to enhance the visual appearance of nodes. The list of effects used in JavaFx are as follows − S.No Effect & Description 1 ColorAdjust It is used to apply colour effects to the JavaFx nodes. 2 Blend In this effect, we combine two or more elements to enhance the visuals. 3 Bloom When we apply this effect to any JavaFx node, then some portion of that node will glow. 4 Reflection This effect will add a reflection at the bottom of node. JavaFX Animations Generally, animations are used to create special visual effects to the elements like images, text, drawings, etc. The most commonly used animations in JavaFx are listed below − S.No Animation & Description 1 Rotate Transition It is used to deal with an object”s position by retaining its shape and properties. 2 Fade Transition This type of animation is done by changing the opacity property of nodes. 3 Stroke Transition It is applied to change the stroke color of a given shape. 4 Scale Transition It is a type animation in which we either increase or decrease the size of an object. Advantages of JavaFX JavaFX offers many advantages over other UI frameworks, such as Swing or AWT. Some of these advantages are as follows − JavaFX supports a declarative syntax for defining UI components, called FXML, which can be easily edited by designers or developers. It supports CSS for styling and animating UI elements, which gives more flexibility and control over the look and feel of the application. It allows us to use a wide range of media formats, such as images, audio, video, and 3D graphics, which can be integrated seamlessly into the UI. Since it is a Java based technology, it also has built-in support for concurrency and multithreading, which enables the application to handle complex tasks without blocking the UI thread. JavaFX also supports binding and properties, which simplifies the communication between the UI and the business logic. Why to Learn JavaFX? JavaFX is a cross-platform and portable framework that allows developers to write an application once and run it on any platform that supports Java. It simplifies UI development with its declarative syntax, FXML, and a rich set of libraries. We can customize a JavaFX application through CSS. Additionally, it supports the creation of dynamic UI effects. As an open-source project, JavaFX is actively developed and maintained by Oracle and the huge Java community. JavaFX is fairly easy to learn, so if you are starting to learn how to develop the user interface of an application then it is very much advised that you should also familiarize yourself with JavaFX. Who Should Learn JavaFX This JavaFX tutorial will help both students as well as working professionals who want to develop Rich Internet Applications. We recommend reading this tutorial, in the sequence listed in the left-side menu. This tutorial has been prepared to cover topics from beginner to advanced level. Prerequisites to Learn JavaFX Though we have tried our best to prepare this JavaFX tutorial in a simple and easy way, still before you start learning JavaFX concepts given in this tutorial, it is assumed that the readers have a prior knowledge of Java programming language. This tutorial will give you enough understanding of the various concepts of JavaFX along with suitable examples so that you can start your User Interface development journey immediately after finishing this tutorial. JavaFX Jobs and Opportunities Professionals skilled in JavaFX are in high demand as the need to develop rich and

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 – 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