Archive for May, 2014

SplitPane Animation with DoubleTransition

May 29, 2014

Today I added a nice effect to SnapCode which slides a SplitPane item onscreen when a button is clicked. This functionality was actually ported over from Swing where it required a significant amount of code, but in JavaFX, I was able to achieve the same effect with just a few lines of code.

So when I click on the “Add Support Pane” button, this code adds the SupportPane to the SplitPane and triggers the animation:

  mySplitPn.getItems().add(_supportPane);
  DoubleProperty dprop = mySplitPn.getDividers().get(0).positionProperty();
  DoubleTransition dt = new DoubleTransition(Duration.millis(500), dprop);
  dt.setFromValue(1); dt.setToValue(500); dt.play();

When I click the button again (to remove), similar code is called, except that the item is removed OnFished:

  DoubleProperty dprop = mySplitPn.getDividers().get(0).positionProperty();
  DoubleTransition dt = new DoubleTransition(Duration.millis(500), dprop);
  dt.setToValue(1d); dt.play();
  dt.setOnFinished(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e) {
      getBrowserBox().getItems().remove(1); }});

So it is very simple to add such a nice animated effect. Now the only slight complication is that there is no “DoubleTransition” class in JavaFX. It turns out that this class is trivial to implement with just a slight modification to something like FadeTransition. And this DoubleTransition class will undoubtedly come in handy in other places as well.

Here is a link to the source: DoubleTransition.java

And a link to a simple video demo as well: SplitPane Animation Video

Advertisements

SuperMario JavaFX in SnapCode

May 15, 2014

This week I flushed out more SnapCode JavaFX sprite functionality using a version of Super Mario written for Greenfoot. This required new SnapScene API to control the order of painting: setPaintOrder(classes) and some touch up to the hit detection code. Check out the two minute video below.

YouTube Video: SuperMario JavaFX in SnapCode

SuperMario

JavaFX PacMan in SnapCode with Greenfoot

May 6, 2014

This week I flushed out a set of classes to emulate Greenfoot in JavaFX and  SnapCode. My test case was a nice version of PacMan written by Dan Post. For this I had to add a number of drawing methods to SnapImage (which uses JavaFX Canvas) and I had to add support for playing MP3 sound files (which uses JavaFX Media and MediaPlayer).

Check out the screenshot and 3 minute video of the game in action with some custom/silly JavaFX modding thrown in for fun.

YouTube Video: JavaFX PacMan in SnapCode with Greenfoot

PacMan