Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Last active June 18, 2020 06:59
Show Gist options
  • Save maxandersen/07ae66cd3e86ae4cf9732b55f8ad5418 to your computer and use it in GitHub Desktop.
Save maxandersen/07ae66cd3e86ae4cf9732b55f8ad5418 to your computer and use it in GitHub Desktop.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.2.0
//DEPS org.openjfx:javafx-base:11.0.2:${os.detected.jfxname}
//DEPS org.openjfx:javafx-graphics:11.0.2:${os.detected.jfxname}
//DEPS org.openjfx:javafx-controls:11.0.2:${os.detected.jfxname}
import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.Transition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.effect.MotionBlur;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.util.Duration;
public class blm extends Application{
@Override
public void start(Stage stage) throws Exception {
Rectangle rect = new Rectangle(100,100);
rect.setFill(Color.BLACK);
Text blm = new Text("BLACK LIVES MATTER!");
blm.setFill(Color.YELLOW);
blm.setTextAlignment(TextAlignment.CENTER);
StackPane root = new StackPane();
root.getChildren().addAll(rect, blm);
Scene scene = new Scene(root,500,250);
rect.heightProperty().bind(scene.heightProperty());
rect.widthProperty().bind(scene.widthProperty());
int duaration = 5000;
TextSizeTransition trans = new TextSizeTransition(blm, 10, 250,Duration.millis(duaration));
FadeTransition fade = new FadeTransition();
fade.setDuration(Duration.millis(duaration));
fade.setFromValue(0);
fade.setToValue(10);
fade.setAutoReverse(true);
fade.setNode(rect);
fade.play();
trans.play();
//Configuring Group and Scene
stage.setScene(scene);
//primaryStage.setTitle("Translate Transition example");
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.addEventHandler(KeyEvent.KEY_RELEASED, (KeyEvent event) -> {
if (KeyCode.ESCAPE == event.getCode()) {
stage.close();
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class TextSizeTransition extends Transition {
private Text UIcontrol; // a little generic -> subclasses: ButtonBase, Cell, Label, TitledPane
private int start, end; // initial and final size of the text
public TextSizeTransition(Text UIcontrol, int start, int end, Duration duration) {
this.UIcontrol = UIcontrol;
this.start = start;
this.end = end - start; // minus start because of (end * frac) + start in interpolate()
setCycleDuration(duration);
//setInterpolator(Interpolator.EASE_IN);
//setCycleCount(100);
// and a lot of other methods
}
@Override
protected void interpolate(double frac) {
// frac value goes from 0 to 1
// when frac is zero -> size is start
// when frac is 1 -> size is end + start
//(that's why we this.end = end - start; above to back to original end value)
int size = (int) ((end * frac) + start);
if(size<=end) {
UIcontrol.setFont(Font.font(size));
}else { // once the size reaches the destination (i.e. end value)
// back to the start size if you want
//UIcontrol.setFont(Font.font(start));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment