Wprowadzenie do JavaFX Color

W JavaFX kolor może być użyty do wypełnienia różnych kształtów, takich jak prostokąt, elipsa, okrąg itp. Używając różnych metod, można tworzyć nasze odcienie kolorów. Po utworzeniu można go przekazać do obiektu farby do metody setFill (). W tym dokumencie omówimy kilka technik tworzenia kolorów.

Jak stworzyć kolor w JavaFX?

Jak już powiedziano, kolory można tworzyć różnymi metodami:

1. Używając nazwy koloru

W tej metodzie nazwa koloru zostanie użyta do utworzenia koloru. Odbywa się to za pomocą klasy javafx.scene.paint.Color, w której wszystkie kolory są dostępne jako właściwości klasy. Nazwa koloru może zostać przekazana do obiektu klasy Paint do metody setFill (). Oto przykład tworzenia koloru za pomocą nazwy koloru.

Kod:

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

Wynik:

2. Używanie koloru internetowego

Kolejną metodą tworzenia kolorów jest użycie koloru internetowego. Tutaj zastosowana zostanie metoda Color.web () w klasie javafx.scene.paint.color, w której zostaną przekazane 2 parametry, takie jak wartość szesnastkowa koloru i kanał alfa. Drugi parametr Kanał alfa jest parametrem opcjonalnym, który określa krycie koloru. Alpha ma zakres wartości od 0, 0 do 1, 0, a także może być niejawna lub jawna, jak pokazano poniżej.

Składnia:

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

Kod:

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

Wynik:

3. Używanie koloru HSB

W JavaFX kolor można również utworzyć za pomocą kombinacji Barwa, Nasycenie i Jasność, która jest znana jako kolor HSB. Odbywa się to za pomocą klasy javafx.scene.paint.Color, która składa się z metody Color.hsb (), która wprowadza 3 liczby całkowite, takie jak h, s i b.

Kod:

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

Wynik:

4. Korzystanie z koloru RGB

Jedną z najczęstszych metod tworzenia kolorów jest system kolorów RGB, w którym czerwony, zielony i niebieski to 3 składniki. Odbywa się to za pomocą klasy javafx.scene.paint.Color, która składa się z metody rgb (), która wprowadza 3 liczby całkowite r, g i b.

Kod:

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

Wynik:

Wniosek

Kolory służą do wypełniania kształtów i można to zrobić różnymi metodami. Wszystkie te metody zostały omówione w tym dokumencie.

Polecany artykuł

To jest przewodnik po JavaFX Color. Tutaj omawiamy tworzenie kolorów w JavFX przy użyciu różnych metod wraz z implementacją i wyjściem kodu. możesz również przejrzeć nasze sugerowane artykuły, aby dowiedzieć się więcej -

  1. Top 5 układów JavaFX
  2. Aplikacje JavaFX z funkcjami
  3. JavaFX vs Swing | Porównanie 6 najlepszych
  4. Etykieta JavaFX (przykłady)
  5. Jak utworzyć pole wyboru w JavaFX z przykładami?
  6. Przykłady pola wyboru w Bootstrap
  7. Kompletny przewodnik po metodach JavaFX VBox
  8. Przewodnik po menu w JavaFX z przykładami?