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

No comments:

Post a Comment