• Home
  • Events
  • About
  • JavaFX Abacus Tutorial, Part VI – Styling

    February 14th, 2013

    This is the sixth 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.

    We have so far created the JavaFX abacus application without giving
    any thoughts to the visual appeal of the app.

    The good news is that JavaFX very nicely supports keeping functionality and
    display aspects apart by means of CSS styling. We can simply apply style classes to
    the various visual components and think about the actual visual properties later –
    or even provide multiple styles that the user can choose from.

    Styling comes in three steps:
    – assign style classes to the components
    – add a stylesheet to the scene
    – fill the stylesheet

    Adding a style class is easy. Each node has a getStyleClass() method that
    returns an observable list of Strings, each of which is a name of a style class such
    that we call

    BTW: the method name is a bit odd It should be named “getStyleClasses”.

    The various builders make this even easier with the builder method styleClass(“mystyle”).

    Second comes adding a CSS style sheet to the scene. Following the meanwhile
    expected JavaFX pattern this is done via

    The “/abacus.css” refers to a resource (file) that resides in the root of the classpath.
    That makes it easy to package your styles together with the application in the same
    jar file. There are more options for referring to styles like reading from external
    sources or even to define styles right in the code, which is conventionally called
    “inline styles”.

    Last not least, we have to fill the CSS stylesheet, which is pretty much the same
    experience like working with stylesheets for HTML. There are a few specialities
    though when it comes to the availability of style classes, selectors, property names,
    functions, and values.
    The JavaFX CSS Reference Guide is an indispensible resource to get this work done.

    Let us start with our black and white style:

    black_and_white

    First, we set an overall background for the “root” style class that JavaFX applies
    to whatever happens to be the root node of your scene.

    This gives us a nice sand-like background. The circles shall get two different
    styles for easier counting: the ones on the left like bright, on the right like
    darker wood. The shall also appear like balls, i.e. three-dimensional.

    Making circles appear like balls either requires some elaborate
    lighting-, shadow-, and reflection-effects or just a clever gradient. Let’s go for
    the latter. Luckily, JavaFX gives us the radial gradient, which is perfect for
    that purpose.

    With the base color of “burlywood” for the darker balls and “papayawhip” for
    a not too prominent highlight color, we place the highlight a little lower (16%)
    than top-centered and let it shine on only the top half of the ball (radius 50%).
    The ball should also be not 100% solid but slightly transparent with a
    opacity of 80%.
    Now comes the coolest trick: when you place balls close to each other like in
    the abacus, then one ball’s highlight will slightly reflect on the ball in the
    rail above.

    Pearls

    As the painters say: “the shape is made from the light in the shadow”.

    We achieve this effect by using the “reflect” property of the
    radial gradient. This leaves us with the following style definitions:

    With this style, your abacus now looks like depicted below:

    wood_and_sand

    Not too shabby, I would say. But there is more.

    Here comes another piece of painting knowledge: “every color also paints its
    surrounding”.

    We make use of that effect and change only one thing: the background – and we
    change it into a radial gradient from red to black.

    Doesn’t that instantly look like a casino’s red velvet furnishing and make the balls on their
    rails appear like shiny pearls?

    casino_style

    Personal tip: radial gradients in a rectangular frame often look spectacular.

    I hope that by now you are convinced that styles are very helpful and that with only
    minimal smart changes you can totally change the user experience of an application.

    Homework

    Come up with your own styles and don’t forget styling for the rails.
    When a rail is selected – as to mark the baseline for calculations – it should
    get a “selected” style class.
    JavaFX CSS support the usual selector combinations such that you can specify the style
    for the selected rail as “.rail.selected { … }”.

    If you don’t want to see the digits, you can use the “transparent” color.

    Next week with Dolphin

    There still is an important piece of functionality missing: the automatic overflow.
    Next week will address this issue with the help of a presentation model and business
    logic that controls on it. We will use OpenDolphin to that end.

    Finally

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

    If you plan to migrate any current application to JavaFX,
    please give us a call!

    See you next week!
    Dierk

    P.S. the GroovyFX version by Tim Yates.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • email
    • Print
    • Twitter
    • LinkedIn
    • XING
    • Facebook
    • Google Bookmarks

    JavaFX Abacus Tutorial, Part V

    February 8th, 2013

    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.
    • email
    • Print
    • Twitter
    • LinkedIn
    • XING
    • Facebook
    • Google Bookmarks

    Maintenance release – Canoo RIA Suite 2012 Update 1

    February 7th, 2013

    We are pleased to announce that the release Canoo RIA Suite 2012 Update 1 is now available for download! This is a maintenance release for Canoo RIA Suite, including the following new features

    • readiness for SSO security environments
    • new print functionality for ULC Office Integration
    • JxBrowser, used by ULC Web Integration, provides bug fixes for Mac OS X and Java 7
    • repository based distribution of Canoo RIA Suite libraries

    Please see the release notes for the complete list of implemented feature requests and fixed problem reports.

    Please note:
    This is a major release for the packages ULC Office Integration and ULC Web Integration. If you want to use the new version of these packages, please make sure that you are using Canoo RIA Suite Update 1 and that you have upgraded the corresponding licenses. Kindly get in touch with our sales department for any question on this.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • email
    • Print
    • Twitter
    • LinkedIn
    • XING
    • Facebook
    • Google Bookmarks

    JavaFX Abacus Tutorial, Part IV

    February 1st, 2013

    This is the fourth 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 add rails, which can easily be achieved with rectangles like so:

    We have now achieved a state where we can explore the powerful concept of JavaFX property binding and we will do so in two steps

    • today, we will add numbers that move together with their respective ball
    • next week, we will use property binding to push adjacent balls around.

    In an abacus, each ball represents a digit. Let’s add this digit as a text to each ball:

    The placement of the text is a bit off-center but since this is only a temporary step, we do not bother putting each ball and its text into an exactly aligned layout. (If you want, you can use StackPane for that.)

    You should now see each ball with a white label that tells the digit that it represents.

    Why is the text on top of the ball and not behind? That is because we add it to the scene graph after the ball. What is added later is printed on top. You can also use a z-position if you need more control.

    When you play with the current state of the program, you will find that the action handler that moves the ball seems to be ignored every now and then.
    The reason for this is that you have clicked on the text, not on the ball. The text node will then “consume” the event.
    The simplest way to resolve this is to give the text and the ball the same event handler:

    Ok, now the circles move reliably but the text doesn’t move together with its ball. But we would like to have a display like in the figure below:
    abacus with numbers

    JavaFX property binding comes to the rescue!
    Moving the ball by means of a TranslateTransition as we have done last week means that the translateX property of the circle will change over time.
    We want the translateX property of the text to change in the exact same way. We are binding both translateX properties.

    This finally leads us to the solution below:

    So far, so good, but one important piece of functionality is still missing: when starting and the user clicks on e.g. digit 5, all the digits 4, 3, 2, 1 should also move to the right. The equivalent needs to happen when subtracting by moving balls to the left.

    Homework

    This weeks homework is a non-coding design exercise. Think about possible solutions for the pushing of adjacent balls.
    If you have followed the tutorial up to this point, you have all the knowledge to solve this task.

    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. and – as it has become tradition – the GroovyFX solution by Tim Yates.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • email
    • Print
    • Twitter
    • LinkedIn
    • XING
    • Facebook
    • Google Bookmarks

    css.php