Úvod do JavaFX Color

V jazyce JavaFX lze barvu použít k vyplnění různých tvarů, jako je obdélník, elipsa, kruh atd. Pomocí různých metod je možné vytvořit naše odstíny barev. Jakmile je vytvořen, může být předán objektu barvy do metody setFill (). V tomto dokumentu budeme diskutovat o několika technikách vytváření barev.

Jak vytvořit barvu v JavaFXu?

Jak již bylo řečeno, barvy lze vyrábět různými způsoby:

1. Použití názvu barvy

V této metodě bude název barvy použit k vytvoření barvy. Děje se to pomocí třídy javafx.scene.paint.Color, kde jsou všechny barvy dostupné jako vlastnosti třídy. Název barvy lze předat objektu třídy Malování do metody setFill (). Zde je příklad vytváření barev pomocí názvu barvy.

Kód:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application (
//application starts at this point
@Override
public void start(Stage s) (
//create a group gp
Group gp = new Group();
//set the title
s.setTitle("Color sample using color name");
//create a rectangle r1
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width and height of rectangle r1
r1.setWidth(110);
r1.setHeight(140);
//set the color as red by passing color name
r1.setFill(Color.RED);
//set an effect
r1.setEffect(new DropShadow());
//create a rectangle r2
Rectangle r2 = new Rectangle();
//set the x coordinate of rectangle r2
r2.setX(60);
//set the x coordinate of rectangle r2
r2.setY(60);
//set the width of rectangle r2
r2.setWidth(100);
//set the height of rectangle r2
r2.setHeight(150);
//set the color as GREEN by passing color name
r2.setFill(Color.GREEN);
//set an effect
r2.setEffect(new DropShadow());
//add children to the group
gp.getChildren().add(r1);
gp.getChildren().add(r2);
//create a scene sc
Scene sc = new Scene(gp, 700, 450);
//set the scene for the stage
s.setScene(sc);
//display the results
s.show();
)
public static void main(String() args) (
launch (args);
)
)

Výstup:

2. Používání webové barvy

Dalším způsobem vytváření barev je použití webové barvy. Zde se použije metoda Color.web () ve třídě javafx.scene.paint.color, kde budou předány 2 parametry, jako je hexadecimální hodnota barvy a alfa kanál. Druhý parametr alfa kanál je volitelný parametr, který označuje krytí barvy. Alfa má rozsah hodnot 0, 0 až 1, 0 a také může být implicitní nebo explicitní, jak je ukázáno níže.

Syntax:

//Red color and Alpha is implicit
Color.web("#ff0000")
//Red color and Alpha is explicit
Color.web("#ff0000", 1)

Kód:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application (
//application starts at this point
@Override
public void start(Stage s) (
//create a group gp
Group gp = new Group();
//set the title of the stage s
s.setTitle("Color sample using web color");
//create a rectangle r1
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width of rectangle r1
r1.setWidth(100);
//set the height of rectangle r1
r1.setHeight(150);
//set the color of rectangle r1 as red by using color.web method
r1.setFill(Color. web ("#ff0000", 1));
//set an effect
r1.setEffect(new DropShadow());
//create a rectangle r2
Rectangle r2 = new Rectangle();
//set the x coordinate of rectangle r2
r2.setX(60);
//set the x coordinate of rectangle r2
r2.setY(60);
//set the width of rectangle r2
r2.setWidth(100);
//set the height of rectangle r2
r2.setHeight(150);
//set the color of rectangle r2 as black by using color.web method
r2.setFill(Color. web ("#000000", 1));
//set an effect
r2.setEffect(new DropShadow());
//add children to the group
gp.getChildren().add(r1);
gp.getChildren().add(r2);
//create a scene sc
Scene sc = new Scene(gp, 700, 450);
//set the scene for the stage
s.setScene(sc);
//display the results
s.show();
)
public static void main(String() args) (
launch(args); ))

Výstup:

3. Použití barvy HSB

V JavaFXu lze barvu vytvořit také pomocí kombinace odstínů, sytosti a jasu, která se nazývá barva HSB. To se provádí pomocí třídy javafx.scene.paint.Color, která se skládá z metody Color.hsb (), která zadává 3 celá čísla, jako jsou h, s a b.

Kód:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application (
//application starts at this point
@Override
public void start(Stage s) (
//create a group gp
Group gp = new Group();
//set the title of the stage s
s.setTitle("Color sample using HSB");
//create a rectangle r1
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width of rectangle r1
r1.setWidth(100);
//set the height of rectangle r1
r1.setHeight(150);
//set an effect
r1.setEffect(new DropShadow());
//add children to the group
gp.getChildren().add(r1);
//create a scene sc
Scene sc = new Scene(gp, 700, 450, Color. hsb (180, 0, 1));
//set the scene
s.setScene(sc);
//display the results
s.show();
)
public static void main(String() args) (
launch (args);
)
)

Výstup:

4. Používání barev RGB

Jednou z nejčastějších metod vytváření barev je barevný systém RGB, kde jsou 3, červená, zelená a modrá. To se provádí pomocí třídy javafx.scene.paint.Color, která se skládá z metody rgb (), která zadává 3 celá čísla r, g a b.

Kód:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application (
//application starts at this point
@Override
public void start(Stage s) (
//create a group gp
Group gp = new Group();
//set the title of the stage s
s.setTitle("Color sample using RGB");
//create a rectangle r
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width and height of rectangle r1
r1.setWidth(100);
r1.setHeight(140);
r1.setFill(Color. rgb (20, 125, 10, 0.63));
//add children to the group
gp.getChildren().add(r1);
//create a scene sc
Scene sc = new Scene(gp, 700, 450);
//set the scene
s.setScene(sc);
//display the results
s.show();
)
public static void main(String() args) (
launch (args);
)
)

Výstup:

Závěr

Barvy se používají k vyplnění tvarů a lze to provést různými způsoby. Všechny tyto metody jsou popsány v tomto dokumentu.

Doporučený článek

Toto je průvodce JavaFX Color. Zde diskutujeme o vytvoření barvy v JavaFX pomocí různých metod spolu s implementací a výstupem kódu. můžete také projít naše doporučené články a dozvědět se více -

  1. Top 5 rozložení JavaFX
  2. JavaFX Aplikace s funkcemi
  3. JavaFX vs Swing | Porovnání top 6
  4. Štítek JavaFX (příklady)
  5. Jak vytvořit zaškrtávací políčko v JavaFX s příklady?
  6. Příklady zaškrtávacího políčka v Bootstrapu
  7. Kompletní průvodce metodami JavaFX VBox
  8. Průvodce po nabídce v JavaFX s příklady?

Kategorie: