uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

Toast.java (2342B)


      1 package population;
      2 
      3 import javafx.animation.KeyFrame;
      4 import javafx.animation.KeyValue;
      5 import javafx.animation.Timeline;
      6 import javafx.scene.Scene;
      7 import javafx.scene.layout.StackPane;
      8 import javafx.scene.paint.Color;
      9 import javafx.scene.text.Font;
     10 import javafx.scene.text.FontWeight;
     11 import javafx.scene.text.Text;
     12 import javafx.stage.Stage;
     13 import javafx.stage.StageStyle;
     14 import javafx.util.Duration;
     15 
     16 public class Toast {
     17 	private final int WIDTH_MARGIN = 20;
     18 	private final int HEIGHT = 50;
     19 	private final int FADEOUT_DUR = 500;
     20 	private final int FADEIN_DUR = 500;
     21 	private final int TOAST_DUR = 2500;
     22 	private final int FONT_SIZE = 14;
     23 	private final String FONT = "Verdana";
     24 	private Stage stg;
     25 	private Text txt;
     26 	private String style;
     27 	
     28 	Toast(Country ctry, Stage owner) {
     29 		stg = new Stage();
     30 		txt = new Text(ctry.getIndex() + ", " + 
     31 		    ctry.getName() + ", " +
     32 		    ctry.getCurpop());
     33 		style = "-fx-background-radius: 20;" +
     34 			"-fx-background-color: rgba(0, 0, 0, 0.18);" +
     35 			"-fx-padding: 50px;";
     36 		
     37 	        txt.setFill(Color.BLUE);
     38 	        txt.setFont(Font.font(FONT, FontWeight.BOLD, FONT_SIZE));
     39 	        stg.setWidth(txt.getLayoutBounds().getWidth() + WIDTH_MARGIN);
     40 	        stg.setHeight(HEIGHT);
     41 		stg.setResizable(false);
     42 		stg.initOwner(owner);
     43 		stg.initStyle(StageStyle.TRANSPARENT);
     44 	}
     45 	
     46 	public void show() {
     47 		StackPane root = new StackPane(txt);
     48 		Scene scene = new Scene(root);
     49 		Timeline fintml;
     50 		KeyFrame finkey;
     51 	        
     52 		root.setStyle(style);
     53 		root.setOpacity(0);
     54 		scene.setFill(Color.TRANSPARENT);
     55 		stg.setScene(scene);
     56 		stg.show();
     57 		fintml = new Timeline();
     58 		finkey = new KeyFrame(Duration.millis(FADEIN_DUR),
     59 		    new KeyValue(stg.getScene().getRoot().opacityProperty(), 1));
     60 		fintml.getKeyFrames().add(finkey);  
     61 		fintml.setOnFinished(ev -> closing_animation()); 
     62 		fintml.play();
     63 	}
     64 	
     65 	private void closing_animation() {
     66 		new Thread(() -> {
     67 			try {
     68 				Thread.sleep(TOAST_DUR);
     69 			} catch (InterruptedException e) {
     70 				e.printStackTrace();
     71 			}
     72 			Timeline fouttml = new Timeline();
     73 			KeyFrame foutkey = 
     74 			    new KeyFrame(Duration.millis(FADEOUT_DUR),
     75 			    new KeyValue(stg.getScene()
     76 			        .getRoot()
     77 			        .opacityProperty(), 0)); 
     78 			fouttml.getKeyFrames().add(foutkey);   
     79 			fouttml.setOnFinished(e -> stg.close());
     80 			fouttml.play();
     81 		}).start();
     82 	}
     83 }