Introduktion till JavaFX Color

I JavaFX kan färg användas för att fylla olika former som rektangel, ellips, cirkel osv. Genom att använda olika metoder är det möjligt att göra våra nyanser av färg. När den är gjord kan den överföras till målarfärg till metoden setFill (). I det här dokumentet kommer vi att diskutera flera tekniker för att skapa färg.

Hur skapar jag färg i JavaFX?

Som redan nämnts kan färger göras på olika sätt:

1. Använd färgnamnet

I den här metoden används färgnamnet för att skapa en färg. Det görs med hjälp av klass javafx.scene.paint.Color där alla färger är tillgängliga som klassens egenskaper. Färgnamn kan överföras till objektet i måla klass i metoden setFill (). Här är ett exempel på att skapa färg med ett färgnamn.

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

Produktion:

2. Använda webbfärg

Nästa metod för att skapa färg är att använda en webbfärg. Här kommer Color.web () -metoden i klassen javafx.scene.paint.color att användas där 2 parametrar kommer att passeras, till exempel färgens hexvärde och en alfakanal. Den andra parametern Alpha-channel är en valfri parameter som anger färgens opacitet. Alpha har ett intervall av värden 0, 0 till 1, 0 och det kan också vara implicit eller uttryckligt som visas nedan.

Syntax:

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

Produktion:

3. Använda HSB-färg

I JavaFX kan färg också skapas med hjälp av kombination av nyans, mättnad och ljusstyrka, som kallas HSB-färg. Det görs med hjälp av klass javafx.scene.paint.Color som består av en metod Color.hsb () som matar in 3 heltal som h, s och 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);
)
)

Produktion:

4. Använda RGB-färg

En av de vanligaste metoderna för att skapa färg är RGB-färgsystem där röda, gröna och blå är de tre komponenterna. Det görs med hjälp av klass javafx.scene.paint.Color som består av en metod rgb () som matar in 3 heltal r, g och 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);
)
)

Produktion:

Slutsats

Färger används för att fylla formerna och det kan göras med olika metoder. Alla dessa metoder behandlas i detta dokument.

Rekommenderad artikel

Detta är en guide till JavaFX Color. Här diskuterar vi att skapa färg i JavaFX med olika metoder tillsammans med kodimplementering och utdata. du kan också gå igenom våra föreslagna artiklar för att lära dig mer -

  1. Topp 5 JavaFX-layouter
  2. JavaFX-applikationer med funktioner
  3. JavaFX vs Swing | Topp 6 jämförelse
  4. JavaFX-etikett (exempel)
  5. Hur skapar jag kryssrutan i JavaFX med exempel?
  6. Exempel på kryssrutan i Bootstrap
  7. Komplett guide till metoder för JavaFX VBox
  8. Guide till meny i JavaFX med exempel?

Kategori: