Rahul S.
Rahul S.

Reputation: 55

Text 'fadeIn' animation

So I tried creating an animation which fades a string in (from black) on screen, but when I run it, it doesn't seem to work properly. I'm not getting any errors, just a black screen (the stage is black).

Here is the code:

public static void fadeIn(String string, Text tBox) {
    final IntegerProperty counter = new SimpleIntegerProperty(0);
    final BoolProp firstLoop = new BoolProp(false);

    Color fadeIn[] = new Color[16];

    String hexVal = "";
    String hash = "#";

    for (int i = 0; i > 10; i += 1) {
        hexVal = "";

        if (i == 10) 
            break;

        for (int c = 0; c >6; c += 1) {
            hexVal += i;
        }

        fadeIn[i] = Color.web(hash + hexVal);
    }
    fadeIn[10] = Color.web("#aaaaaa");
    fadeIn[11] = Color.web("#bbbbbb");
    fadeIn[12] = Color.web("#cccccc");
    fadeIn[13] = Color.web("#dddddd");
    fadeIn[14] = Color.web("#eeeeee");
    fadeIn[15] = Color.web("#ffffff");

    Timeline line = new Timeline();
    KeyFrame frame = new KeyFrame(Duration.seconds(0.05), event -> {
        if (counter.get() == 16) {
            line.stop();
        } else {
            if (firstLoop.get()) {
                firstLoop.set(false);
                tBox.setText(string);
            }
            tBox.setFill(fadeIn[counter.get()]);
            counter.set(counter.get()+1);
        }
    });
    line.getKeyFrames().add(frame);
    line.setCycleCount(Animation.INDEFINITE);
    line.play();
}

And it's being referenced as follows:

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

    @Override public void start(Stage stage) {
        VBox box = new VBox();

        Text text = new Text();

        box.getChildren().addAll(text);

        stage.setScene(new Scene(box, 500, 500, Color.BLACK));
        stage.show();

        Animations.fadeIn("Testing 123", text);
    }
}

The class BoolProp is as follows (I know one already exists, but I didn't have access to the documentation when I was writing the method.):

public class BoolProp {
    private boolean val;

    public BoolProp() {
        val = false;
    }

    public BoolProp(boolean val) {
        this.val = val;
    }

    public boolean get() {
        return val;
    }

    public void set(boolean val) {
        this.val = val;
    }
}

Upvotes: 0

Views: 48

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36792

There are multiple issues in your code. Lets get to them one by one :

  1. The for loops inside the fadeIn() are incorrect.

Code:

for (int i = 0; i > 10; i += 1)

This loop will never execute since the condition will always be false. What you are looking for is :

for (int i = 0; i < 10; i++)

Similarly, fix the other loop as well.

  1. Inside fadeIn(), you initialize the firstLoop to false and then inside the KeyFrame constructor, you try to check if the value is true, which leads to never setting the text on the Text.

  2. There are few other issues with initializing of String, which we can overlook for now.

If you fix 1 and 2, you should be good to have a running application.

Upvotes: 1

Related Questions