Uvod v barvo JavaFX
Pri JavaFX lahko barvo uporabimo za polnjenje različnih oblik, kot so pravokotnik, elipsa, krog itd. Z različnimi metodami je mogoče narediti naše barvne odtenke. Ko je narejen, ga lahko posredujemo predmetu barve v metodo setFill (). V tem dokumentu bomo razpravljali o več tehnikah ustvarjanja barve.
Kako ustvariti barvo v JavaFX?
Kot že rečeno, barve lahko izdelamo z različnimi metodami:
1. Uporaba imena barve
Pri tej metodi bo ime barve uporabljeno za ustvarjanje barve. Izvede se s pomočjo razreda javafx.scene.paint.Color, kjer so vse lastnosti na voljo kot lastnosti razreda. Ime barve lahko prenesete predmetu razreda Paint v metodo setFill (). Tu je primer ustvarjanja barve z uporabo barvnega imena.
Koda:
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);
)
)
Izhod:
2. Uporaba spletne barve
Naslednji način ustvarjanja barve je uporaba spletne barve. Tukaj bo uporabljena metoda Color.web () v razredu javafx.scene.paint.color, kjer bosta podana 2 parametra, na primer šestnajsta vrednost barve in alfa kanal. Drugi parameter Alpha-kanal je neobvezen parameter, ki označuje motnost barve. Alpha ima razpon vrednosti od 0, 0 do 1, 0, prav tako pa je lahko implicitno ali nazorno, kot je prikazano spodaj.
Sintaksa:
//Red color and Alpha is implicit
Color.web("#ff0000")
//Red color and Alpha is explicit
Color.web("#ff0000", 1)
Koda:
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); ))
Izhod:
3. Uporaba barve HSB
V JavaFX-u lahko barvo ustvarite tudi s kombinacijo Hue, Saturation in Brightness, ki je znana kot barva HSB. To se naredi s pomočjo razreda javafx.scene.paint.Color, ki je sestavljen iz metode Color.hsb (), ki vnese 3 cela števila, kot so h, s in b.
Koda:
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);
)
)
Izhod:
4. Uporaba RGB Barve
Eden najpogostejših načinov ustvarjanja barv je barvni sistem RGB, v katerem so 3, rdeča, zelena in modra barva. To se naredi s pomočjo razreda javafx.scene.paint.Color, ki je sestavljen iz metode rgb (), ki vnese 3 cela števila r, g in b.
Koda:
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);
)
)
Izhod:
Zaključek
Za zapolnitev oblik se uporabljajo barve, to pa lahko storite z različnimi metodami. Vse te metode so obravnavane v tem dokumentu.
Priporočeni članek
To je vodnik za barvo JavaFX. Tukaj razpravljamo o ustvarjanju barv v JavaFX z uporabo različnih metod, skupaj z izvajanjem in izhodom kode. lahko preberete tudi naše predlagane članke, če želite izvedeti več -
- Najboljših 5 postavitev JavaFX
- JavaFX aplikacije s funkcijami
- JavaFX vs Swing | Top 6 Primerjava
- Oznaka JavaFX (primeri)
- Kako ustvariti potrditveno polje v JavaFX s primeri?
- Primeri potrditvenega polja v programu Bootstrap
- Popoln vodnik o metodah JavaFX VBox
- Vodnik po meniju v JavaFX s primeri?