简体   繁体   English

JavaFx如何在CSS中使用java生成的RGB颜色

[英]JavaFx how to use java generated RGB color in CSS

I am working on a project where I try to find the most common color from a picture. 我正在开展一个项目,我试图从图片中找到最常见的颜色。 My code for finding this works, but I want to set the background color of my scene to the rgb color I found. 我找到这个的代码有效,但我想将场景的背景颜色设置为我找到的rgb颜色。

I know how to set the background color of my scene using css but I don't have a clue how I can use my methods in there. 我知道如何使用css设置场景的背景颜色,但我不知道如何在那里使用我的方法。 If it is not possible, is there another way I can set the background color? 如果不可能,还有其他方法可以设置背景颜色吗?

The css code right now: 现在的CSS代码:

.root{
-fx-background-color: rgb(50,50,50);
-fx-font-size: 11pt; 
}

The JavaFx code right now: JavaFx代码现在:

Stage window;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    ColorFinder finder= new CollorFinder("/imgs/picture.jpg");
    int r = finder.rood();
    int g = finder.groen();      //calling my method and setting r g & b
    int b = finder.blauw();

    window = primaryStage;
    window.setTitle("Color");

    Label text = new Label("Most popular color:");
    Label rgb = new Label("rgb("+r+","+g+","+b + ")");



    VBox layout = new VBox(20);
    layout.getChildren().addAll(text,rgb);
    layout.setAlignment(Pos.CENTER);

    Scene scene = new Scene(layout, 300,200);
    String css = gui.class.getResource("styles.css").toExternalForm();
    scene.getStylesheets().add(css);
    window.setScene(scene);
    window.show();
}
}

What I would like to do in css but is not possible: 我想用css做什么但是不可能:

ColorFinder finder= new CollorFinder("/imgs/picture.jpg");
    int r = finder.rood();
    int g = finder.groen();
    int b = finder
.root{
    -fx-background-color: rgb(r,g,b);
    -fx-font-size: 11pt;
}

There are two approaches: 有两种方法:

  1. Inline style method setStyle(String style) : 内联样式方法setStyle(String style)

     layout.setStyle("-fx-background-color: rgb(" + r + "," + g + ", " + b + ");"); 

    r, g, b values range -> (0 - 255) r, g, b值范围 - >(0 - 255)

  2. Method setBackground(Background value) : 方法setBackground(Background value)

     layout.setBackground(new Background(new BackgroundFill(Color.rgb(r, g, b), CornerRadii.EMPTY, Insets.EMPTY))); 

    r, g, b values range -> (0 - 255) r, g, b值范围 - >(0 - 255)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM