Monday, 26 October 2020

JavaFX: Working with Line widget

 Line widget is used to draw straight line between two points.

 

Line class provides following constructors to define Line widget.

 

public Line()

public Line(double startX, double startY, double endX, double endY)

 

startX the horizontal coordinate of the start point of the line segment

startY the vertical coordinate of the start point of the line segment

endX the horizontal coordinate of the end point of the line segment

endY the vertical coordinate of the end point of the line segment

 

You can define a line in FXML like below.

 

<Line fx:id="line1" stroke="black" startX="10" startY="50" endX="350" endY="50" strokeWidth="10"/>

 

Find the below working application.

 

lineDemo.fxml

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

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

	<Line fx:id="line1" stroke="black" startX="10" startY="50" endX="350" endY="50" strokeWidth="10"/>
	
	<Line fx:id="line2" stroke="green" startX="10" startY="90" endX="350" endY="90" strokeWidth="10" strokeLineCap="round"/>
	
	<Line fx:id="line3" stroke="red" startX="10" startY="130" endX="350" endY="130" strokeWidth="10"/>
	
</Group>

 

LineController.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.Line;

public class LineController implements Initializable {

	@FXML
	private Line line1;
	
	@FXML
	private Line line2;
	
	@FXML
	private Line line3;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		line3.getStrokeDashArray().addAll(15d);
	}

}

 

LineDemo.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 LineDemo extends Application {

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

	}

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

		Parent root = (Parent) FXMLLoader.load(LineDemo.class.getResource("/lineDemo.fxml"));

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

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

}

 

Run above application, you will see below screen.


  

Previous                                                    Next                                                    Home

JavaFX: css hello world

JavaFX supports css styling. In this post, I am going to explain how to apply styles to a UI widget using css styles.

 

Create a css file with all the necessary styles and apply this css style sheet file to the Scene instance.

 

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

String cssFile = HelloWorldCss.class.getResource("/hello.css").toExternalForm();

scene.getStylesheets().add(cssFile);

 

Find the below working application.

 

hello.css

#myText {
    -fx-fill: yellow;
    -fx-stroke: darkblue;
    -fx-stroke-width: 2;
	-fx-font-weight: bold; 
	-fx-font-size: 50; 
	-fx-font-family: Verdana; 
	-fx-font-style: italic
}

HelloWorldCss.java

package com.sample.app;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HelloWorldCss extends Application {

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

		Text text = new Text("Learning JavaFX");
		text.setId("myText");

		StackPane stackPane = new StackPane();
		stackPane.getChildren().addAll(text);

		Scene scene = new Scene(stackPane, 500, 500, Color.WHITE);
		String cssFile = HelloWorldCss.class.getResource("/hello.css").toExternalForm();
		scene.getStylesheets().add(cssFile);

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

	}

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


Run above application, you will see below screen.




  

Previous                                                    Next                                                    Home

JavaFX: Working with Text widget

 Text widget is used to define a node that displays text.

 

How to define Text widget?

Text class provides below constructors to define a Text widget.

 

public Text()

public Text(String text)

public Text(double x, double y, String text)

 

‘x’ specifies horizontal position of the text.

‘y’ specifies vertical position of the text.

 

How to define a text element in fxml?

You can define using Text element.

 

Example

<Text fx:id = "text" text="Learning JavaFX"  fill="lightgreen"

         style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"

         stroke="darkblue" strokeWidth="2"

         underline="true"/>

 

Find the below working application.

 

textDemo.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.TextController">
	
	<Text fx:id = "text" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"/>

</StackPane>

 

TextController.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.text.Text;

public class TextController implements Initializable {

	@FXML
	private Text text;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
	}

}

 

TextControllerDemo.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 TextControllerDemo extends Application {

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

	}

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

		Parent root = (Parent) FXMLLoader.load(TextControllerDemo.class.getResource("/textDemo.fxml"));

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

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

 

Output


 

Previous                                                    Next                                                    Home

JavaFX: Set font posture (italic or regular)

 'FontPosture' enum is used to set the font posture to italic. Following table summarizes the constants for font posture.

 

Font Posture

Description

REGULAR

Represents Regular.

ITALIC

Represents Italic

 

Example: Below snippet sets font style to italic.

<Text fx:id = "text" text="Learning JavaFX"  fill="lightgreen"

         style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"

         stroke="darkblue" strokeWidth="2"

         underline="true"/>

 

Find the below working application.

 

fontPosture.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.FontPostureController">
	
	<Text fx:id = "text" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"/>

</StackPane>

 

FontPostureController.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.text.Text;

public class FontPostureController implements Initializable {

	@FXML
	private Text text;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
	}

}

 

