2017-02-08 8 views
3

私は画像から最も一般的な色を見つけようとするプロジェクトに取り組んでいます。これを見つけるための私のコードは動作しますが、私が見つけたRGBの色に自分のシーンの背景色を設定したいのです。JavaFx CSSでJavaで生成されたRGBカラーを使用する方法

私はcssを使って自分のシーンの背景色を設定する方法を知っていますが、そこで私のメソッドをどのように使うことができるかわかりません。それが可能でない場合、私は背景色を設定する別の方法はありますか?

今CSSコード:今

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

JavaFXのコード:私はCSSでやりたいが、可能ではないだろうか

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

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

答えて

1

2つのアプローチがあります:

  1. インラインスタイル方法setStyle(String style)

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

    r, g, b値範囲 - >(0 - 255)

  2. 方法setBackground(Background value)

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

    r, g, b値が範囲 - >(0から255まで)

+0

ありがとうsoooo!できます! – jdlChicory

+1

問題ありません。答えを正しいものとしてマークすることができます。 – MBec

関連する問題