タイムラインアニメーションを使用して画像の配列のアニメーションを行っています。配列は299画像です。 0から298までの画像を一度反復してからアニメーションを停止します。javafxアプリケーションで画像を使用したアニメーションが動作しません
アニメーションは連続的に動作する必要がありますが動作しません。タイムラインアニメーションを使用して各画像ビューに対してopacityProperty()を使用しています。 1つの画像アニメーションが完了すると、次の画像に進みます。しかし、私はそれが298のイメージに達すると連続してループすることはできません。変数xは0になり、アニメーションが再び開始されます。
public class Animation_Program_version3 extends Application {
Timeline timeline = null;
Group rootGroup = null;
int x = 0;
Image [] images = new Image[299];;
ArrayList imageview = null;
public Animation_Program_version3() {
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 800, 400, Color.BEIGE);
imageview = new ArrayList();
int y = 0;
for(int x = -50; x < 100; x=x+1){
images[y] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y]));
y = y+1;
}
int y1 = 150;
for(int x = 99; x > -50; x=x-1){
images[y1] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y1]));
y1 = y1+1;
}
rootGroup.getChildren().addAll(imageview);
int x = 0;
timeline = new Timeline();
doAnimation();
primaryStage.setScene(scene);
`primaryStage.show(); `
}
<code>
public void doAnimation(){
KeyFrame[] kf = new KeyFrame[images.length];
ImageView im = (ImageView)imageview.get(x);
<code>
im.setImage(images[x]);
kf[x] = new KeyFrame(Duration.millis(1), new KeyValue(im.opacityProperty(), 0));
timeline.getKeyFrames().add(kf[x]);
// When timeline animation is finished it executes the seetOnFinished Event
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(x == 298){
System.out.println("VALUE OF x:"+x);
x=0; -------> This is where code does not work When it reaches end of array and x initialize to 0 then animation stops.
Collections.reverse(imageview);
doAnimation();
}
/* This if loop works fine animation iterates through 0 to 298 images. */
if(x < 298){
x++;
doAnimation();
}
}
});
timeline.play();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
プログラムを修正しましたが、今はこのエラーが発生していません。子:子が重複しています。
しかし、まだ問題は、私はプログラムが画面上で実行されて表示されません。私はシーンにルートグループを追加しましたが、画面上には何も表示されません。私の新しいプログラム:
public class Animation_Program_version3 extends Application {
Timeline timeline = null;
Group rootGroup = null;
int x = 0;
Image [] images = new Image[299];;
ArrayList imageview = null;
ImageView im = new ImageView();
public Animation_Program_version3() {
// this.imageview = new TreeSet();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
rootGroup = new Group();
final Scene scene =
new Scene(rootGroup, 800, 400, Color.BEIGE);
//
// final Scene scene =
// new Scene(rootGroup, 800, 400, Color.BEIGE);
// int x = 0;
//Image [] images =
imageview = new ArrayList();
int y = 0;
for(int x = -50; x < 100; x=x+1){
images[y] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y]));
y = y+1;
}
int y1 = 150;
for(int x = 99; x > -50; x=x-1){
images[y1] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y1]));
// imageview[y1] = new ImageView(images[y1]);
y1 = y1+1;
}
//for (int i = 0; i < 299; i++) {
// rootGroup.getChildren().addAll(imageview);
//}
int x = 0;
timeline = new Timeline();
doAnimation();
primaryStage.setScene(scene);
primaryStage.show();
}
public void doAnimation(){
KeyFrame[] kf = new KeyFrame[images.length];
// im = (ImageView)imageview.get(x);
im.setImage(images[x]);
rootGroup.getChildren().setAll(im);
kf[x] = new KeyFrame(Duration.millis(1), new KeyValue(im.opacityProperty(), 0));
timeline.getKeyFrames().add(kf[x]);
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// timeline = null;
if(x == 298){
System.out.println("VALUE OF x:"+x);
x=0;
// Collections.reverse(imageview);
// rootGroup.getChildren().setAll(imageview);
//
doAnimation();
}
if(x < 298){
System.out.println("Inside 298 OF x:"+x);
x++;
// Animation_Program_version3.rootGroup = null;
// Animation_Program_version3.rootGroup = new Group();
doAnimation();
}
}
});
timeline.play();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
を使用すると、特定のエラーを取得していますか?拡張また、あなたの質問を再フォーマットしてください。 – Pieter
あなたのコードは 'java.lang.IllegalArgumentException:Children:duplicate children'を投げています、それはあなたの問題ですか? – aKilleR
問題を修正しましたが、このjava.lang.IllegalArgumentExceptionが発生しません:子:重複子エラー – Mike