Sunday 21 April 2019

dialog in StackPane



package sam.fx.dialog;

import java.util.List;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Effect;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import sam.reference.WeakPool;

public class StackPaneDialogViewer implements DialogViewer {
    private final Effect effect = new DropShadow();
    private final List<Node> list;
    
    public StackPaneDialogViewer(StackPane root) {
        this.list = root.getChildren();
    }

    private Temp primary;
    private boolean primary_in_use;
    private final WeakPool<Temp> dialogs = new WeakPool<>(Temp::new); 

    @Override
    public Runnable viewDialog(String title, Node node, Runnable onClose) {
       // Node prev = list.isEmpty() ? null : list.get(list.size() - 1);
        Temp temp;

        if(primary_in_use) {
            temp = dialogs.poll();
        } else {
            if(primary == null)
                primary = new Temp();
            temp = primary;
            primary_in_use = true;
        }

        temp.pane.setCenter(node);
        temp.title.setText(title);
        list.add(temp);

        Runnable r = new Runnable() {
            boolean closed = false;

            @Override
            public void run() {
                if(closed)
                    return;

                closed = true;
                temp.close = null;
                temp.pane.setCenter(null);
                list.remove(temp);

                if(temp == primary)
                    primary_in_use = false;
                else
                    dialogs.add(temp);

                if(onClose != null)
                    onClose.run();
            }
        };
        
        temp.close = r;
        return r;
    }
    
    private class Temp extends Group implements EventHandler<ActionEvent> { 
        private Runnable close;
        private final BorderPane pane = new BorderPane();
        private Button button = new Button("X");
        private final Label title = new Label(); 
        
        public Temp() {
            
            BorderPane.setAlignment(title, Pos.CENTER_LEFT);
            title.setMaxWidth(Double.MAX_VALUE);
            title.setPadding(new Insets(0, 0, 0, 10));
            BorderPane top = new BorderPane(title, null, button, null, null);
            top.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(0, 0, 2, 0))));
            top.setPadding(new Insets(2));
            BorderPane.setMargin(top, new Insets(0,0,5,0));
                    
            pane.setTop(top);
            getChildren().add(pane);
            pane.setBackground(new Background(new BackgroundFill(Color.WHITE, new CornerRadii(2), Insets.EMPTY)));
            
            button.getStyleClass().clear();
            button.setPadding(new Insets(3, 7,3, 7));
            button.setBackground(new Background(new BackgroundFill(Color.RED, new CornerRadii(50, true), Insets.EMPTY)));
            button.setTextFill(Color.WHITE);
            button.setFont(Font.font("Consolas", FontWeight.BOLD, -1));
            
            button.setTranslateX(10);
            button.setTranslateY(-10);
            
            button.setOnAction(this);
            
            BorderPane.setAlignment(button, Pos.TOP_RIGHT);
            StackPane.setAlignment(this, Pos.CENTER);
            pane.setEffect(effect);
        }

        @Override
        public void handle(ActionEvent event) {
            close.run();
        }
    }
}


code highlighted using: hilite.me

StackPaneDialogViewer.java

Sunday 1 April 2018

NeonText



import java.io.IOException;

import javafx.animation.Animation;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.effect.Glow;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class NeonText extends Application{

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

    @Override
    public void start(Stage stage) throws Exception {
        Circle circle = new Circle(80);
        circle.setId("circle1");
        circle.setFill(Color.RED);

        Circle circle2 = new Circle(100);
        circle2.setId("circle2");

        StackPane pane = new StackPane(circle, text(), circle2);
        pane.setAlignment(Pos.CENTER);

        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.setWidth(500);
        stage.setHeight(500);
        scene.getStylesheets().add("styles.css");

        stage.show();
    }

    private Text text() {
        Text text = new Text(" LOADING ");
        GaussianBlur gb = new GaussianBlur(5);
        DropShadow dropShadow = new DropShadow(15, Color.WHITE);
        gb.setInput(dropShadow);
        Glow glow = new Glow();
        glow.setInput(gb);

        text.setEffect(glow);

        text.setId("text");
        TranslateTransition transition = new TranslateTransition(Duration.seconds(2), text);
        transition.setByX(-2);
        transition.setCycleCount(Animation.INDEFINITE);
        transition.setFromX(100);
        transition.setToX(-100);

        Platform.runLater(transition::playFromStart);

        return text;
    }
}
styled using hilite.me

Saturday 11 November 2017

Image Carousel

my flavor of : DisplaySelf
 https://github.com/shemnon/javafx-gradle/blob/master/samples/Ensemble8/src/samples/java/ensemble/samples/graphics/displayshelf/DisplayShelf.java




package pagination;

import java.io.File;
import java.net.URI;
import java.util.stream.Stream;

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.effect.Reflection;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

// DisplayShelf
public class DisplayShelf extends Application {
 public static void main(String[] args) {
  launch(args);
 }

 @Override
 public void start(Stage stage) throws Exception {
  HostServices hs = getHostServices();
  String folder = hs.resolveURI(hs.getDocumentBase(), "imgs/animals/");

  int[] index = {0};

  Unit[] images = 
    Stream.of(new File(new URI(folder).getPath()).list())
    .map(name -> hs.resolveURI(folder, name))
    .map(url -> new Unit(url, index[0]++))
    .toArray(Unit[]::new);

  Group container = new Group();
  container.setStyle("-fx-background-color:derive(black, 20%)");
  container.getChildren().addAll(images);

  Slider slider = new Slider(0, images.length - 1, 0);
  slider.setMajorTickUnit(1);
  slider.setMinorTickCount(0);
  slider.setBlockIncrement(1);
  slider.setSnapToTicks(true);
  
  container.getChildren().add(slider);

  Scene scene = new Scene(container, 1000, 400, true);
  scene.setFill(Color.rgb(33,33,33));

  stage.setScene(scene);
  stage.getScene().setCamera(new PerspectiveCamera());
  stage.setResizable(false);
  stage.initStyle(StageStyle.UNDECORATED);
  stage.show();

  slider.translateXProperty().bind(stage.widthProperty().divide(2).subtract(slider.widthProperty().divide(2)));
  slider.setTranslateY(10);

  // FxTransformer.sliders(new DoubleProperty[] {x, z, rotation}, new String[] {"x", "z", "rotation"}, stage, -360, 360).show();
  // new FxTransformer(images, IntStream.range(0, images.length).mapToObj(i -> "images["+i+"]").toArray(String[]::new), stage, -500, 1000).show();

  slider.valueProperty().addListener((p, o, n) -> {
   if(n.doubleValue() == n.intValue())
    Stream.of(images).forEach(u -> u.update(n.intValue(), stage.getWidth(), stage.getHeight()));
  });

  container.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
   if(e.getTarget() instanceof Unit)
    slider.setValue(((Unit)e.getTarget()).index);
  });
  
  Button close = new Button("X");
  close.setOnAction(e -> System.exit(0));
  close.getStyleClass().clear();
  close.setStyle("-fx-text-fill:white;-fx-font-size:15;-fx-font-weight:bold;-fx-font-family:'Comic Sans MS';");

  container.getChildren().add(close);
  close.translateXProperty().bind(stage.widthProperty().subtract(15));

  slider.setValue(5);
 }

 private static class Unit extends ImageView {
  final static Reflection reflection = new Reflection();
  final static Point3D  rotationAxis = new Point3D(0, 90, 1);

  static {
   reflection.setFraction(0.5);
  }

  final int index;
  final Rotate rotate = new Rotate(0, rotationAxis);
  final TranslateTransition transition = new TranslateTransition(Duration.millis(300), this);

  public Unit(String imageUrl, int index) {
   super(imageUrl);
   setEffect(reflection);
   setUserData(index);

   this.index = index;
   getTransforms().add(rotate);
  }
  public void update(int currentIndex, double width, double height) {
   int ef = index - currentIndex;
   double middle = width / 2 - 100;
   boolean b = ef < 0;

   setTranslateY(height/2 - getImage().getHeight()/2);
   double x,z, theta, pivot;

   if(ef == 0) {
    z = -300;
    x = middle;
    theta = 0;
    pivot = b ? 200 : 0;
   }
   else {
    x = middle + ef * 82 + (b ? -147 : 147);
    z = -78.588;
    pivot = b ? 200 : 0 ;
    theta = b ? 46 : -46;
   }
   rotate.setPivotX(pivot);
   rotate.setAngle(theta);

   transition.pause();
   transition.setToX(x);
   transition.setToZ(z);
   transition.play();
  }

 }

}
styled using hilite.me

download code

Thursday 9 November 2017

Floating Boxes




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Random;

