简体   繁体   中英

JavaFX how to jump to adjacent timelines?

Timelines is a collections initialized as

List<Timeline> timelines = new ArrayList<>(); 

the value of each timeline (element of timelines ) is a short tweet. A snippet about constructions of each timeline , note every timeline come with a KeyFrame

// offset is adjusted according to msg length
Text tweet = new Text(offset, displayHeight - 15, tweetBody);

// set timeline
Timeline ti= new Timeline();
ti.setCycleCount(1);
ti.setAutoReverse(false);
// stop at a place where users cannot see
KeyValue kv = new KeyValue(tweet.xProperty(),-10 - mesLength); 
// playtime is defined according to msg length
KeyFrame kf = new KeyFrame(Duration.millis(playtime), kv); 
tl.getKeyFrames().add(kf);
timelines.add(ti);

Suppose in total we have 10 tweets. Build 10 of above timelines iteratively, chain them with proper X-positions (ie tweet1: offset 800, tweet2: offset 1800, tweet3: offset 2500 etc) and durations, run all timelines the same time. That is, all tweets are initialized out of view.

For example, in this picture you have all tweets initialized but they are out of view. 在此处输入图片说明

Click Run 在此处输入图片说明

Messages kick in 在此处输入图片说明

First message ends second message begins 在此处输入图片说明

What I am playing:

timelines.forEach(Animation::play)

How to switch to adjacent tweets in timelines using Next and Previous menuItem.

MenuItem nextControl = new MenuItem("Next");
fasterControl.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        // I want to use jumpTo but didn't figure it out
        // timelines.forEach(timeline -> timeline.jumpTo(Duration.));
    }
});

One extra question, once the tweets got running, how to reset the window back to empty. 在此处输入图片说明

If there needs for clarification and/or code, please let me know.

All you're doing is changing one property. Save a List,

 List<Text> tweets = new ArrayList<>();

Then have a field/variable representing your current text.

 Text current;

No on handle, you find your current index, and grab it's timeline.

 public void handle(ActionEvent event) {

      if(current!=null){
          TimeLine tl = timeLines.get(0);
          current = tweets.get(0);

          tl.stop();
          tl.getKeyFrames().clear();
          //set the display values eg the x value to zero.
          tl.play();

      } else{
          int index = tweets.indexOf(current);
          TimeLine oldLine = timeLines.get(index);
          //stop the timeline, clear the old keyframes.
          //create a new keyvalue, keyframe that hides this text, and play the timeline.

          index = (index+1)%tweets.size();
          TimeLine line = timeLines.get(index);
          current = tweets.get(index);
          //create a new keyvale/keyframe that shows this text, and play the time line.
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM