Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

java - javafx animation looping

in c++ or c programming language, we know to change the cordinate we use gotoxy(x,y) and we can use looping and sleep to change the cordinate and making animation. like this;

for(x = 20; x < 25; x++){
  gotoxy(x,20); cout << "*"
}

but my queston is how about in JAVAFX 2.0 programming? i'm using netbeans 7.2. thanks for your any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the JavaFX Animation Package.

There are numerous examples in the JavaFX Animation Tutorial, as Andy pointed out in his comment.

And there is a cute example of a running horse animation loop.

The key is that you don't sleep the JavaFX application thread and you have to release control of the JavaFX thread back to the JavaFX system each time you update something and want it rendered. The JavaFX animation classes take care of these things for you so that you don't have to worry about it. If you just loop like you do in the sample code from your question, JavaFX will just render the scene once after your loop has completed and you will never see anything happen.

Here is a fairly boring example which uses a Timeline to emulate the c++ code in your question to move a dot a pixel every 400 milliseconds.

import java.util.Date;
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

/** Simple JavaFX Animation Sample. */
public class AnimationSample extends Application {
  private       int         x        = 20;
  private       String      status   = "";
  private final Circle      dot      = new Circle(20, 20, 3);
  private final TimeCounter counter  = new TimeCounter();

  public static void main(String[] args) throws Exception { launch(args); }
  @Override public void start(final Stage stage) throws Exception {
    final Timeline timeline = new Timeline(
      new KeyFrame(Duration.ZERO, new EventHandler() {
        @Override public void handle(Event event) {
          refreshScene();
        }
      }),  
      new KeyFrame(Duration.millis(400))
    );
    timeline.setCycleCount(Timeline.INDEFINITE);

    stage.setScene(new Scene(new Group(dot), 50, 50));
    stage.show();

    counter.reset();
    timeline.play();
  }

  private void refreshScene() {
    gotoxy(x, 20);

    status = "*****".equals(status) ? "*" : status + "*";
    System.out.println(String.format("%7d", counter.elapsed()) + " ms " + x + " " + status);

    if (x == 24) {
      x = 20;
    } else {
      x++;
    }
  }      

  private void gotoxy(int x, int y) {
    dot.setCenterX(x); 
    dot.setCenterY(y);
  }

  class TimeCounter {
    private long start = new Date().getTime();
    void reset()   { start = new Date().getTime(); }
    long elapsed() { return new Date().getTime() - start; }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...