• Home
  • Events
  • About
  • JavaFX Abacus Tutorial, Part V

    This is the fifth part of a basic tutorial on JavaFX. The other parts can be found here and the code is on github. The running example of the tutorial is to build an abacus. If you are curious how the final solution will look like, please
    have look at this 7 min video.

    Your latest homework was to think about possible solutions for pushing adjacent balls to the right or left just like a physical abacus does it mechanically.

    Now, what do you think?

    There are two typical solutions that students tend to come up with:

    • a full model with rails and balls with move actions that find out each ball that has to be moved
    • every ball listens for position changes of his neighbors

    The first approach requires quite some work while the latter keeps amount of state that has to be managed pretty small. It is also closer to what happens in the mechanical world. We will start with the listening to our neighbors.

    JavaFX provides a ChangeListener for that purpose. For the sake of simplicity we create a new one for every pair. It is cumbersome enough to define the anonymous inner classes and the final variables that Java requires.

    This code works surprisingly well. Before JavaFX I would have expected this solution to result in a recognizable jitter in the movement of the balls but they move perfectly smooth and synchronous as if we had used some elaborate cropping techniques.

    Here is the full code:

    We have now reached a state where we can really use the abacus for calculation. What is still missing is the “overflow” logic that we will add later – when we will also consider a presentation model for better structuring of the code.

    Homework

    Play with the abacus!
    Do some calculations.
    Can you build the powers of 2 with your abacus?

    This abacus works but it doesn’t look nice, yet. We will explore styling options next week.

    Finally

    If you are committed to learn JavaFX, please consider joining one of our JavaFX workshops at Canoo.

    See you next week!
    Dierk

    P.S. Tim Yates thankfully posted a GroovyFX version.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • DZone
    • Y!GG
    • Webnews
    • Digg
    • del.icio.us
    • DotNetKicks
    • Facebook
    • Google Bookmarks
    • Newsrider
    • Twitter
    • YahooBuzz

    Leave a Comment


    − 3 = one

    css.php