下面列出了怎么用javafx.scene.effect.Lighting的API类实例代码及写法,或者点击链接到github查看源代码。
private Circle createMovingCircle(Interpolator interpolator, Color color){
Circle circle = new Circle(25,25,35,color);
circle.setOpacity(0.0);
//add effect
circle.setEffect(new Lighting());
//create a timeline for moving the circle
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//create a keyValue for horizontal translation of circle to the position 155px with given interpolator
KeyValue keyValue = new KeyValue(circle.translateXProperty(), 155, interpolator);
//create a keyFrame with duration 4s
KeyFrame keyFrame = new KeyFrame(Duration.seconds(4), keyValue);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
return circle;
}
private Circle createMovingCircle(Interpolator interpolator, Color color){
Circle circle = new Circle(25,25,35,color);
circle.setOpacity(0.0);
//add effect
circle.setEffect(new Lighting());
//create a timeline for moving the circle
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//create a keyValue for horizontal translation of circle to the position 155px with given interpolator
KeyValue keyValue = new KeyValue(circle.translateXProperty(), 155, interpolator);
//create a keyFrame with duration 4s
KeyFrame keyFrame = new KeyFrame(Duration.seconds(4), keyValue);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
return circle;
}
private Circle createMovingCircle(Interpolator interpolator) {
//create a transparent circle
Circle circle = new Circle(45,45, 40, Color.web("1c89f4"));
circle.setOpacity(0);
//add effect
circle.setEffect(new Lighting());
//create a timeline for moving the circle
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//create a keyValue for horizontal translation of circle to the position 155px with given interpolator
KeyValue keyValue = new KeyValue(circle.translateXProperty(), 155, interpolator);
//create a keyFrame with duration 4s
KeyFrame keyFrame = new KeyFrame(Duration.seconds(4), keyValue);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
return circle;
}
protected static Effect createShadowEffect() {
final DropShadow outerShadow = new DropShadow();
outerShadow.setRadius(3);
outerShadow.setSpread(0.2);
outerShadow.setOffsetX(3);
outerShadow.setOffsetY(3);
outerShadow.setColor(new Color(0.3, 0.3, 0.3, 1));
final Distant light = new Distant();
light.setAzimuth(-135.0f);
final Lighting l = new Lighting();
l.setLight(light);
l.setSurfaceScale(3.0f);
final Blend effects = new Blend(BlendMode.MULTIPLY);
effects.setTopInput(l);
effects.setBottomInput(outerShadow);
return effects;
}
private static Effect createShadowEffect() {
DropShadow outerShadow = new DropShadow();
outerShadow.setRadius(3);
outerShadow.setSpread(0.2);
outerShadow.setOffsetX(3);
outerShadow.setOffsetY(3);
outerShadow.setColor(new Color(0.3, 0.3, 0.3, 1));
Distant light = new Distant();
light.setAzimuth(-135.0f);
Lighting l = new Lighting();
l.setLight(light);
l.setSurfaceScale(3.0f);
Blend effects = new Blend(BlendMode.MULTIPLY);
effects.setTopInput(l);
effects.setBottomInput(outerShadow);
return effects;
}
public TimelineSample() {
super(280,120);
//create a circle
final Circle circle = new Circle(25,25, 20, Color.web("1c89f4"));
circle.setEffect(new Lighting());
//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//one can start/pause/stop/play animation by
//timeline.play();
//timeline.pause();
//timeline.stop();
//timeline.playFromStart();
//add the following keyframes to the timeline
timeline.getKeyFrames().addAll
(new KeyFrame(Duration.ZERO,
new KeyValue(circle.translateXProperty(), 0)),
new KeyFrame(new Duration(4000),
new KeyValue(circle.translateXProperty(), 205)));
getChildren().add(createNavigation());
getChildren().add(circle);
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Timeline rate", timeline.rateProperty(), -4d, 4d)
//TODO it is possible to do it for integer?
);
// END REMOVE ME
}
private void configureEffect() {
handEffect.setOffsetX(radius / 40);
handEffect.setOffsetY(radius / 40);
handEffect.setRadius(6);
handEffect.setColor(Color.web("#000000"));
Lighting lighting = new Lighting();
Light.Distant light = new Light.Distant();
light.setAzimuth(225);
lighting.setLight(light);
handEffect.setInput(lighting);
handEffectGroup.setEffect(handEffect);
}
private Rectangle createBackground(Color stroke, Color fill) {
Rectangle background = new Rectangle(14, 17, fill);
background.setStroke(stroke);
background.setStrokeWidth(2);
background.setEffect(new Lighting());
background.setCache(true);
return background;
}
public StageSample() {
//create a button for initializing our new stage
Button button = new Button("Create a Stage");
button.setStyle("-fx-font-size: 24;");
button.setDefaultButton(true);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent t) {
final Stage stage = new Stage();
//create root node of scene, i.e. group
Group rootGroup = new Group();
//create scene with set width, height and color
Scene scene = new Scene(rootGroup, 200, 200, Color.WHITESMOKE);
//set scene to stage
stage.setScene(scene);
//center stage on screen
stage.centerOnScreen();
//show the stage
stage.show();
//add some node to scene
Text text = new Text(20, 110, "JavaFX");
text.setFill(Color.DODGERBLUE);
text.setEffect(new Lighting());
text.setFont(Font.font(Font.getDefault().getFamily(), 50));
//add text to the main root group
rootGroup.getChildren().add(text);
}
});
getChildren().add(button);
}
public TimelineSample() {
super(280,120);
//create a circle
final Circle circle = new Circle(25,25, 20, Color.web("1c89f4"));
circle.setEffect(new Lighting());
//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//one can start/pause/stop/play animation by
//timeline.play();
//timeline.pause();
//timeline.stop();
//timeline.playFromStart();
//add the following keyframes to the timeline
timeline.getKeyFrames().addAll
(new KeyFrame(Duration.ZERO,
new KeyValue(circle.translateXProperty(), 0)),
new KeyFrame(new Duration(4000),
new KeyValue(circle.translateXProperty(), 205)));
getChildren().add(createNavigation());
getChildren().add(circle);
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Timeline rate", timeline.rateProperty(), -4d, 4d)
//TODO it is possible to do it for integer?
);
// END REMOVE ME
}
private void configureEffect() {
handEffect.setOffsetX(radius / 40);
handEffect.setOffsetY(radius / 40);
handEffect.setRadius(6);
handEffect.setColor(Color.web("#000000"));
Lighting lighting = new Lighting();
Light.Distant light = new Light.Distant();
light.setAzimuth(225);
lighting.setLight(light);
handEffect.setInput(lighting);
handEffectGroup.setEffect(handEffect);
}
private Rectangle createBackground(Color stroke, Color fill) {
Rectangle background = new Rectangle(14, 17, fill);
background.setStroke(stroke);
background.setStrokeWidth(2);
background.setEffect(new Lighting());
background.setCache(true);
return background;
}
public StageSample() {
//create a button for initializing our new stage
Button button = new Button("Create a Stage");
button.setStyle("-fx-font-size: 24;");
button.setDefaultButton(true);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent t) {
final Stage stage = new Stage();
//create root node of scene, i.e. group
Group rootGroup = new Group();
//create scene with set width, height and color
Scene scene = new Scene(rootGroup, 200, 200, Color.WHITESMOKE);
//set scene to stage
stage.setScene(scene);
//center stage on screen
stage.centerOnScreen();
//show the stage
stage.show();
//add some node to scene
Text text = new Text(20, 110, "JavaFX");
text.setFill(Color.DODGERBLUE);
text.setEffect(new Lighting());
text.setFont(Font.font(Font.getDefault().getFamily(), 50));
//add text to the main root group
rootGroup.getChildren().add(text);
}
});
getChildren().add(button);
}
private void configureEffect() {
handEffect.setOffsetX(radius / 40);
handEffect.setOffsetY(radius / 40);
handEffect.setRadius(6);
handEffect.setColor(Color.web("#000000"));
Lighting lighting = new Lighting();
Light.Distant light = new Light.Distant();
light.setAzimuth(225);
lighting.setLight(light);
handEffect.setInput(lighting);
handEffectGroup.setEffect(handEffect);
}
private Rectangle createBackground(Color stroke, Color fill) {
Rectangle background = new Rectangle(14, 17, fill);
background.setStroke(stroke);
background.setStrokeWidth(2);
background.setEffect(new Lighting());
background.setCache(true);
return background;
}
private Shape makeGrid() {
Shape shape = new Rectangle((COLUMNS + 1) * TILE_SIZE, (ROWS + 1) * TILE_SIZE);
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < COLUMNS; x++) {
Circle circle = new Circle(TILE_SIZE / 2);
circle.setCenterX(TILE_SIZE / 2);
circle.setCenterY(TILE_SIZE / 2);
circle.setTranslateX(x * (TILE_SIZE + 5) + TILE_SIZE / 4);
circle.setTranslateY(y * (TILE_SIZE + 5) + TILE_SIZE / 4);
shape = Shape.subtract(shape, circle);
}
}
Light.Distant light = new Light.Distant();
light.setAzimuth(45.0);
light.setElevation(30.0);
Lighting lighting = new Lighting();
lighting.setLight(light);
lighting.setSurfaceScale(5.0);
shape.setFill(Color.BLUE);
shape.setEffect(lighting);
return shape;
}
public TimelineEventsSample() {
super(70,70);
//create a circle with effect
final Circle circle = new Circle(20, Color.rgb(156,216,255));
circle.setEffect(new Lighting());
//create a text inside a circle
final Text text = new Text (i.toString());
text.setStroke(Color.BLACK);
//create a layout for circle with text inside
final StackPane stack = new StackPane();
stack.getChildren().addAll(circle, text);
stack.setLayoutX(30);
stack.setLayoutY(30);
//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//one can add a specific action when each frame is started. There are one or more frames during
// executing one KeyFrame depending on set Interpolator.
timer = new AnimationTimer() {
@Override
public void handle(long l) {
text.setText(i.toString());
i++;
}
};
//create a keyValue with factory: scaling the circle 2times
KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);
KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);
//create a keyFrame, the keyValue is reached at time 2s
Duration duration = Duration.seconds(2);
//one can add a specific action when the keyframe is reached
EventHandler<ActionEvent> onFinished = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
stack.setTranslateX(java.lang.Math.random()*200-100);
//reset counter
i = 0;
}
};
KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
getChildren().add(stack);
}
public static Node createIconContent() {
Group rectangleGroup = new Group();
double xStart = 5.0;
double xOffset = 30.0;
double yPos = 230.0;
double barWidth = 22.0;
double barDepth = 7.0;
Group base1Group = createRectangle(new Color(0.2, 0.12, 0.1, 1.0),
xStart + 135, yPos + 20.0, barWidth*11.5, 10.0);
Group base2Group = createRectangle(new Color(0.2, 0.12, 0.1, 1.0),
xStart + 135, yPos - 20.0, barWidth*11.5, 10.0);
Group bar1Group = createRectangle(Color.PURPLE,
xStart + 1*xOffset, yPos, barWidth, 100.0);
Group bar2Group = createRectangle(Color.BLUEVIOLET,
xStart + 2*xOffset, yPos, barWidth, 95.0);
Group bar3Group = createRectangle(Color.BLUE,
xStart + 3*xOffset, yPos, barWidth, 90.0);
Group bar4Group = createRectangle(Color.GREEN,
xStart + 4*xOffset, yPos, barWidth, 85.0);
Group bar5Group = createRectangle(Color.GREENYELLOW,
xStart + 5*xOffset, yPos, barWidth, 80.0);
Group bar6Group = createRectangle(Color.YELLOW,
xStart + 6*xOffset, yPos, barWidth, 75.0);
Group bar7Group = createRectangle(Color.ORANGE,
xStart + 7*xOffset, yPos, barWidth, 70.0);
Group bar8Group = createRectangle(Color.RED,
xStart + 8*xOffset, yPos, barWidth, 65.0);
Light.Point light = new Light.Point();
light.setX(-20);
light.setY(-20);
light.setZ(100);
Lighting l = new Lighting();
l.setLight(light);
l.setSurfaceScale(1.0f);
bar1Group.setEffect(l);
bar2Group.setEffect(l);
bar3Group.setEffect(l);
bar4Group.setEffect(l);
bar5Group.setEffect(l);
bar6Group.setEffect(l);
bar7Group.setEffect(l);
bar8Group.setEffect(l);
rectangleGroup.getChildren().add(base1Group);
rectangleGroup.getChildren().add(base2Group);
rectangleGroup.getChildren().add(bar1Group);
rectangleGroup.getChildren().add(bar2Group);
rectangleGroup.getChildren().add(bar3Group);
rectangleGroup.getChildren().add(bar4Group);
rectangleGroup.getChildren().add(bar5Group);
rectangleGroup.getChildren().add(bar6Group);
rectangleGroup.getChildren().add(bar7Group);
rectangleGroup.getChildren().add(bar8Group);
rectangleGroup.setScaleX(0.4);
rectangleGroup.setScaleY(0.4);
return rectangleGroup;
}
public TimelineEventsSample() {
super(70,70);
//create a circle with effect
final Circle circle = new Circle(20, Color.rgb(156,216,255));
circle.setEffect(new Lighting());
//create a text inside a circle
final Text text = new Text (i.toString());
text.setStroke(Color.BLACK);
//create a layout for circle with text inside
final StackPane stack = new StackPane();
stack.getChildren().addAll(circle, text);
stack.setLayoutX(30);
stack.setLayoutY(30);
//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//one can add a specific action when each frame is started. There are one or more frames during
// executing one KeyFrame depending on set Interpolator.
timer = new AnimationTimer() {
@Override
public void handle(long l) {
text.setText(i.toString());
i++;
}
};
//create a keyValue with factory: scaling the circle 2times
KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);
KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);
//create a keyFrame, the keyValue is reached at time 2s
Duration duration = Duration.seconds(2);
//one can add a specific action when the keyframe is reached
EventHandler<ActionEvent> onFinished = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
stack.setTranslateX(java.lang.Math.random()*200-100);
//reset counter
i = 0;
}
};
KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
getChildren().add(stack);
}
public static Node createIconContent() {
Group rectangleGroup = new Group();
double xStart = 5.0;
double xOffset = 30.0;
double yPos = 230.0;
double barWidth = 22.0;
double barDepth = 7.0;
Group base1Group = createRectangle(new Color(0.2, 0.12, 0.1, 1.0),
xStart + 135, yPos + 20.0, barWidth*11.5, 10.0);
Group base2Group = createRectangle(new Color(0.2, 0.12, 0.1, 1.0),
xStart + 135, yPos - 20.0, barWidth*11.5, 10.0);
Group bar1Group = createRectangle(Color.PURPLE,
xStart + 1*xOffset, yPos, barWidth, 100.0);
Group bar2Group = createRectangle(Color.BLUEVIOLET,
xStart + 2*xOffset, yPos, barWidth, 95.0);
Group bar3Group = createRectangle(Color.BLUE,
xStart + 3*xOffset, yPos, barWidth, 90.0);
Group bar4Group = createRectangle(Color.GREEN,
xStart + 4*xOffset, yPos, barWidth, 85.0);
Group bar5Group = createRectangle(Color.GREENYELLOW,
xStart + 5*xOffset, yPos, barWidth, 80.0);
Group bar6Group = createRectangle(Color.YELLOW,
xStart + 6*xOffset, yPos, barWidth, 75.0);
Group bar7Group = createRectangle(Color.ORANGE,
xStart + 7*xOffset, yPos, barWidth, 70.0);
Group bar8Group = createRectangle(Color.RED,
xStart + 8*xOffset, yPos, barWidth, 65.0);
Light.Point light = new Light.Point();
light.setX(-20);
light.setY(-20);
light.setZ(100);
Lighting l = new Lighting();
l.setLight(light);
l.setSurfaceScale(1.0f);
bar1Group.setEffect(l);
bar2Group.setEffect(l);
bar3Group.setEffect(l);
bar4Group.setEffect(l);
bar5Group.setEffect(l);
bar6Group.setEffect(l);
bar7Group.setEffect(l);
bar8Group.setEffect(l);
rectangleGroup.getChildren().add(base1Group);
rectangleGroup.getChildren().add(base2Group);
rectangleGroup.getChildren().add(bar1Group);
rectangleGroup.getChildren().add(bar2Group);
rectangleGroup.getChildren().add(bar3Group);
rectangleGroup.getChildren().add(bar4Group);
rectangleGroup.getChildren().add(bar5Group);
rectangleGroup.getChildren().add(bar6Group);
rectangleGroup.getChildren().add(bar7Group);
rectangleGroup.getChildren().add(bar8Group);
rectangleGroup.setScaleX(0.4);
rectangleGroup.setScaleY(0.4);
return rectangleGroup;
}
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 260,100));
//create a circle with effect
final Circle circle = new Circle(20, Color.rgb(156,216,255));
circle.setEffect(new Lighting());
//create a text inside a circle
final Text text = new Text (i.toString());
text.setStroke(Color.BLACK);
//create a layout for circle with text inside
final StackPane stack = new StackPane();
stack.getChildren().addAll(circle, text);
stack.setLayoutX(30);
stack.setLayoutY(30);
//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//one can add a specific action when each frame is started. There are one or more frames during
// executing one KeyFrame depending on set Interpolator.
timer = new AnimationTimer() {
@Override
public void handle(long l) {
text.setText(i.toString());
i++;
}
};
//create a keyValue with factory: scaling the circle 2times
KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);
KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);
//create a keyFrame, the keyValue is reached at time 2s
Duration duration = Duration.seconds(2);
//one can add a specific action when the keyframe is reached
EventHandler<ActionEvent> onFinished = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
stack.setTranslateX(java.lang.Math.random()*200);
//reset counter
i = 0;
}
};
KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
root.getChildren().add(stack);
}