Advertisement
Guest User

Untitled

a guest
May 24th, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import java.util.concurrent.CountDownLatch;
  2.  
  3. import javafx.application.Application;
  4. import javafx.scene.Scene;
  5. import javafx.scene.layout.FlowPane;
  6. import javafx.stage.Stage;
  7. import javafx.application.Platform;
  8. import javafx.scene.ImageCursor;
  9. import javafx.scene.Scene;
  10. import javafx.scene.image.Image;
  11. import javafx.scene.image.PixelReader;
  12. import javafx.scene.image.PixelWriter;
  13. import javafx.scene.image.WritableImage;
  14. import javafx.scene.paint.Color;
  15.  
  16. public class FadingRectangle extends Application {
  17.  
  18.     FadingCursor fade = new FadingCursor();
  19.  
  20.     @Override
  21.     public void start(Stage primaryStage) throws Exception {
  22.  
  23.         primaryStage.setWidth(300);
  24.         primaryStage.setHeight(300);
  25.  
  26.         Scene scene = new Scene(new FlowPane());
  27.  
  28.         primaryStage.setScene(scene);
  29.  
  30.         fade.startFade(scene,100);
  31.  
  32.         primaryStage.show();
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         launch(args);
  37.     }
  38.  
  39. }
  40.  
  41. class FadingCursor {
  42.  
  43.     private int counter;
  44.  
  45.     /**
  46.      * Start fading the Cursor
  47.      *
  48.      * @param scene
  49.      */
  50.     public void startFade(Scene scene, int millisecondsDelay) {
  51.  
  52.         // Create a Thread
  53.         new Thread(() -> {
  54.  
  55.             counter = 100;
  56.             for (; counter >= 0; counter--) {
  57.                 CountDownLatch count = new CountDownLatch(1);
  58.                 Platform.runLater(() -> {
  59.  
  60.                     // Create the fading image
  61.                     WritableImage writable = new WritableImage(64, 64);
  62.                     PixelWriter pixelWriter = writable.getPixelWriter();
  63.  
  64.                     // Fade out the image
  65.                     for (int readY = 0; readY < 64; readY++) {
  66.                         for (int readX = 0; readX < 64; readX++) {
  67.                             Color color = new Color(1.0, 1.0, 1.0, (counter / 100.00));
  68.                             pixelWriter.setColor(readX, readY, color);
  69.                         }
  70.                     }
  71.  
  72.                     System.out.println("With counter:"+counter+" opacity is:" + writable.getPixelReader().getColor(32, 32).getOpacity());
  73.  
  74.                     scene.setCursor(new ImageCursor(writable));
  75.                     count.countDown();
  76.                 });
  77.                 try {
  78.                     // Wait JavaFX Thread to change the cursor
  79.                     count.await();
  80.                     // Sleep some time
  81.                     Thread.sleep(millisecondsDelay);
  82.                 } catch (InterruptedException e) {
  83.                     e.printStackTrace();
  84.                 }
  85.  
  86.             }
  87.  
  88.         }).start();
  89.  
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement