JavaFX – SepiaTone Effect

JavaFX – SepiaTone Effect ”; Previous Next Sepia tone effect in general changes the image from the black and white color to a reddish brown color. You can apply the Sepia Tone Effect to a node in JavaFX (image in general). The class named SepiaTone of the package javafx.scene.effect represents the sepia tone effect, this class contains two properties, which are − level − This property is of double type representing the intensity of this effect. The range of this property is 0.0 to 1.0. input − This property is of the type effect and it represents an input to the sepia tone effect. Example The following program is an example demonstrating the Sepia Tone Effect of JavaFX. In here, we are embedding the following image (tutorialspoint logo) in JavaFX scene using Image and ImageView classes. This is done at the position 100, 70 along with fit height and fit width 200 and 400 respectively. To this image, we are applying the Sepia Tone Effect with the level value 0.9. Save this code in a file with name SepiaToneEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.SepiaTone; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class SepiaToneEffectExample extends Application { @Override public void start(Stage stage) { //Creating an image Image image = new Image(“http://www.tutorialspoint.com/images/tp-logo.gif”); //Setting the image view ImageView imageView = new ImageView(image); //Setting the position of the image imageView.setX(150); imageView.setY(0); //setting the fit height and width of the image view imageView.setFitHeight(300); imageView.setFitWidth(400); //Setting the preserve ratio of the image view imageView.setPreserveRatio(true); //Instanting the SepiaTone class SepiaTone sepiaTone = new SepiaTone(); //Setting the level of the effect sepiaTone.setLevel(0.8); //Applying SepiaTone effect to the image imageView.setEffect(sepiaTone); //Creating a Group object Group root = new Group(imageView); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Sepia tone 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 SepiaToneEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SepiaToneEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Reflection Effect

JavaFX – Reflection Effect ”; Previous Next Reflection is scientifically defined as a phenomenon occurring when a light hits a surface (called a reflective surface like mirror), it gets thrown back. In such cases, all the objects present in the line of these light waves are also projected back as an image. You can also apply this reflection effect in JavaFX applications. 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. This class contains four properties, which are − topOpacity − This property is of double type representing the top extremes opacity value of the reflection. bottomOpacity − This property is of double type representing the bottom extremes opacity value of the reflection. input − This property is of the type Effect and it represents an input to the reflection effect. topOffset − This property is of double type representing the distance between the bottom of the input and the top of the reflection. fraction −This property is of double type representing the fraction of input that is visible in the output. The range of the fraction value is 0.0 to 1.0. Example The following program is an example demonstrating the reflection effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the reflection effect to it. Save this code in a file with the name ReflectionEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Reflection; 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 ReflectionEffectExample 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 embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Instanting the reflection class Reflection reflection = new Reflection(); //setting the bottom opacity of the reflection reflection.setBottomOpacity(0.0); //setting the top opacity of the reflection reflection.setTopOpacity(0.5); //setting the top offset of the reflection reflection.setTopOffset(0.0); //Setting the fraction of the reflection reflection.setFraction(0.7); //Applying reflection effect to the text text.setEffect(reflection); //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(“Reflection 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 ReflectionEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls ReflectionEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – 2D Shapes Operations

JavaFX – 2D Shapes(Objects) Operations ”; Previous Next Geometrically, a 2D shape is defined as any figure that can be displayed on a two-dimensional plane. Various applications these days make use of such shapes to develop an element or improve the look of their interface. For example, consider any mobile game application. These games contain various 2D shapes on their interface in order to enhance the user experience of the game. Or, a template editor where several 2D shapes are used to give options to the application user. JavaFX provides various nodes to create these 2D shapes like Line, Circle, Rectangle, other Polygons etc. However, to offer permutations and combinations of these shapes, JavaFX also allows you to perform some operations on them. In this chapter, let us briefly learn more about the operations provided. JavaFX Operations on 2D Shapes JavaFX provides various operations mainly to create complex shapes from the simpler shapes. For instance, we learned how to draw a house in the previous chapters of 2D shapes. We had to use several shapes like rectangles, lines etc. to create the final complex house shape; by making use of these operations, we can either merge two shapes easily or remove some area from it. There are three operations available in JavaFX that can be performed on 2D shapes. They are listed below − Union Operation Intersection Operation Subtraction Operation Usually, 2D shapes have a certain area they cover in an application. These three operations are performed on the area covered by these shapes by adding the area together, or subtracting a shape area from another shape, etc. Union Operation Union Operation is generally defined as the combination of two or more elements in an application. In JavaFX, a union operation can be performed on 2D shapes where two or more shapes are taken as inputs and combines the area of them together. This operation is fundamentally represented in the form of a following venn diagram − Intersection Operation Intersection operation retains the common elements from two or more sets. When used with 2D shapes in JavaFX, the common area of two or more shapes will be retained. This is also known as the intersected area of the objects. Look at the image shown below for better understanding. Subtraction Operation Subtraction Operation, also known as the Difference Operation, subtracts the elements of one set from another set. If some elements are not present in the first set (the set from which another set is being subtracted), they are ignored. In JavaFX, an area of a 2D shape is subtracted from the area of another 2D shape as long as they intersect. Print Page Previous Next Advertisements ”;

JavaFX – ProgressIndicator

JavaFX – ProgressIndicator ”; Previous Next A progress indicator is a UI control that is used to indicate the progress of a task to the user. It is often used with the Task API to notify users about the progress of background tasks and how much time will be required to complete the user action. In this tutorial, we are going to learn how to create and use progress indicator in JavaFX. Before that, let”s see how a general progress indicator looks like − ProgressIndicator in JavaFX In JavaFX, the progress indicator is represented by a class named ProgressIndicator. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a progress indicator in JavaFX. Constructors of the ProgressIndicator class are listed below − ProgressIndicator() − It constructs a new progress indicator without initial progress value. ProgressIndicator(double progress) − It constructs a new progress indicator with a specified initial progress value. Example The following program demonstrates how to create a progress indicator in JavaFX. Save this code in a file with the name ProgressindicatorJavafx.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ProgressIndicator; import javafx.stage.Stage; public class ProgressindicatorJavafx extends Application { @Override public void start(Stage stage) { // Creating a progress indicator without an initial progress value ProgressIndicator progress = new ProgressIndicator(); // Create a scene and stage Scene scene = new Scene(progress, 400, 300); stage.setScene(scene); stage.setTitle(“Progress Indicator in Javafx”); stage.show(); } public static void main(String[] args) { launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls ProgressindicatorJavafx.java java –module-path %PATH_TO_FX% –add-modules javafx.controls ProgressindicatorJavafx Output When we execute the above code, it will generate the following output. ProgressIndicator with an Initial Progress Value We can set the initial progress value to the progress indicator by using the parameterized constructor of the ProgressIndicator class. The constructor accepts a progress value as an argument which is of double type ranging between 0.0 and 1.0. Here, 0.0 means no progress and 1.0 means complete. If the progress value is negative, the progress indicator is indeterminate, meaning that it does not show any specific progress amount. Example In the following example, we are creating a progress indicator initialized with a specific progress value. Additionally, we are also creating two buttons labelled ”Increase” and ”Decrease”. Clicking the ”Increase” button increments the progress value, while clicking ”Decrease” decrements it. Save this code in a file with the name Javafxprogress.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ProgressIndicator; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Javafxprogress extends Application { @Override public void start(Stage stage) { // Create a progress indicator with an initial progress of 0.5 ProgressIndicator progressIndctr = new ProgressIndicator(0.5); // Create a button that increases the progress by 0.1 Button buttonOne = new Button(“Increase”); buttonOne.setOnAction(e -> { // Get the current progress value double progress = progressIndctr.getProgress(); // Increase the progress by 0.1 progress += 0.1; // Set the new progress value progressIndctr.setProgress(progress); }); // Create second button that decreases the progress by 0.1 Button buttonTwo = new Button(“Decrease”); buttonTwo.setOnAction(e -> { // Get the current progress value double progress = progressIndctr.getProgress(); // Decrease the progress by 0.1 progress -= 0.1; // Set the new progress value progressIndctr.setProgress(progress); }); // Create an HBox to hold the progress indicator and the button HBox hbox = new HBox(10); hbox.getChildren().addAll(progressIndctr,buttonOne, buttonTwo); // Create a scene with the HBox and set it to the stage Scene scene = new Scene(hbox, 400, 300); stage.setScene(scene); stage.setTitle(“Progress Indicator in JavaFX”); stage.show(); } public static void main(String[] args) { launch(args); } } Compile and execute the saved Java file from the command prompt by using the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls Javafxprogress.java java –module-path %PATH_TO_FX% –add-modules javafx.controls Javafxprogress Output On executing the above code, it will generate the following output. Print Page Previous Next Advertisements ”;

JavaFX – QuadCurveTo Path Object

JavaFX – QuadCurveTo Path Object ”; Previous Next Quadratic Curve or QuadCurve is generally defined as the curve that is defined by a quadratic equation. In JavaFX, we make use of 6 different properties to create this QuadCurve node. And to create a complex shape using QuadCurve, we would have to specify these properties everytime it is required. JavaFX makes this process simpler by providing a QuadCurve path object that takes lesser number of properties to draw it. We will learn about the QuadCurve Path object in detail further in this chapter. QuadCurve Path Object The path element quadratic curve is used to draw a quadratic curve to a point in the specified coordinates from the current position. It is represented by a class named QuadraticCurveTo. This class belongs to the package javafx.scene.shape. This class has 4 properties of the double datatype namely − setX − The x coordinate of the point to which a curve is to be drawn from the current position. setY − The y coordinate of the point to which a curve is to be drawn from the current position. controlX − The x coordinate of the control point of the curve. controlY − The y coordinate of the control point of the curve. To draw a quadratic curve, you need to pass values to these properties. This can be done either by passing them to the constructor of this class, in the same order, at the time of instantiation; Or, by using their respective setter methods. Steps to draw PathElement Quadratic Curve To draw a quadratic curve to a specified point from the current position in JavaFX, follow the steps given below. Step 1: Creating a Path Object Instantiate the Path class to create a Path object 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: Create a Path Create the MoveTo path element and set the XY coordinates to the 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 QuadCurveTo Create the path element Quadratic Curve by instantiating the class named QuadCurveTo which belongs to the package javafx.scene.shape as follows. //Creating an object of the class QuadCurveTo QuadCurveTo quadCurveTo = new QuadCurveTo() Step 4: Setting Properties to the Quadratic Curve Element Specify the coordinates of the point to which a Quadratic Curve is to be drawn from the current position. Then you should set the properties x, y, controlx, controlY and the coordinates of the control point by their setter methods as shown below. //Setting properties of the class QuadCurve quadCurveTo.setX(500.0f); quadCurveTo.setY(220.0f); quadCurveTo.setControlX(250.0f); quadCurveTo.setControlY(0.0f); Step 5: Adding Elements to the Observable List of the Path Class Add the path elements MoveTo and QuadraticCurveTo created in the previous steps to the observable list of the Path class as follows − //Adding the path elements to Observable list of the Path class path.getElements().add(moveTo); path.getElements().add(quadCurveTo) Step 6: Launching Application Once the QuadCurveTo 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 1 Following is a program which draws a quadratic curve from the current point to a specified position using the class named Path of JavaFX. Save this code in a file with the name QuadCurveToExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.scene.shape.QuadCurveTo; public class QuadCurveToExample extends Application { @Override public void start(Stage stage) { //Creating an object of the class named Path Path path = new Path(); //Moving to the starting point MoveTo moveTo = new MoveTo(); moveTo.setX(100.0); moveTo.setY(150.0); //Instantiating the class QuadCurve QuadCurveTo quadCurveTo = new QuadCurveTo(); //Setting properties of the class QuadCurve quadCurveTo.setX(500.0f); quadCurveTo.setY(220.0f); quadCurveTo.setControlX(250.0f); quadCurveTo.setControlY(0.0f); //Adding the path elements to Observable list of the Path class path.getElements().add(moveTo); path.getElements().add(quadCurveTo); //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 QuadCurve through a specified 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 QuadCurveToExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls QuadCurveToExample Output On executing, the above program generates a JavaFX window displaying a quadratic curve. This is drawn from the current position to the specified point as shown below. Example 2 In this example, let us try to apply some animation to this QuadCurve Path. Here, we are drawing a sample QuadCurve and applying the Fade Transition to it. Save this code in a file with the name QuadCurveToAnimation.java. import javafx.application.Application; import javafx.animation.FadeTransition; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.scene.shape.QuadCurveTo; import javafx.util.Duration; public class QuadCurveToAnimation extends Application { @Override public void start(Stage stage) { //Creating an object of the class named Path Path path = new Path(); //Moving to the starting point MoveTo moveTo = new MoveTo(); moveTo.setX(100.0); moveTo.setY(150.0); //Instantiating the class QuadCurve QuadCurveTo quadCurveTo = new QuadCurveTo(); //Setting properties of the class QuadCurve quadCurveTo.setX(500.0f); quadCurveTo.setY(220.0f); quadCurveTo.setControlX(250.0f); quadCurveTo.setControlY(0.0f); //Adding the path elements to Observable list

JavaFX – ChoiceBox

JavaFX – ChoiceBox ”; Previous Next A ChoiceBox is a drop-down list of predefined choices that allows users to select one of them at a time. It always displays the currently selected option on the top of the box, and when the user clicks on the ChoiceBox, it shows a drop-down list of all other available choices. In JavaFX, a choicebox is represented by a class named ChoiceBox. This class belongs to the package javafx.scene.control. By instantiating this class, we can create an ChoiceBox node in JavaFX. Two constructors are available for this class, and they are as follows − ChoiceBox() − It is used for creating an accordion without TitledPane. ChoiceBox(ObservableList items) − It will create an accordion with the specified TitledPane. Creating a Choice Box in JavaFX We need to follow the steps given below to create a ChoiceBox in JavaFX. Step 1: Instantiate the ChoiceBox class In JavaFX, the choice boxes are created by instantiating the class named ChoiceBox which belongs to a package javafx.scene.control. Instantiate this class inside the start() method as shown below − //Instantiating the ChoiceBox class ChoiceBox<String> box = new ChoiceBox<String>(); Step 2: Add Items to the ChoiceBox We use the ObservableList to add the items to a ChoiceBox in JavaFX. It holds all the items with specified type such as String. //Retrieving the observable list ObservableList<String> oslist = box.getItems(); //Adding items to the list oslist.addAll(“Windows7”, “Windows8”, “Windows10”, “Windows11”, “MAC OS”); Note − Sometimes, we may require to specify additional description for ChoiceBox, in such cases, we can use the Label or TextField class of JavaFX. Step 3: Launch the Application Once the ChoiceBox is created and its items are added, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the ChoiceBox object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The following JavaFX program demonstrates how to use a ChoiceBox in a JavaFX application. Save this code in a file with the name JavafxChoiceBox.java. import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.stage.Stage; public class JavafxChoiceBox extends Application { public void start(Stage stage) { //Instantiating the ChoiceBox class ChoiceBox<String> box = new ChoiceBox<String>(); //Retrieving the observable list ObservableList<String> oslist = box.getItems(); //Adding items to the list oslist.addAll(“Windows7”, “Windows8”, “Windows10”, “Windows11”, “MAC OS”); //Setting the position of the choice box box.setTranslateX(200); box.setTranslateY(15); //Setting the label Label setlabel = new Label(“Select your Operating System:”); setlabel.setTranslateX(20); setlabel.setTranslateY(20); //Adding the choice box to the group Group newgrp = new Group(box, setlabel); //Setting the stage Scene scene = new Scene(newgrp, 500, 200); stage.setTitle(“Choice Box in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxChoiceBox.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxChoiceBox Output When we execute the above code, it will generate the following output. Creating a Choice Box using its Parameterized Constructor Previously, we have used the empty constructor of the ChoiceBox class to create a choice box. However, there is also another way available for the same. We can use the parameterized constructor by passing the list items using observableArrayList as an argument. Example Following is the JavaFX program which will create a Choice Box using Parameterized Constructor of ChoiceBox class. Save this code in a file with the name JavafxChoiceBox.java. import javafx.application.Application; import javafx.collections.*; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.stage.Stage; public class JavafxChoiceBox extends Application { public void start(Stage stage) { //Creating a ChoiceBox ChoiceBox<String> box = new ChoiceBox<String> ( FXCollections.observableArrayList( “Windows7”, “Windows8”, “Windows10”, “Windows11”, “MAC OS”)); //Setting the position of the choice box box.setTranslateX(200); box.setTranslateY(15); //Setting the label Label setlabel = new Label(“Select your Operating System:”); setlabel.setTranslateX(20); setlabel.setTranslateY(20); //Adding the choice box to the group Group newgrp = new Group(box, setlabel); //Setting the stage Scene scene = new Scene(newgrp, 500, 200); stage.setTitle(“Choice Box in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxChoiceBox.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxChoiceBox Output On executing, the above program generates a JavaFX window displaying the below output. Print Page Previous Next Advertisements ”;

JavaFX – HTMLEditor

JavaFX – HTMLEditor ”; Previous Next An HTML editor is a type of text editor where users can create and edit HTML code within the JavaFX application. Some of the popular HTML text editors include Notepad, Sublime Text, Atom, Vscode and so on. Note − The HTML is a markup language used for developing web applications. In JavaFX, the HTML editor is represented by a class named HTMLEditor. This class belongs to the package javafx.scene.web. By instantiating this class, we can embed an HTMLEditor node in JavaFX. The JavaFX HTMLEditor provides the following features − It supports text indentation and alignment. We can create bulleted as well as numbered lists. It allows us to change the background and foreground colors. It also includes text styling features such as colors, bold, italic and underline. We can also set the font size and font family. Embedding an HTMLEditor in JavaFX As mentioned earlier, we can directly embed an HTML editor within the JavaFX application by instantiating the HTMLEditor class. Similar to other UI controls, it is necessary to add the HTMLEditor instance to the Scene object to make it visible in the JavaFX application. Example The following JavaFX program demonstrates how to embed an HTML editor into a JavaFX application. Save this code in a file with the name JavafxHtmlEditor.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.scene.web.HTMLEditor; public class JavafxHtmlEditor extends Application { @Override public void start(Stage stage) { // Instantiating HTMLEditor class HTMLEditor editorhtml = new HTMLEditor(); // including the HTMLEditor to Scene Scene scene = new Scene(editorhtml, 600, 500); // setting the stage to display editor stage.setScene(scene); stage.setTitle(“HTML Editor in JavaFX”); stage.show(); } public static void main(String[] args) { launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls,javafx.web JavafxHtmlEditor.java java –module-path %PATH_TO_FX% –add-modules javafx.controls,javafx.web JavafxHtmlEditor Output When we execute the above code, it will generate the following output. Creating an HtmlEditor in JavaFX with predefined Text We can also provide predefined text with desired styling through JavaFX code. For this operation, we can use the setHtmlText() method of the HTMLEditor class. This method takes a String as a parameter and displays that content in the editing area when the JavaFX application starts. Example Following is the JavaFX program which will create an HTML editor with the predefined text. Save this code in a file with the name HtmlEditorText.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.scene.web.HTMLEditor; public class HtmlEditorText extends Application { @Override public void start(Stage stage) { // Instantiating HTMLEditor class HTMLEditor editorhtml = new HTMLEditor(); // Setting the content for HTML Editor String text = “<html><body>Lorem ipsum dolor sit “ + “amet, consectetur adipiscing elit. Nam tortor felis, pulvinar “ + “in scelerisque cursus, pulvinar at ante. Nulla consequat” + “congue lectus in sodales. Nullam eu est a felis ornare.</body></html>”; editorhtml.setHtmlText(text); // including the HTMLEditor to Scene Scene scene = new Scene(editorhtml, 600, 500); // setting the stage to display editor stage.setScene(scene); stage.setTitle(“HTML Editor in JavaFX”); 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,javafx.web HtmlEditorText.java java –module-path %PATH_TO_FX% –add-modules javafx.controls,javafx.web HtmlEditorText Output When we execute the above code, it will generate the following output. Generating HTML code using HtmlEditor in JavaFX The HTMLEditor class provides a method named getHtmlText() to retrieve the content of the editing area. This method is called along with the object of HTMLEditor class. Example In the following JavaFX program, we will create an HTML editor to create and edit content, a button to obtain respective HTML code and a text area to show obtained code. Save this code in a file with the name HtmlgetText.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.scene.web.HTMLEditor; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; public class HtmlgetText extends Application { @Override public void start(Stage stage) { // Instantiating HTMLEditor class HTMLEditor editorhtml = new HTMLEditor(); editorhtml.setPrefHeight(300); // Creating a Text area to show HTML Code TextArea code = new TextArea(); ScrollPane pane = new ScrollPane(); pane.setContent(code); pane.setFitToWidth(true); pane.setPrefHeight(300); // creating button to get code Button button = new Button(“Get Code”); button.setOnAction(a -> { code.setText(editorhtml.getHtmlText()); }); // Creating root VBox root = new VBox(); root.setPadding(new Insets(10)); root.setSpacing(5); root.setAlignment(Pos.BOTTOM_LEFT); root.getChildren().addAll(editorhtml, button, pane); // including the HTMLEditor to Scene Scene scene = new Scene(root, 625, 500); // setting the stage to display editor stage.setScene(scene); stage.setTitle(“HTML Editor in JavaFX”); 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,javafx.web HtmlgetText.java java –module-path %PATH_TO_FX% –add-modules javafx.controls,javafx.web HtmlgetText Output On executing, the above program generates a JavaFX window displaying the below output. Print Page Previous Next Advertisements ”;

JavaFX – Subtraction Operation

JavaFX – Subtraction Operation ”; Previous Next As the name suggests, the subtraction operation will subtract the elements of a set from another set. Most people are generally confused between the intersection operation, the two operations completely differ with respect to their operations. While intersection operation retrieves the common elements between two sets, the subtraction operation finds the common elements between two sets and removes them from the first set. If there are elements in second set that are not present in the first set, they are ignored. Like other operations, the subtraction operation is also adopted in computer programming. It is available as difference operator in few programming languages; but in JavaFX, this operation can be used on 2D shapes. Subtraction Operation in JavaFX In JavaFX, the subtraction operation works with the area covered by two or more 2D shapes. It eliminates the area of the second shape from the area of the first shape. If the areas of these two shapes are fully exclusive, the area of first shape is retained as the result. Technically, this operation takes two or more shapes as an input. Then, it returns the area of the first shape excluding the area overlapped by the second one as shown below. You can perform the Subtraction Operation on the shapes using the method named subtract(). Since this is a static method, you should call it using the class name (Shape or its subclasses) as shown below. Shape shape = Shape.subtract(circle1, circle2); Following is an example of the Subtraction Operation. In here, we are drawing two circles and performing a subtraction operation on them. Save this code in a file with name SubtractionExample.java. Example import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.shape.Circle; import javafx.scene.shape.Shape; public class SubtractionExample extends Application { @Override public void start(Stage stage) { //Drawing Circle1 Circle circle1 = new Circle(); //Setting the position of the circle circle1.setCenterX(250.0f); circle1.setCenterY(135.0f); //Setting the radius of the circle circle1.setRadius(100.0f); //Setting the color of the circle circle1.setFill(Color.DARKSLATEBLUE); //Drawing Circle2 Circle circle2 = new Circle(); //Setting the position of the circle circle2.setCenterX(350.0f); circle2.setCenterY(135.0f); //Setting the radius of the circle circle2.setRadius(100.0f); //Setting the color of the circle circle2.setFill(Color.BLUE); //Performing subtraction operation on the circle Shape shape = Shape.subtract(circle1, circle2); //Setting the fill color to the result shape.setFill(Color.DARKSLATEBLUE); //Creating a Group object Group root = new Group(shape); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Subtraction 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 SubtractionExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SubtractionExample Output On executing, the above program generates a JavaFX window displaying the following output − Example Now, let us try to perform subtraction operation on two ellipses where we will subtract the area of second ellipse from the first ellipse. Save this file under the name EllipseSubtractionOperation.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.shape.Ellipse; import javafx.scene.shape.Shape; public class EllipseSubtractionOperation extends Application { @Override public void start(Stage stage) { Ellipse ellipse1 = new Ellipse(); ellipse1.setCenterX(250.0f); ellipse1.setCenterY(100.0f); ellipse1.setRadiusX(150.0f); ellipse1.setRadiusY(75.0f); ellipse1.setFill(Color.BLUE); Ellipse ellipse2 = new Ellipse(); ellipse2.setCenterX(350.0f); ellipse2.setCenterY(100.0f); ellipse2.setRadiusX(150.0f); ellipse2.setRadiusY(75.0f); ellipse2.setFill(Color.RED); Shape shape = Shape.subtract(ellipse1, ellipse2); //Setting the fill color to the result shape.setFill(Color.DARKSLATEBLUE); //Creating a Group object Group root = new Group(shape); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Subtraction 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 EllipseSubtractionOperation.java java –module-path %PATH_TO_FX% –add-modules javafx.controls EllipseSubtractionOperation Output On executing, the above program generates a JavaFX window displaying the following output − Print Page Previous Next Advertisements ”;

JavaFX – Intersection Operation

JavaFX – Intersection Operation ”; Previous Next Similar to the Union operation, the intersection operation was fundamentally used in set theory. As the name suggests, this operation is defined as the intersection of two different sets into one. Hence, the common elements from two or more sets are retrieved; ignoring duplicate elements. This concept is then adopted by various techniques in computer programming. For instance, it is also used in programming languages like C, C++, Java, Python etc. as an operator or a method. Similarly, JavaFX also provides intersection operation on 2D shapes. Intersection Operation in JavaFX JavaFX allows you to perform intersection operation on 2D shapes, where, the areas of two or more shapes are intersected together and the common area of these shapes is obtained as a result. Thus, this operation takes two or more shapes as inputs and returns the intersection area between them as shown below. You can perform an intersection operation on the shapes using the method named intersect(). Since this is a static method, you should call it using the class name (Shape or its subclasses) as shown below. Shape shape = Shape.intersect(circle1, circle2); Following is an example of the intersection operation. In here, we are drawing two circles and performing a intersection operation on them. Save this code in a file with the name IntersectionExample.java Example import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.shape.Circle; import javafx.scene.shape.Shape; public class IntersectionExample extends Application { @Override public void start(Stage stage) { //Drawing Circle1 Circle circle1 = new Circle(); //Setting the position of the circle circle1.setCenterX(250.0f); circle1.setCenterY(135.0f); //Setting the radius of the circle circle1.setRadius(100.0f); //Setting the color of the circle circle1.setFill(Color.DARKSLATEBLUE); //Drawing Circle2 Circle circle2 = new Circle(); //Setting the position of the circle circle2.setCenterX(350.0f); circle2.setCenterY(135.0f); //Setting the radius of the circle circle2.setRadius(100.0f); //Setting the color of the circle circle2.setFill(Color.BLUE); //Performing intersection operation on the circle Shape shape = Shape.intersect(circle1, circle2); //Setting the fill color to the result shape.setFill(Color.DARKSLATEBLUE); //Creating a Group object Group root = new Group(shape); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Intersection 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 IntersectionExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls IntersectionExample Output On executing, the above program generates a JavaFX window displaying the following output − Example Now, let us try to perform intersection operation on two elliptical shapes (to retrieve the common area between them). Save this file under the name EllipseIntersectionOperation.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.shape.Ellipse; import javafx.scene.shape.Shape; public class EllipseIntersectionOperation extends Application { @Override public void start(Stage stage) { Ellipse ellipse1 = new Ellipse(); ellipse1.setCenterX(250.0f); ellipse1.setCenterY(100.0f); ellipse1.setRadiusX(150.0f); ellipse1.setRadiusY(75.0f); ellipse1.setFill(Color.BLUE); Ellipse ellipse2 = new Ellipse(); ellipse2.setCenterX(350.0f); ellipse2.setCenterY(100.0f); ellipse2.setRadiusX(150.0f); ellipse2.setRadiusY(75.0f); ellipse2.setFill(Color.RED); Shape shape = Shape.intersect(ellipse1, ellipse2); //Setting the fill color to the result shape.setFill(Color.DARKSLATEBLUE); //Creating a Group object Group root = new Group(shape); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Intersection 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 EllipseIntersectionOperation.java java –module-path %PATH_TO_FX% –add-modules javafx.controls EllipseIntersectionOperation Output On executing, the above program generates a JavaFX window displaying the following output − Print Page Previous Next Advertisements ”;

JavaFX – MenuBar

JavaFX – MenuBar ”; Previous Next A menubar is a graphical user interface component that displays a horizontal row of menus, each containing a list of commands or options. It is located at the top of a window or screen, and provides a convenient way for users to access the functionality of an application. A typical menubar looks like the below figure − Creating MenuBar in JavaFX In JavaFX, the menubar control is represented by a class named MenuBar. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a MenuBar control in JavaFX. In addition to the MenuBar class, we also require the following classes − Menu The Menu class represents a single menu in the menubar. It has a text property that defines its label, and an items property that holds a list of menu items. To create a Menu, use the code given below − //Creating a menu Menu objectOfMenu = new Menu(“nameOfMenu”); MenuItem The MenuItem is used to create a single command or option within a menu. It has a text property that defines its label, and an onAction property that defines what happens when the user selects it. It is created by using the below code − //Creating menu items for the menu MenuItem menuItemObject = new MenuItem(“nameOfMenuItem”); Example The following example illustrates how to create a MenuBar in JavaFX application. Save this code in a file with the name JavafxmenuBar.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.stage.Stage; public class JavafxmenuBar extends Application { public void start(Stage stage) { //Creating the first menu Menu category = new Menu(“Category”); //Creating menu items for the menu MenuItem itemOne = new MenuItem(“Programming”); MenuItem itemTwo = new MenuItem(“Cyber Security”); MenuItem itemThree = new MenuItem(“Marketing”); //Adding all the items to the category category.getItems().addAll(itemOne, itemTwo, itemThree); //Creating another menu Menu library = new Menu(“Library”); //Creating menu items for the library menu MenuItem itemFour = new MenuItem(“HTML”); MenuItem itemFive = new MenuItem(“Java”); MenuItem itemSix = new MenuItem(“JavaFX”); //Adding all the items to library menu library.getItems().addAll(itemFour, itemFive, itemSix); //Creating the third menu Menu articles = new Menu(“Articles”); //Creating menu items for the articles MenuItem itemSeven = new MenuItem(“Constructor”); MenuItem itemEight = new MenuItem(“Inheritance”); MenuItem itemNine = new MenuItem(“Current Affairs”); //Adding all items to the menu articles.getItems().addAll(itemSeven, itemEight, itemNine); //Instantiating the MenuBar class MenuBar newmenubar = new MenuBar(); //Adding all the menus to the MenuBar object newmenubar.getMenus().addAll(category, library, articles); //Setting the stage Group newgroup = new Group(newmenubar); Scene scene = new Scene(newgroup, 600, 400); stage.setTitle(“MenuBar in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxmenuBar.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxmenuBar Output When we execute the above code, it will generate the following output. How to add Icons to the MenuItems? To add icons to the menuitems, we call the setGraphic() method which accepts an object of ImageView class. The icon will be appear next to the MenuItem name. Example In the following JavaFX application, we are going to demonstrate how to add icons for the menuitems within MenuBar. Save this code in a file with the name JavafxmenuBar.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.stage.Stage; import java.io.File; import javafx.scene.image.ImageView; public class JavafxmenuBar extends Application { public void start(Stage stage) { //Creating the first menu Menu category = new Menu(“Category”); //Creating menu items for the menu MenuItem itemOne = new MenuItem(“Programming”); MenuItem itemTwo = new MenuItem(“Cyber Security”); MenuItem itemThree = new MenuItem(“Marketing”); // adding icons to the menuitems itemOne.setGraphic(new ImageView(“file:programming.png”)); itemTwo.setGraphic(new ImageView(“file:cyber.png”)); itemThree.setGraphic(new ImageView(“file:marketing.png”)); //Adding all the items to the category category.getItems().addAll(itemOne, itemTwo, itemThree); //Creating another menu Menu library = new Menu(“Library”); //Creating menu items for the library menu MenuItem itemFour = new MenuItem(“HTML”); MenuItem itemFive = new MenuItem(“Java”); MenuItem itemSix = new MenuItem(“JavaFX”); //Adding all the items to library menu library.getItems().addAll(itemFour, itemFive, itemSix); //Creating the third menu Menu articles = new Menu(“Articles”); //Creating menu items for the articles MenuItem itemSeven = new MenuItem(“Constructor”); MenuItem itemEight = new MenuItem(“Inheritance”); MenuItem itemNine = new MenuItem(“Current Affairs”); //Adding all items to the menu articles.getItems().addAll(itemSeven, itemEight, itemNine); //Instantiating the MenuBar class MenuBar newmenubar = new MenuBar(); //Adding all the menus to the MenuBar object newmenubar.getMenus().addAll(category, library, articles); //Setting the stage Group newgroup = new Group(newmenubar); Scene scene = new Scene(newgroup, 600, 400); stage.setTitle(“MenuBar in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved Java file from the command prompt by using the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxmenuBar.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxmenuBar Output On executing the above JavaFX code, it will generate the following output. Print Page Previous Next Advertisements ”;