Uvod u JavaFX boju

U JavaFX-u se boja može koristiti za popunjavanje različitih oblika poput pravokutnika, elipse, kruga itd. Korištenjem različitih metoda moguće je napraviti naše nijanse boje. Jednom kada se napravi, može se prenijeti na objekt boje u metodu setFill (). U ovom ćemo dokumentu razgovarati o nekoliko tehnika za stvaranje boje.

Kako stvoriti boju u JavaFX-u?

Kao što je već rečeno, boje se mogu izrađivati ​​različitim metodama:

1. Korištenje naziva boje

U ovoj će se metodi koristiti boja za stvaranje boje. To je učinjeno uz pomoć klase javafx.scene.paint.Color gdje su sve boje dostupne kao svojstva klase. Naziv boje može se prenijeti na objekt klase Paint u metodu setFill (). Evo primjera stvaranja boje pomoću naziva boje.

Kodirati:

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);
)
)

Izlaz:

2. Korištenje web boje

Sljedeća metoda stvaranja boja je upotreba web boje. Ovdje će se upotrijebiti metoda Color.web () u klasi javafx.scene.paint.color gdje će se proslijediti 2 parametra, poput heksade vrijednosti boje i alfa kanala. Drugi parametar Alpha-kanal je izborni parametar koji označava neprozirnost boje. Alpha ima raspon vrijednosti od 0, 0 do 1, 0, a isto tako, može biti implicitna ili eksplicitna kao što je prikazano u nastavku.

Sintaksa:

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

Kodirati:

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); ))

Izlaz:

3. Korištenje HSB boje

U JavaFX-u, boja se može stvoriti i kombinacijom Hue, Saturation i Svjetlina koja je poznata i kao HSB boja. To se vrši uz pomoć klase javafx.scene.paint.Color koja se sastoji od metode Color.hsb () koja unosi 3 cijela broja kao što su h, s i b.

Kodirati:

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);
)
)

Izlaz:

4. Korištenje RGB boje

Jedna od najčešćih metoda stvaranja boja je RGB sustav boja u kojem su Crvena, Zelena i Plava 3 komponente. To se vrši uz pomoć klase javafx.scene.paint.Color koja se sastoji od metode rgb () koja unosi 3 cijela broja r, g i b.

Kodirati:

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);
)
)

Izlaz:

Zaključak

Za popunjavanje oblika koriste se boje a to se može učiniti različitim metodama. Sve ove metode opisane su u ovom dokumentu.

Preporučeni članak

Ovo je vodič za JavaFX boju. Ovdje smo razgovarali o stvaranju boje u JavaFX-u koristeći različite metode zajedno s implementacijom i ishodom koda. također možete proći kroz naše predložene članke da biste saznali više -

  1. Top 5 JavaFX izgleda
  2. JavaFX aplikacije sa značajkama
  3. JavaFX vs Swing | Top 6 usporedba
  4. JavaFX oznaka (primjeri)
  5. Kako stvoriti potvrdni okvir u JavaFX-u s primjerima?
  6. Primjeri potvrdnog okvira u Bootstrapu
  7. Kompletan vodič za metode JavaFX VBox-a
  8. Vodič za izbornik u JavaFX-u s primjerima?

Kategorija: