Introdução ao JavaFX Color

No JavaFX, a cor pode ser usada para preencher as diferentes formas, como retângulo, elipse, círculo, etc. Usando métodos diferentes, é possível criar nossos tons de cor. Uma vez feito, ele pode ser passado para o objeto de tinta no método setFill (). Neste documento, discutiremos várias técnicas para criar cores.

Como criar cores no JavaFX?

Como já foi dito, as cores podem ser feitas usando métodos diferentes:

1. Usando o nome da cor

Nesse método, o nome da cor será usado para criar uma cor. Isso é feito com a ajuda da classe javafx.scene.paint.Color, onde todas as cores estão disponíveis como propriedades da classe. O nome da cor pode ser passado para o objeto da classe Paint no método setFill (). Aqui está um exemplo de criação de cores usando um nome de cor.

Código:

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

Resultado:

2. Usando Web Color

O próximo método para criar cores é usar uma cor da Web. Aqui, o método Color.web () na classe javafx.scene.paint.color será usado onde serão passados ​​2 parâmetros, como o valor hexadecimal da cor e um canal alfa. O segundo parâmetro Alpha-channel é um parâmetro opcional que indica a opacidade da cor. Alfa tem um intervalo de valores de 0, 0 a 1, 0 e também pode ser implícito ou explícito, como mostrado abaixo.

Sintaxe:

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

Código:

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

Resultado:

3. Usando HSB Color

No JavaFX, a cor também pode ser criada usando a combinação Matiz, Saturação e Brilho, que é conhecida como cor HSB. Isso é feito com a ajuda da classe javafx.scene.paint.Color que consiste em um método Color.hsb () que insere 3 números inteiros, como h, se eb.

Código:

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

Resultado:

4. Usando cores RGB

Um dos métodos mais comuns para criar cores é o sistema de cores RGB, onde vermelho, verde e azul são os três componentes. Isso é feito com a ajuda da classe javafx.scene.paint.Color que consiste em um método rgb () que insere 3 números inteiros r, g e b.

Código:

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

Resultado:

Conclusão

As cores são usadas para preencher as formas e isso pode ser feito usando métodos diferentes. Todos esses métodos são abordados neste documento.

Artigo recomendado

Este é um guia para o JavaFX Color. Aqui discutimos para criar cores no JavaFX usando vários métodos, juntamente com a implementação e saída de código. você também pode consultar nossos artigos sugeridos para saber mais -

  1. Os 5 principais layouts JavaFX
  2. Aplicativos JavaFX com recursos
  3. JavaFX vs Swing | Top 6 Comparação
  4. Etiqueta JavaFX (exemplos)
  5. Como criar a caixa de seleção no JavaFX com exemplos?
  6. Exemplos de caixa de seleção no Bootstrap
  7. Guia Completo para Métodos do JavaFX VBox
  8. Guia de menus no JavaFX com exemplos?