Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.CountDownLatch;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.FlowPane;
- import javafx.stage.Stage;
- import javafx.application.Platform;
- import javafx.scene.ImageCursor;
- import javafx.scene.Scene;
- import javafx.scene.image.Image;
- import javafx.scene.image.PixelReader;
- import javafx.scene.image.PixelWriter;
- import javafx.scene.image.WritableImage;
- import javafx.scene.paint.Color;
- public class FadingRectangle extends Application {
- FadingCursor fade = new FadingCursor();
- @Override
- public void start(Stage primaryStage) throws Exception {
- primaryStage.setWidth(300);
- primaryStage.setHeight(300);
- Scene scene = new Scene(new FlowPane());
- primaryStage.setScene(scene);
- fade.startFade(scene,100);
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- class FadingCursor {
- private int counter;
- /**
- * Start fading the Cursor
- *
- * @param scene
- */
- public void startFade(Scene scene, int millisecondsDelay) {
- // Create a Thread
- new Thread(() -> {
- counter = 100;
- for (; counter >= 0; counter--) {
- CountDownLatch count = new CountDownLatch(1);
- Platform.runLater(() -> {
- // Create the fading image
- WritableImage writable = new WritableImage(64, 64);
- PixelWriter pixelWriter = writable.getPixelWriter();
- // Fade out the image
- for (int readY = 0; readY < 64; readY++) {
- for (int readX = 0; readX < 64; readX++) {
- Color color = new Color(1.0, 1.0, 1.0, (counter / 100.00));
- pixelWriter.setColor(readX, readY, color);
- }
- }
- System.out.println("With counter:"+counter+" opacity is:" + writable.getPixelReader().getColor(32, 32).getOpacity());
- scene.setCursor(new ImageCursor(writable));
- count.countDown();
- });
- try {
- // Wait JavaFX Thread to change the cursor
- count.await();
- // Sleep some time
- Thread.sleep(millisecondsDelay);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement