我在javase8和netbeans 8.0.2中使用javafx,我制作了随机生成的形状图像,并按照时间顺序显示它们。但最后一张照片没有显示。timeline.setcyclecount( 12 )我使用java生成12个图像,但没有在时间线中显示12.Image。
public class JavaFXApplication3 extends Application {
int k;
Timeline timeline;
class ResizableCanvas extends Canvas {
private void draw() {
int[] uyaran = {3, 7, 12};
boolean[] type = new boolean[12];
for (int i = 0; i < 12; i++) {
type[i] = false;
}
for (int v : uyaran) {
type[v - 1] = true;
}
double w = getWidth();
double h = getHeight();
GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, w, h);
gc.setFill(Color.RED);
System.out.println(k);
if (type[k]) {
gc.fillOval(0, 0, w, h);
}
k++;
}
}
@Override
public void start(Stage stage) throws Exception {
k = 0;
ResizableCanvas canvas = new ResizableCanvas();
timeline = new Timeline(new KeyFrame(Duration.millis(1000), ae -> canvas.draw()));
timeline.setCycleCount(12);
timeline.setOnFinished(ActionEvent -> {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {}
stage.close();
});
timeline.play();
Pane pane = new Pane();
pane.getChildren().add(canvas);
canvas.widthProperty().bind(pane.widthProperty());
canvas.heightProperty().bind(pane.heightProperty());
stage.setScene(new Scene(pane));
stage.show();
}
}发布于 2015-02-20 15:28:55
您正在上调用Thread.sleep(...),它阻止线程并阻止其更新。最后一个椭圆实际上只有在暂停结束时才会呈现出来,但是当然你会关闭窗口,这样你就永远看不到它了。
使用PauseTransition暂停,并在暂停结束时使用其onFinished处理程序执行某些操作:
timeline.setOnFinished(ae -> {
PauseTransition pause = new PauseTransition(Duration.seconds(10));
pause.setOnFinished(event -> stage.close());
pause.play();
});https://stackoverflow.com/questions/28624156
复制相似问题