如何绑定更改按钮和rgb Sliders JavaFX

时间:2018-05-09 07:26:55

标签: java javafx

例如我在Color Change TextField中输入颜色按下颜色改变的按钮,所以我希望rgb滑块能够获得"获得"这种颜色。

例如我输入白色并按下按钮滑块需要更改为255 255 255。

package sample;

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.Slider;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.StrokeType;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;

    import java.text.DecimalFormat;

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    GridPane root = new GridPane();

    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(10);
    grid.setHgap(70);

    //Circle creation
        Circle circle = new Circle(150, Color.rgb(0,0,0));
        circle.setStrokeType(StrokeType.CENTERED);
        circle.setOpacity(0.5);
        circle.minHeight(150);
        circle.minWidth(170);
        circle.setStroke(Color.web("green"));
        circle.setStrokeWidth(5);

    //adding circle to root group
    root.getChildren().add(circle);

    //Opacity Slider
    Slider opacity = new Slider();
    opacity.setMin(0);
    opacity.setValue(0.5);
    opacity.setMax(1);
    Text opacitylevel = new Text("0.5");
    javafx.scene.control.Label opacityCaption = new Label("Opacity Level:");
    grid.add(opacityCaption,0,1);
    grid.add(opacity,1,1);
    grid.add(opacitylevel,2,1);

    //Colour Change
    TextField colour = new TextField();
    javafx.scene.control.Label Colour = new Label("Change Colour:");
    grid.add(Colour,0,2);
    grid.add(colour,1,2);

    Button clbtn = new Button("Change Colour");
    clbtn.setPrefSize(200,10);
    grid.add(clbtn,1,3);

    //formatting
    DecimalFormat df = new DecimalFormat("#.##");

    opacity.valueProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> ov,
                            Number old_val, Number new_val) {
            circle.setOpacity(new_val.doubleValue());
            opacitylevel.setText(df.format((double)new_val));
        }
    });
    //R G B
    Slider R = new Slider();
    R.setMin(0);
    R.setValue(0);
    R.setMax(255);
    Text red = new Text("0");

    javafx.scene.control.Label Rt = new Label("Red:");
    grid.add(Rt,0,4);
    grid.add(R,1,4);
    grid.add(red,2,4);

    Slider G = new Slider();
    G.setMin(0);
    G.setValue(0);
    G.setMax(255);
    Text green = new Text("0");

    javafx.scene.control.Label Gt = new Label("Green:");
    grid.add(Gt,0,5);
    grid.add(G,1,5);
    grid.add(green,2,5);

    Slider B = new Slider();
    B.setMin(0);
    B.setValue(0);
    B.setMax(255);
    Text blue = new Text("0");

    javafx.scene.control.Label Bt = new Label("Blue:");
    grid.add(Bt,0,6);
    grid.add(B,1,6);
    grid.add(blue,2,6);
    root.add(grid,1,0);

    //creating scene
    Scene scene = new Scene(root,800,600,Color.web("white"));

    //centering circle
    circle.setCenterX(170);
    circle.setCenterY(150);

    //Change Colour event

    clbtn.setOnAction(event -> {
        circle.setFill(Color.web(colour.getText()));
    });

    //RGB events

    //red
    R.valueProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> ov,
                            Number old_val, Number new_val) {
            red.setText("" + new_val.intValue());
            circle.setFill(Color.rgb(new_val.intValue(),Integer.parseInt(green.getText()),Integer.parseInt(blue.getText())));
        }
    });

    //green
    G.valueProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> ov,
                            Number old_val, Number new_val) {
            green.setText("" + new_val.intValue());
            circle.setFill(Color.rgb(Integer.parseInt(red.getText()),new_val.intValue(),Integer.parseInt(blue.getText())));
        }
    });

    //blue
    B.valueProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> ov,
                            Number old_val, Number new_val) {
            blue.setText("" + new_val.intValue());
            circle.setFill(Color.rgb(Integer.parseInt(red.getText()),Integer.parseInt(green.getText()),new_val.intValue()));
        }
    });

    primaryStage.setScene(scene);

    primaryStage.show();
}

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

我自己可以意识到这一点,但我不明白如何将红绿蓝与网页颜色分开。

按钮代码,便于搜索。

clbtn.setOnAction(event -> {
    circle.setFill(Color.web(colour.getText()));
});

2 个答案:

答案 0 :(得分:1)

<?php
#Just convert it into a JSON string and decode it 
$your_json =  '{
    "ERROR_CODE": 0,
    "M_USER": {
        "CREATE_DATE": "2018-05-09 13:57:49",
        "CREATE_USER_ID": "t1074567",
        "FACE_PICTURE_FILE_PATH": "<null>",
        "MAIL_ADDRESS": "Testing@gmail.com",
        "NATIVE_LANGUAGE_CD": 102,
        "REQ_LANGUAGE_CD": 102,
        "TERMINAL_ID": "C71B456F-EA16-4734-8C9B-00B0856143DA",
        "TERMINAL_TYPE": 1,
        "TOTAL_GRADE": 0,
        "TRANSLATABLE_FLG": 1,
        "UPDATE_DATE": "2018-05-09 13:57:49",
        "UPDATE_USER_ID": "tdu1074567",
        "USER_ID": "tdu1074567",
        "USER_NAME": "Testing",
        "USER_PWD": "testing123",
        "VALID_FLG": 1
    },
    "TRANS_LANGUAGE": [
        {
            "C": 102,
            "L": 10203
        },
        {
            "C": 101,
            "L": 10101
        }
    ]
}';

$object = json_decode($your_json);

print_r($object);

答案 1 :(得分:0)

clbtn.setOnAction(event -> {
        circle.setFill(Color.web(colour.getText()));
        Color rgb = Color.web(colour.getText());
        double redd = rgb.getRed() * 255;
        red.setText("" + redd);
        R.setValue(redd);
        double greenn = rgb.getGreen() * 255;
        green.setText("" + greenn);
        G.setValue(greenn);
        double bluee = rgb.getBlue() * 255;
        blue.setText("" + bluee);
        B.setValue(greenn);
    });