Saturday, 24 October 2020

JavaFX: Working with Colors

 

You can apply colors to fill a shape, draw border line colors etc.,

 

How to define Colors?

Colors can be defined in following ways.

a.   Constants

b.   Constructors

c.    Utility methods.

d.   Using Hex Web values

e.   Using hsb color model

 

Using Color constants

Color class defines multiple public static constants, you can use them directly. There are more than 200 built-in color constants available in Java Color class.

 

Example

Color c1 = Color.GREEN;

 

Using Constructors

Color class provides following constructor to define an instance of Color.

 

public Color(double red, double green, double blue, double opacity)

 

Example

Color c2 = new Color(0, 1, 0, 1);

 

Using Utility methods

Color class provides following utility methods to define color instance.

 

public static Color color(double red, double green, double blue)

public static Color color(double red, double green, double blue, double opacity)

public static Color rgb(int red, int green, int blue)

public static Color rgb(int red, int green, int blue, double opacity)

 

Example

Color c3= Color.color(0, 1, 0);

Color c4 = Color.color(0, 1, 0, 1);

 

Color c5 = Color.rgb(0, 1, 0);

Color c6 = Color.rgb(0, 1, 0, 1);

 

 

Using Hex Web values

Color class provides ‘web’ method to create an RGB color specified with an HTML or CSS attribute string.

 

public static Color web(String colorString)

Creates an RGB color specified with an HTML or CSS attribute string.

 

public static Color web(String colorString, double opacity)

Creates an RGB color specified with an HTML or CSS attribute string and with given opacity.

 

Examples

Color.web("green");

Color.web("green", 0.5);

Color.web("ox00ff00");

Color.web("ox00ff00", 0.5);

Color.web("rgb(100%,50%,50%)");

Color.web("rgb(255,50%,50%,0.25)");

 

Using hsb color model

public static Color hsb(double hue, double saturation, double brightness)

Creates an opaque Color based on the specified values in the HSB color model.

 

public static Color hsb(double hue, double saturation, double brightness, double opacity)

Creates a Color based on the specified values in the HSB color model, and a given opacity.

 

Example

Color c9 = Color.hsb(10, 0.5, 0.5);

Color c10 = Color.hsb(10, 0.5, 0.5, 0.5);

 

Find the below working application.

 

ColorDemo.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.ColorController">

	<Rectangle fx:id="rectangle1" fill="black" width="300" height="100" />
	
	<Rectangle fx:id="rectangle2" fill="#f0fff0" width="200" height="70" />
	
	<Rectangle fx:id="rectangle3" fill="lightgreen" width="100" height="40" />
	
</StackPane>

 

ColorController.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;

public class ColorController implements Initializable {

	@FXML
	private Rectangle rectangle1;

	@FXML
	private Rectangle rectangle2;

	@FXML
	private Rectangle rectangle3;

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

	}

}


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

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

	}

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

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

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

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

}


Output







Previous                                                    Next                                                    Home

javaFX: strokeLineJoin:Defines line join style of a shape

‘strokeLineJoin’ property is used to specify the line join style of a shape.

 

Below table summarizes possible values for strokeLineJoin property.

 

strokeLineJoin

Description

MITER

Joins path segments by extending their outside edges until they meet.

BEVEL

Joins path segments by connecting the outer corners of their wide outlines with a straight segment.

ROUND

Joins path segments by rounding off the corner at a radius of half the line width.

Previous                                                    Next                                                    Home

javaFX: strokeLineCap: Specify end cap style

‘strokeLineCap’ property is used to specify end cap style.

 

Below table summarizes possible value for strokeLineCap.

strokeLineCap

Value

SQUARE

Ends unclosed subpaths and dash segments with a square projection that extends beyond the end of the segment to a distance equal to half of the line width.

BUTT

Ends unclosed subpaths and dash segments with no added decoration.

ROUND

Ends unclosed subpaths and dash segments with a round decoration that has a radius equal to half of the width of the pen.

 

 

Previous                                                    Next                                                    Home

JavaFX: Shape: strokeType: Defines where to draw the stroke

 

'StrokeType' enum define properties which specify where to draw the stroke around the boundary of a Shape node.

 

Following table summarizes possible values for strokeType.

 

Stroke Type

Description

INSIDE

The stroke is applied by extending the boundary of a closed Shape node into its interior by a distance specified by the strokeWidth.

 

OUTSIDE

 

The stroke is applied by extending the boundary of a closed Shape node outside of its interior by a distance specified by the strokeWidth.

 

CENTERED

 

The stroke is applied by thickening the boundary of the Shape node by a distance of half of the strokeWidth on either side of the boundary.

 

 

Example

<Rectangle fx:id="rectangle" arcHeight="30" arcWidth="30" fill="black"

width="300" height="100" smooth="true" stroke="green" strokeWidth="5"

strokeType="inside"/>

 

Find the below working application.

 

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="black"
		 width="300" height="100" smooth="true" stroke="green" strokeWidth="5"
		 strokeType="inside"/>
	
	<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();
	}

}

 

Output


 

 



Previous                                                    Next                                                    Home

Thursday, 22 October 2020

JavaFX: strokeWidth: Set stroke width to a shape

'stroke' property is used to draw a color line around the shape. ‘strokeWidth’ property is used to specify the width of stroke line. Default value for ‘strokeWidth’ is 1.

 

<Rectangle fx:id="rectangle" arcHeight="30" arcWidth="30" fill="black" width="300" height="100" smooth="true" stroke="green" strokeWidth="5"/>

 

Find the below working application.

 

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="black"
		 width="300" height="100" smooth="true" stroke="green" strokeWidth="5"/>
	
	<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();
	}

}

 

Output


 

 

Previous                                                    Next                                                    Home

Tuesday, 20 October 2020

JavaFx: Shape: stroke: Draw outline around the shape

 'stroke' property is used to draw a color line around the shape.

 

Example

<Rectangle fx:id="rectangle" arcHeight="30" arcWidth="30" fill="black" width="300" height="100" smooth="true" stroke="green"/>

 

Above snippet draws a green line around the rectangle shape.

 

Find the below working application.

 

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="black" width="300" height="100" smooth="true" stroke="green"/>
	
	<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();
	}

}

 

Output


 


 

Previous                                                    Next                                                    Home