Introduzione a JavaFX Color

In JavaFX, il colore può essere utilizzato per riempire le diverse forme come rettangolo, ellisse, cerchio, ecc. Usando metodi diversi, è possibile creare le nostre sfumature di colore. Una volta creato, può essere passato all'oggetto paint nel metodo setFill (). In questo documento, discuteremo di diverse tecniche per creare il colore.

Come creare il colore in JavaFX?

Come già detto, i colori possono essere realizzati con diversi metodi:

1. Utilizzo del nome del colore

In questo metodo, il nome del colore verrà utilizzato per creare un colore. Viene fatto con l'aiuto della classe javafx.scene.paint.Color dove tutti i colori sono disponibili come proprietà della classe. Il nome del colore può essere passato all'oggetto della classe Paint nel metodo setFill (). Ecco un esempio di creazione del colore usando un nome colore.

Codice:

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

Produzione:

2. Utilizzo del colore Web

Il prossimo metodo per creare il colore è usando un colore web. Qui, verrà utilizzato il metodo Color.web () nella classe javafx.scene.paint.color dove verranno passati 2 parametri come il valore esadecimale del colore e un canale alfa. Il secondo parametro Alpha-channel è un parametro opzionale che indica l'opacità del colore. Alpha ha un intervallo di valori da 0, 0 a 1, 0 e inoltre, può essere implicito o esplicito come mostrato di seguito.

Sintassi:

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

Codice:

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

Produzione:

3. Utilizzo di HSB Color

In JavaFX, il colore può anche essere creato usando la combinazione Tonalità, Saturazione e Luminosità, nota come colore HSB. Viene fatto con l'aiuto della classe javafx.scene.paint.Color che consiste in un metodo Color.hsb () che immette 3 numeri interi come h, s e b.

Codice:

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

Produzione:

4. Utilizzo di RGB Color

Uno dei metodi più comuni per creare il colore è il sistema di colore RGB in cui rosso, verde e blu sono i 3 componenti. Viene fatto con l'aiuto della classe javafx.scene.paint.Color che consiste in un metodo rgb () che immette 3 numeri interi r, g e b.

Codice:

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

Produzione:

Conclusione

I colori sono usati per riempire le forme e può essere fatto usando diversi metodi. Tutti questi metodi sono trattati in questo documento.

Articolo raccomandato

Questa è una guida a JavaFX Color. Qui discutiamo di creare colore in JavaFX usando vari metodi insieme all'implementazione e all'output del codice. puoi anche consultare i nostri articoli suggeriti per saperne di più -

  1. I 5 migliori layout JavaFX
  2. Applicazioni JavaFX con funzionalità
  3. JavaFX vs Swing | Top 6 Confronto
  4. Etichetta JavaFX (esempi)
  5. Come creare la casella di controllo in JavaFX con esempi?
  6. Esempi di casella di controllo in Bootstrap
  7. Guida completa ai metodi di VBox JavaFX
  8. Guida al menu in JavaFX con esempi?

Categoria: