javafx color
In JavaFX, the Color
class represents a color. You can use it to specify the color of various graphical objects in your user interface, such as shapes, text, and backgrounds. Here are some examples of how to create and use Color
objects:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; import javafx.stage.Stage; public class MyJavaFXApp extends Application { @Override public void start(Stage primaryStage) { // Create a rectangle with a blue fill color and a black stroke color Rectangle rectangle = new Rectangle(100, 100, Color.BLUE); rectangle.setStroke(Color.BLACK); // Create a text object with a red fill color Text text = new Text("Hello, world!"); text.setFill(Color.RED); // Create a pane with a gray background color Pane pane = new Pane(); pane.setBackground(new Background(new BackgroundFill(Color.GRAY, null, null))); pane.getChildren().addAll(rectangle, text); // Create a scene and set it on the stage Scene scene = new Scene(pane, 300, 250); primaryStage.setScene(scene); // Show the stage primaryStage.show(); } public static void main(String[] args) { launch(args); } }
In this example, we create a Rectangle
object with a blue fill color and a black stroke color, and a Text
object with a red fill color. We also create a Pane
object with a gray background color, and add the Rectangle
and Text
objects to it. We set the Pane
object as the content of the Scene
object, which is displayed on the screen. You can customize the appearance of the Color
objects by setting their RGB values or using predefined constants, such as Color.RED
, Color.GREEN
, Color.BLUE
, and so on.