Friday, July 8, 2016

97. Stroke Transition

With stroke transition we can change the border color.


The stroke around a rectangle is changed from blue to green, in 5 seconds.


package ex97;

import javafx.animation.StrokeTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Ex97 extends Application {
  
    @Override
    public void start(Stage stage) {
        
        DropShadow dropShadow = new DropShadow();
        dropShadow.setOffsetX(5);
        dropShadow.setOffsetY(5);
        dropShadow.setColor(Color.GOLD);
        
        Rectangle rect = new Rectangle(300, 100, Color.RED);
        rect.setStrokeWidth(5);
        rect.setArcWidth(30);
        rect.setArcHeight(30);
        rect.setStroke(Color.BLUE);
        rect.setEffect(dropShadow);

        Text text = new Text("StrokeTransition");
        text.setFont(Font.font("Geogia", 32));
        
        StackPane stackPane = new StackPane(rect, text);
        
        StrokeTransition strokeTransition = 
                new StrokeTransition(Duration.seconds(5), rect);
        
        strokeTransition.setToValue(Color.GREEN);
        
        Button button = new Button("Start");
        button.setOnAction(e -> { 
            strokeTransition.play();
            button.setVisible(false);
        });
        
        VBox vbox = new VBox(50, stackPane, button); 
        vbox.setAlignment(Pos.CENTER);
        vbox.setStyle("-fx-background-color: gray");

        Scene scene = new Scene(vbox, 400, 300);
        stage.setTitle("Example 97. StrokeTransition");
        
        stage.setScene(scene);
        stage.show();
    }

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

This is the output during the animation:


No comments:

Post a Comment