import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FloatingBoxes extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    final Random random = new Random();
    final int orbitalCounts = 15;

    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group();

        for (int i = 0; i < 25; i++)
            unit(root);

        CheckBox cb = new CheckBox("show path");
        root.getChildren().add(0, cb);
        cb.setOnAction(e -> {
            Color color = cb.isSelected() ? Color.BLACK : Color.TRANSPARENT;
            root.getChildren().stream()
            .filter(n -> n instanceof Circle)
            .forEach(c -> ((Circle) c).setStroke(color));
        });

        Scene scene = new Scene(root, 500, 500, true);
        scene.setCamera(new PerspectiveCamera(false));
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    private void unit(Group root) {
        Circle c = new Circle(250, 250, 100 + random.nextInt(150), null);
        c.setStroke(Color.BLACK);
        c.setRotate(random.nextInt(360));
        c.setRotationAxis(new Point3D(random.nextInt(360), random.nextInt(360), random.nextInt(360)));
        c.setStroke(Color.TRANSPARENT);
        c.setStrokeWidth(0.2);

        int size = 10 + random.nextInt(10);
        Box box = new Box(size, size, size);
        box.setMaterial(new PhongMaterial(Color.hsb(random.nextInt(360), 0.5, 1)));
        box.setRotate(random.nextInt(360));
        box.setRotationAxis(new Point3D(random.nextInt(360), random.nextInt(360), random.nextInt(360)));

        PathTransition pt = new PathTransition(Duration.seconds(5), c, box);
        RotateTransition rt = new RotateTransition(Duration.seconds(2), box);
        rt.setToAngle(360 + box.getRotate());

        pt.setDelay(Duration.millis(random.nextInt(1000)));
        pt.setCycleCount(Animation.INDEFINITE);
        rt.setCycleCount(Animation.INDEFINITE);
        pt.setInterpolator(Interpolator.LINEAR);
        rt.setInterpolator(Interpolator.LINEAR);
        rt.play();
        pt.play();

        root.getChildren().addAll(box, c);
    }
}
styled using hilite.me

Tuesday 7 November 2017

Bouncing Circles


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
import java.util.Arrays;
import java.util.Random;
import java.util.function.DoubleSupplier;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    final Color fill = new Color(1, 1, 1, 00.05);
    double width, height;
    Circle[] circles = new Circle[0];
    double[][] moveBy = new double[0][2];
    double[] radiusDivs = new double[0];

    @Override
    public void start(Stage stage) throws Exception {
        Random random = new Random();
        Pane pane = new Pane();

        pane.setStyle("-fx-background-color:derive(black, 30%);");

        Scene scene = new Scene(new BorderPane(pane), 300, 300);
        stage.setScene(scene);
        stage.show();

        DoubleSupplier randomFraction = () -> (random.nextBoolean() ? 1 : -1) * random.nextDouble();

        InvalidationListener resizeListener = e -> {
            double oldW = width;
            double oldH = height;

            width = stage.getWidth();
            height = stage.getHeight();

            double min = Math.min(width, height);
            double max = Math.max(width, height);
            int size = (int) Math.sqrt(max);
            int sizePrev = circles.length;
            circles = sizePrev == size ? circles : Arrays.copyOf(circles, size);
            radiusDivs = sizePrev == size ? radiusDivs : Arrays.copyOf(radiusDivs, size);
            moveBy = sizePrev == size ? moveBy : Arrays.copyOf(moveBy, size);

            if (sizePrev < size) {
                for (int i = sizePrev; i < size; i++) {
                    Circle c = circles[i] = new Circle(5, fill);
                    c.getStyleClass().add("circles");

                    double r = radiusDivs[i] = 0.5 + random.nextDouble();
                    c.setRadius((min / 10) * radiusDivs[i]);
                    c.setCenterX(r + random.nextInt((int) (width - r)));
                    c.setCenterY(r + random.nextInt((int) (height - r)));

                    moveBy[i] = new double[] { randomFraction.getAsDouble(), randomFraction.getAsDouble() };
                }
            }
            int limit = sizePrev > size ? size : sizePrev;
            for (int i = 0; i < limit; i++) {
                Circle c = circles[i];

                c.setRadius((min / 10) * radiusDivs[i]);
                c.setCenterX(width * (c.getCenterX() / oldW));
                c.setCenterY(height * (c.getCenterY() / oldH));
            }
            pane.getChildren().setAll(circles);
        };

        stage.widthProperty().addListener(resizeListener);
        stage.heightProperty().addListener(resizeListener);
        resizeListener.invalidated(null);

        new AnimationTimer() {

            @Override
            public void handle(long now) {
                for (int i = 0; i < circles.length; i++) {
                    Circle c = circles[i];
                    double x = c.getCenterX();
                    double y = c.getCenterY();
                    double r = c.getRadius();

                    if (x - r < 0)
                        moveBy[i][0] = random.nextDouble();
                    else if (x + r > width)
                        moveBy[i][0] = -1 * random.nextDouble();
                    if (y - r < 0)
                        moveBy[i][1] = random.nextDouble();
                    else if (y + r > height)
                        moveBy[i][1] = -1 * random.nextDouble();

                    c.setCenterX(x + moveBy[i][0]);
                    c.setCenterY(y + moveBy[i][1]);
                }
            }
        }.start();
    }
}

styled using hilite.me