【发布时间】:2015-11-12 10:14:03
【问题描述】:
所以我尝试创建一个在屏幕上淡入(从黑色)字符串的动画,但是当我运行它时,它似乎无法正常工作。我没有收到任何错误,只是黑屏(舞台是黑色的)。
代码如下:
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();
}
它的引用如下:
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);
}
}
BoolProp 类如下(我知道已经存在一个,但我在编写方法时无法访问文档。):
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;
}
}
【问题讨论】: