Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created April 16, 2021 12:19
Show Gist options
  • Save Da9el00/fb611cf0848d60e5ecfad77599e8a222 to your computer and use it in GitHub Desktop.
Save Da9el00/fb611cf0848d60e5ecfad77599e8a222 to your computer and use it in GitHub Desktop.
Changing a drop shadow with sliders
package sample;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.shape.Polygon;
import javafx.scene.effect.DropShadow;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private Polygon triangle;
@FXML
private Slider radiusSlider;
@FXML
private Slider offsetXSlider;
@FXML
private Slider offsetYSlider;
DropShadow dropShadow = new DropShadow();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
triangle.setEffect(dropShadow);
radiusSlider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
dropShadow.setRadius(radiusSlider.getValue());
}
});
offsetXSlider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
dropShadow.setOffsetX(offsetXSlider.getValue());
}
});
offsetYSlider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
dropShadow.setOffsetY(offsetYSlider.getValue());
}
});
offsetXSlider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
}
});
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #EAF4D3;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Polygon fx:id="triangle" fill="DODGERBLUE" layoutX="364.0" layoutY="210.0" points="-50.0, 40.0, 50.0, 40.0, 0.0, -60.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" />
<VBox layoutX="94.0" layoutY="119.0" prefHeight="200.0" prefWidth="138.0" spacing="5.0">
<children>
<Slider fx:id="radiusSlider" showTickLabels="true" showTickMarks="true" />
<Slider fx:id="offsetXSlider" min="-100.0" showTickLabels="true" showTickMarks="true" />
<Slider fx:id="offsetYSlider" min="-100.0" showTickLabels="true" showTickMarks="true" />
</children>
</VBox>
<VBox layoutX="36.0" layoutY="119.0" prefHeight="200.0" prefWidth="58.0" spacing="30.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Radius" />
<Text layoutX="10.0" layoutY="23.0" strokeType="OUTSIDE" strokeWidth="0.0" text="X offset" />
<Text layoutX="10.0" layoutY="40.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Y offSet" />
</children>
</VBox>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment