/************************************************** * Author: Morrison * Date: 27 Oct 202020 **************************************************/ import javafx.application.Application; import javafx.application.Platform; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.layout.HBox; import javafx.scene.layout.BorderPane; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; public class SliderDemo extends Application { private Stage primary; private Color color; public SliderDemo() { color = Color.BLACK; } @Override public void init() { } private Color greyScale(int value) { return Color.rgb(value, 0, 0); } @Override public void start(Stage primary) { this.primary = primary; BorderPane bp = new BorderPane(); HBox hbox = new HBox(); Slider s = new Slider(0, 255, 127); Label level = new Label("127"); Canvas canvas = new Canvas(500,500); hbox.getChildren().addAll(s, level); bp.setTop(hbox); bp.setCenter(canvas); GraphicsContext pen = canvas.getGraphicsContext2D(); pen.setFill(greyScale(127)); pen.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); s.valueProperty().addListener( (observable, oldValue, newValue) -> { level.setText("" + newValue.intValue()); pen.setFill(greyScale(newValue.intValue())); pen.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); }); primary.setScene(new Scene(bp)); primary.show(); } @Override public void stop() { } }