Tuesday, 20 October 2020

JavaFX: Shape fill property

 ‘fill’ property is used to fill the interior of an Shape with specified color.

Example

<Rectangle fx:id="rectangle" arcHeight="30" arcWidth="30" fill="gray" width="300" height="100" />

HelloFxmlDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.HelloWorldController">

	<Rectangle fx:id="rectangle" arcHeight="30" arcWidth="30" fill="gray" width="300" height="100" />
	
	<Text fx:id = "text" fill="lightsalmon" text="Hello World" style="-fx-font-weight: bold; -fx-font-size: 30" />

</StackPane>

HelloWorldController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;

public class HelloWorldController implements Initializable{
	
	@FXML
	private Rectangle rectangle;
	
	@FXML
	private Text text;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// Write custom logic here
		
	}

}

HelloFXML.java

package com.sample.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class HelloFXML extends Application {

	public static void main(String args[]) {
		launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(HelloFXML.class.getResource("/HelloFxmlDemo.fxml"));

		Scene scene = new Scene(root, 500, 500, Color.WHITE);

		primaryStage.setTitle("StackPane Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

Run HelloFXML application, you will see below window.


Previous                                                    Next                                                    Home

JavaFX: Working with Shape


            Shape fill property
            Shape: smooth: Apply rendering hints
            Shape: stroke: Draw outline around the shape
            strokeWidth: Set stroke width to a shape
            Shape: strokeType: Defines where to draw the stroke
            strokeLineCap: Specify end cap style
            strokeLineJoin:Defines line join style of a shape
Previous                                                    Next                                                    Home

JavaFX: Hello World using FXML

 FXML is a XML based markup language, used to define UI components. One major advantage of FXML is, you can create Scene graphs in XML file and load them in run time.

 

Scene Builder tool is available to generate this FXML by drag and drop the widget.

 

Why FXML?

We can separate the view from controller logic.

 

How to define application using FXML?

Step 1: Define .fxml file with all the UI components. This .fxml file contains information about the controller which is responsible to initialize these ui components.

 

Define ‘HelloFxmlDemo.fxml’ file in src/main/resources folder.

 

HelloFxmlDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.HelloWorldController">

	<Rectangle fx:id="rectangle" arcHeight="30" arcWidth="30" fill="gray" width="300" height="100" />
	
	<Text fx:id = "text" fill="lightsalmon" text="Hello World" style="-fx-font-weight: bold; -fx-font-size: 30" />

</StackPane>

 

As you see the snippet, I defined StackPane, Rectangle and Text widget. I specified the controller that mapped to this view using fx:controller attribute.

 

‘fx:id’ is required to map the widget in respective Java property. If fx:id property is ‘rectangle’, then you should map this widget in controller class with the property name ‘rectangle’.

 

@FXML

private Rectangle rectangle;

 

Step 2:  Define a controller that access the UI components defined in .fxml file using @FXML annotations.

 

You can specify custom behaviours to the components by implementing Initializable interface.

 

HelloWorldController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;

public class HelloWorldController implements Initializable{
	
	@FXML
	private Rectangle rectangle;
	
	@FXML
	private Text text;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// Write custom logic here
		
	}

}

 

Step 3: You need a main class

a.   that invokes FXML loader which in turn read FXML file.

b.   To set the Scene and stage.

 

HelloFXML.java

 

package com.sample.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class HelloFXML extends Application {

	public static void main(String args[]) {
		launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(HelloFXML.class.getResource("/HelloFxmlDemo.fxml"));

		Scene scene = new Scene(root, 500, 500, Color.WHITE);

		primaryStage.setTitle("StackPane Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

Run HelloFXML application, you will see below screen.



 


 

Previous                                                    Next                                                    Home

JavaFX: Hello World application

  Step 1: Extend 'javafx.application.Application' class and override start method.

public class HelloWorld extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		......
		......
	}

}

 

Application class is responsible for launching and stopping of the application. This class starts JavaFX application thread.

 

Step 2: We should create a scene and attach it to the Stage instance.

 

Group group = new Group();

Scene scene = new Scene(group, 500, 500, Color.AZURE);

primaryStage.setScene(scene);

 

In the above example, ‘group’ is the root node of the scene graph.

 

Step 3: Once you attach the Scene instance to Stage instance, you can call the show method of stage, to show this window on screen.

 

primaryStage.show();

 

Step 4: You can set title to your stage using ‘setTitle’ method

primaryStage.setTitle("Hello World");

 

Step 5: To launch the application, call the launch method available in Application class.

 

Find the below working application, you will see following kind of window.

 

HelloWorld.java

package com.sample.app;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class HelloWorld extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {

		primaryStage.setTitle("Hello World");

		Group group = new Group();

		Scene scene = new Scene(group, 500, 500, Color.AZURE);

		primaryStage.setScene(scene);

		primaryStage.show();

	}

	public static void main(String args[]) {
		launch(args);
	}

}

 

Run HelloWorld application, you will see below window.


Scene graph of above application looks like below.



 

Previous                                                    Next                                                    Home

JavaFX: Scene Graph

Scene Graph is a hierarchical tree of nodes. It can render graphical shapes.

 


Stage class can run on browser, desktop or mobile phone)

Screen class represents display for running JavaFX program.

 

Node is the super class for all the graphical elements in JavaFX.

 

Previous                                                    Next                                                    Home

Introduction to JavaFX

 

Why JavaFX?

a.   Rich Client Platform with lot of features to develop rich desktop clients.

b.   Rich UI controls like buttons, text boxes, checkboxes, menus, tables, charts etc.,

c.    Support web component APIs, where you can embed web pages in JavaFX Components.

d.   Support for Graphics, you can perform 2D and 3D graphics

e.   Media support for both audio and video.

f.     Support declarative markup language like FXML. Using FXML, you can define UI components easily.

 

Core packages of JavaFX

a.   javafx.stage

b.   javafx.scene

Previous                                                    Next                                                    Home

JavaFX using fxml

 

      Introduction to JavaFX
      Scene Graph
      Hello World application
      Hello World using FXML
      Working with Shape
            Shape fill property
            Shape: smooth: Apply rendering hints
            Shape: stroke: Draw outline around the shape
            strokeWidth: Set stroke width to a shape
            Shape: strokeType: Defines where to draw the stroke
            strokeLineCap: Specify end cap style
            strokeLineJoin:Defines line join style of a shape
      Working with Colors
      Working with fonts
      Get font names
      Get font families
      Set font posture (italic or regular)
      Working with Text widget
      css hello world
      Working with Line widget
      Working with Circle
      Working with Rectangle shape
      Working with Ellipse shape
      Polygon shape
      Polyline
      Arc shape
      QuadCurve Shape
      CubicCurve shape
      Path and PathElement : Draw arbitrary shapes
      Working with effects
            Shadow effect
            DropShadow Effect
            InnerShadow effect
            BoxBlur: Create Box Blur effect
            GaussianBlur Effect
            MotionBlur Effect
            Apply Blend effect
            Bloom Effect: Make the portion of image brighter
            Glow effect
            Lighting Effect
            ColorAdjust effect
            ColorInput Effect
            Reflection Effect
            SepiaTone effect
            DisplacementMap effect
            PerspectiveTransform effect
            Create chain of effects
      Working with Layout panes
            When to use Pane layout
            HBox Layout
            VBox layout
            Combine VBox and HBoxes
            FlowPane
            TilePane
            GridPane layout
            StackPane layout
            BorderPane layout
            Anchor Pane layout
            TextFlow layout
      Writing event handlers
            Event handling using anonymous Inner class
            Event handling using Lambda expression
            Event Handling using method reference
      Label widget
      Button widget
            Button Modes
            add image to a button
            Button: Background color chooser program
      RadioButton
      Radio button demo: Change background color of window
      ToggleButton
      ToggleButton demo: Change background color of window
      Node: Attach data to a control
      Properties
      CheckBox
      ChoiceBox
      ComboBox
      TextField widget
      PasswordField
      ScrollBar
      Slider
      ProgressIndicator
      ProgressBar Demo
      
      TextArea
      Support copy and paste functionality
      Hyperlink widget
      ToolBar control
      Menu and MenuItem
      ContextMenu
      Working with Tooltip
      ColorPicker
      DatePicker
      ListView
      TreeView
      TableView widget
      Working with dialogs
      ButtonType
      Dialog with custom actions
      TextInputDialog
      ChoiceDialog
      Example of StackPane
      How to define rounded rectangle?
      How to fade a text frame in and out?


Previous                                                    Next                                                    Home