FontPostureDemo.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 FontPostureDemo extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		Parent root = (Parent) FXMLLoader.load(FontPostureDemo.class.getResource("/fontPosture.fxml"));

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

		primaryStage.setTitle("Font Posture Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

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

 

Output


 

 

 

Previous                                                    Next                                                    Home

Sunday, 25 October 2020

JavaFX: Get font families

'getFamilies' method return all the font families installed on the user's system, including any application fonts or SDK fonts.

 

Example

List<String> fontNames = Font.getFamilies();

 

FontFamilies.java

package com.sample.app;

import java.util.List;

import javafx.scene.text.Font;

public class FontFamilies {
	public static void main(String args[]) {
		List<String> fontNames = Font.getFamilies();
		
		for(String fontName: fontNames) {
			System.out.println(fontName);
		}
	}
}
 

Sample Output

.AppleSystemUIFont
Al Bayan
Al Nile
Al Tarikh
American Typewriter
Andale Mono
Apple Braille
Apple Chancery
Apple Color Emoji
Apple SD Gothic Neo
Apple Symbols
AppleGothic
AppleMyungjo
Arial
Arial Black
Arial Hebrew
Arial Hebrew Scholar
Arial Narrow
Arial Rounded MT Bold
Arial Unicode MS
Avenir
Avenir Next
Avenir Next Condensed
Ayuthaya
Baghdad
Bangla MN
Bangla Sangam MN
Baskerville
Beirut
Big Caslon
Bodoni 72
Bodoni 72 Oldstyle
Bodoni 72 Smallcaps
Bodoni Ornaments
Bogle
Bradley Hand
Brush Script MT
Chalkboard
Chalkboard SE
Chalkduster
Charter
Cochin
Comic Sans MS
Copperplate
Corsiva Hebrew
Courier
Courier New
DIN Alternate
DIN Condensed
Damascus
DecoType Naskh
Devanagari MT
Devanagari Sangam MN
Didot
Diwan Kufi
Diwan Thuluth
Euphemia UCAS
Farah
Farisi
Futura
GB18030 Bitmap
Galvji
Geeza Pro
Geneva
Georgia
Gill Sans
Gujarati MT
Gujarati Sangam MN
Gurmukhi MN
Gurmukhi MT
Gurmukhi Sangam MN
Heiti SC
Heiti TC
Helvetica
Helvetica Neue
Herculanum
Hiragino Maru Gothic ProN
Hiragino Mincho ProN
Hiragino Sans
Hiragino Sans GB
Hoefler Text
ITF Devanagari
ITF Devanagari Marathi
Impact
InaiMathi
Kailasa
Kannada MN
Kannada Sangam MN
Kefa
Khmer MN
Khmer Sangam MN
Kohinoor Bangla
Kohinoor Devanagari
Kohinoor Gujarati
Kohinoor Telugu
Kokonor
Krungthep
KufiStandardGK
Lao MN
Lao Sangam MN
Lucida Bright
Lucida Grande
Lucida Sans
Lucida Sans Typewriter
Luminari
Malayalam MN
Malayalam Sangam MN
Marker Felt
Menlo
Microsoft Sans Serif
Mishafi
Mishafi Gold
Monaco
Monospaced
Mshtakan
Mukta Mahee
Muna
Myanmar MN
Myanmar Sangam MN
Nadeem
New Peninim MT
Noteworthy
Noto Nastaliq Urdu
Noto Sans Javanese
Noto Sans Kannada
Noto Sans Myanmar
Noto Sans Oriya
Noto Serif Myanmar
Optima
Oriya MN
Oriya Sangam MN
PT Mono
PT Sans
PT Sans Caption
PT Sans Narrow
PT Serif
PT Serif Caption
Palatino
Papyrus
Phosphate
PingFang HK
PingFang SC
PingFang TC
Plantagenet Cherokee
Raanana
Rockwell
STIXGeneral
STIXIntegralsD
STIXIntegralsSm
STIXIntegralsUp
STIXIntegralsUpD
STIXIntegralsUpSm
STIXNonUnicode
STIXSizeFiveSym
STIXSizeFourSym
STIXSizeOneSym
STIXSizeThreeSym
STIXSizeTwoSym
STIXVariants
STSong
Sana
SansSerif
Sathu
Savoye LET
Serif
Shree Devanagari 714
SignPainter
Silom
Sinhala MN
Sinhala Sangam MN
Skia
Snell Roundhand
Songti SC
Songti TC
Sukhumvit Set
Symbol
System
Tahoma
Tamil MN
Tamil Sangam MN
Telugu MN
Telugu Sangam MN
Thonburi
Times
Times New Roman
Trattatello
Trebuchet MS
Verdana
Waseem
Webdings
Wingdings
Wingdings 2
Wingdings 3
Zapf Dingbats
Zapfino

 

 

 

 

Previous                                                    Next                                                    Home