Thursday 7 September 2017

javafx - directional hover

(Inspired by)/(Copied from): https://codepen.io/manjitkarve/pen/Nvvjxv




  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
import java.util.Arrays;
import java.util.LinkedList;
import java.util.stream.Stream;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane grid = new GridPane();

        ObservableList<BackgroundFill> fills = FXCollections.observableList(new LinkedList<>());
        fills.addListener(new ListChangeListener<BackgroundFill>() {
            @Override
            public void onChanged(Change<? extends BackgroundFill> c) {
                grid.setBackground(new Background(c.getList().toArray(new BackgroundFill[0])));
            }
        });

        Label[] labels = Stream.iterate(1, i -> i + 1).limit(9)
                .map(i -> {
                    Label l = new Label(String.valueOf(i));
                    l.setMaxWidth(Double.MAX_VALUE);
                    l.setMaxHeight(Double.MAX_VALUE);
                    l.setAlignment(Pos.CENTER);

                    BackgroundFill fill;

                    if(i < 9) {
                        String pos = "to ";
                        if(i < 4) {
                            pos += "bottom ";
                            if(i == 1)
                                pos += "right ";
                            if(i == 3)
                                pos += "left ";
                        }
                        else if(i == 4)
                            pos += "right";
                        else if(i == 5)
                            pos += "left";
                        else {
                            pos += "top ";
                            if(i == 6)
                                pos += "right ";
                            if(i == 8)
                                pos += "left ";
                        }
                        fill = new BackgroundFill(Paint.valueOf("linear-gradient("+pos+", tomato 0%, transparent 70%)"), CornerRadii.EMPTY, Insets.EMPTY);
                    }
                    else 
                        fill = new BackgroundFill(Paint.valueOf("radial-gradient(center 50% 50%, radius 100%,  tomato 0%, transparent 110%)"), CornerRadii.EMPTY, Insets.EMPTY);

                    l.hoverProperty().addListener((pro, old, _new) -> {
                        if(_new)
                            fills.add(fill);
                        else
                            fills.remove(fill);
                    });

                    return l;
                }).toArray(Label[]::new);

        Label target = labels[8];
        target.setText("TARGET");

        grid.addRow(0, Arrays.copyOfRange(labels, 0, 3));
        grid.addRow(1, labels[3], target, labels[4]);
        grid.addRow(2, Arrays.copyOfRange(labels, 5, labels.length - 1));

        for (Label l : labels) {
            GridPane.setFillHeight(l, true);
            GridPane.setFillWidth(l, true);
            GridPane.setHgrow(l, Priority.ALWAYS);
            GridPane.setVgrow(l, Priority.ALWAYS);
            GridPane.setColumnSpan(l, 1);
            GridPane.setRowSpan(l, 1);
        };

        Scene scene = new Scene(grid, 300, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

styled using hilite.me



src download

No comments:

Post a Comment