EasyDI Example

| tagged as

This tutorial will show you the usage of Easy-DI. All files for this example can be found in the github-project in the package /src/test/java/eu/lestard/easydi/examples/coffee

Let's assume this class diagram for a coffee machine application:


In this class diagram we can see the dependencies of each class: The CoffeeMachine has a reference to the WaterTank and the CoffeePowderProvider. The CoffeePowderProvider has a reference to the BeanContainer and to the Mill.


Dependency Injection in less then 50 lines of code

| tagged as

I'm a big fan of the Dependency Injection design pattern and I'm using it a lot. I'm saying "design pattern" because when I'm thinking and talking about "DI" I don't think about frameworks, big containers and a lot of magic. Instead, for me DI is a simple concept of how you write your classes and put them together.

In the past I've used many DI frameworks like Guice, CDI/Weld, EJB and Spring. In other use cases (for example my SnakeFX game) I've done the DI by hand:

Simply put all dependencies of a class into the constructor of this class. In the main method you can now start to instantiate the classes you need starting by the ones that have no dependencies and can therefore be instantiated without constructor parameters needed. It's easy and this way you can't create cyclic dependencies by accident. Look at an old version of the DependencyInjector class of SnakeFX to see what I mean.

Learning how to do the DI by hand was a real eye-opener for me. You can learn that DI is no magic at all and you will lose all your fear from the big buzzword "Dependency Injection". It's nothing special. Simply a design pattern.


New release of Advanced-Bindings

| tagged as

Last week I've released a new minor version of my Advanced-Bindings library. In this post I will describe the new features in more detail.

Logic Bindings

In the standard JavaFX Bindings API there are methods to create bindings for the logical OR and AND operations. The drawback of these methods is that they can only be used to combine exactly two observable boolean values.

In the new LogicBindings class there are now methods to create OR and AND bindings with a variable number of arguments.

ObservableBooleanValue a = ...
ObservableBooleanValue b = ...
ObservableBooleanValue c = ...
ObservableBooleanValue d = ...

BooleanBinding and = LogicBindings.and(a,b,c,d);

BooleanBinding or = LogicBindings.or(a,b,c,d);


Adjustable UI Brightness with JavaFX

| tagged as

Maybe some of you know or have already worked with Adobe products like Premier Pro or After Effects.

From the perspective of a UI developer there are many cool things about these products. For example you can seamlessly fade the brightness of the UI and it looks great in every brightness level (which seems to be a hard task).

This is cool because there are good reasons for users to prefer either a dark or light interface. For example some creative artists like to work in a dimmed office so that no light from outside is overlaying the colors picture/video they are working on. In this situation a dark user interface is better then a light one because it's more comfortable for the eyes.

On the other hand I know people how prefer a light and friendly user interface because a dark one would give them a depressive fealing.

So it can be a big plus for the usability of your application when you allow the user to adjust the brightness of your app.

Adjustable Brightness in JavaFX

Something like this can be done with JavaFX too. With JavaFX 8 there was a new default stylesheet created known as modena.

In this stylesheet there is a color definition named -fx-base that is, as the name suggests, the base for all other color definitions in the stylesheet.

When you adjust the value of -fx-base all other colors will be adjusted automatically in relation to -fx-base.


The first thing to do is to create an IntegerProperty that will hold the brightness level in a range from 0 to 100%.

IntegerProperty brightness = new SimpleIntegerProperty();

After that we create a StringExpression (which is a base class of StringBinding) that contains the style definition of -fx-base with the value of our brightness property.

DoubleBinding colorValue = brightness.multiply(2.55);

StringExpression styleString = 
    Bindings.format("-fx-base:rgb(%1$.0f , %1$.0f, %1$.0f)", colorValue);

For this task we can use the Bindings.format method that takes a format string and an observable value. In the example I'm using the rgb css method to define the color. This method takes the color information for every channel in a range from 0 to 255. For that reason I'm creating a DoubleBinding that contains the multiplication of our brightness property (from 0 to 100) with 2.55 so that the value range is correct.

The last step is to bind the style property of the root container of your application to the StringExpression with our style definition.

Parent root = ...


This way the styling of the application is adjusted everytime the brightness property changes.

I've created a small example app that shows the usage of this binding. In the app I have bound the brightness property to a slider so you can change the brightness level without interruption. To demonstrate the effect I created a dummy form with some JavaFX controls.







You can find the example code on github: https://github.com/lestard/javafx-brightness-example. Feel free to use it in your own applications!


SchachZWO Online

| tagged as

In the lecture "XML based applications" at the HSZG we were working on a project called "SchachZWO Online". "Schach" is the german word for "chess" and "ZWO" is another word for "two" so (as you can guess) this is a chess like online game.

SchachZWO (offline)

screenshot from the official website

The basic for the project is a board game that a game author from dresden has developed. In his "SchachZWO" you play on a 7x7 or 9x9 board with some special figures: Instead of the rook and the queen you have the "Mann" (man) and "Frau" (woman). Instead of the pawn you have the "Rocks" and instead of the king you have the "Zenit" (zenith). On the 9x9 board you have two additional figures with special movement capabilities: The "Wissen" (knowledge) and the "Glauben" (faith).

One of the main differences between chess and "SchachZWO" is that there are more than one possibilities to win the game. Beside the classical chess mate you win when you move your zenith to the middle field of the board. Only the zenith can step on this field in the middle, all other figures aren't allowed to step on the field.

In the first place this rules might sound a little strange to you but it's realy fun to play the game. At the moment the board game isn't available for buying but it will be in the near future. Fisit the website of the game author when you like to know more: http://www.schachzwo.de.

SchachZWO Online


Our project for the lecture was to create an online multiplayer browser game version of this game. In the client we used HTML5 canvas together with JQuery to draw the board and the figures. To create a modern Single-Page-Application we used Angular.JS and Require.JS in the client.

On the server side we have a node.js together with express.js. There are REST like webservices where the client can GET the current state of the board and can POST new moves.

To tell the client that the opponent has done a move we use Server-Sent Events to send messages. The database connection was created in a generic manner to be able to replace the database implementation. At the moment we have a MongoDB and CouchDB implementation.

State of Play

The lecture is over and we finished the project. We have decided to put the source code on github: https://github.com/lestard/schachzwo-online and you can play the game online at: http://schachzwo.inf.hszg.de.

Please note that this was the first Node.JS project for all of us. We aren't pros and we hadn't had much time to finish this project so the source code will probably not be high quality. But of cause we always like to learn: When you have suggestions on what we can do better please tell us!

Also note that the game is sort of a prototype. We hadn't had the time to do much quality assurance and testing so it's best to see the hostet version as sort of a "BETA". When you find bugs please report them on github: https://github.com/lestard/schachzwo-online/issues.


Advanced-Bindings for JavaFX

| tagged as

I'm a big fan of JavaFX's Properties and Data-Bindings. In my opinion, one of the most interesting part of the data binding capabilities of JavaFX is that you can create a binding that represents a calculation of values. For example you can create a binding that represents the addition of two integer properties:

import javafx.beans.binding.Bindings; 
import static eu.lestard.assertj.javafx.api.Assertions.assertThat;

public void test(){
    IntegerProperty a = new SimpleIntegerProperty(12);
    IntegerProperty b = new SimpleIntegerProperty(30);

    NumberBinding c = Bindings.add(a, b);




There are binding method for basic operations like add, subtract, multiply, divide. But what if you need some more specific operations like exponentiation or sine?

To fill this gap I have created a library called "advanced-bindings" that will become a collection of custom bindings that are needed to create application logic with data bindings. The first step was to create binding implementations for all methods of java.lang.Math. This way you can do stuff like this:

public void testPow(){

    DoubleProperty a = new SimpleDoubleProperty(3);
    DoubleProperty b = new SimpleDoubleProperty(2);

    final DoubleBinding pow = MathBindings.pow(a, b);

    // 3^2 = 9


    // 5^3 = 125

In the future I will add more useful helpers and custom bindings. If you have ideas feel free to add an issue at github.

I have uploaded the library to maven-central so you can use it with Gradle:

compile 'eu.lestard:advanced-bindings:0.1.0'

or with Maven:



| tagged as

Again, I have rewritten one of my old games with JavaFX. This time I've created a JavaFX version of my old java swing game ColorPuzzle (yes I know, the name isn't very creative).

screenshot of the game

The game mechanics are the same: You can switch the color of all fields under your control. Everytime you are switching the active color, all fields in the direct neighbourhood of your own fields with that same color are going under your control from now on. The goal is to bring all fields under your control with the fewest moves.

Before you ask: Yes, the game idea is not my invention, there are many games of this type out there and my version is only a clone. I create games only because programming is fun and not to become the best game inventor :-)

The code is available on github: https://github.com/lestard/ColorPuzzleFX.

Grid Component

While the development of the game I have encapsulated the logic for manipulating and drawing the grid and extracted it into it's own component. This way I was able to reuse the grid logic for other projects like SnakeFX.

You can view the code for this grid component on github too (https://github.com/lestard/Grid) but it's not intended for wide usage at the moment as documentation and testing far from production ready. It's more like a playground for me.

In another post I will describe the idea behind the grid component in more detail.


Another reason to build the game was to create a simple example for our own UI framework MvvmFX.



| tagged as

As you probably already know I'm a big fan of the AssertJ library. That's the reason I've done a talk about it at the JavaUserGroup Görlitz.

I like the way you can write readable unit tests and are supported by the IDE because AssertJ is implemented as a Fluent-API.

In the past weeks I've written a package of assertions specially for JavaFX. With this assertions you can verify the values of JavaFX Properties and Bindings.

With the normal AssertJ you would write something like this:

DoubleProperty x = new SimpleDoubleProperty(10.13);


With my extensions you don't need to call the .get() all the time. Instead you can write:


Project Info

The project is hosted on github: https://github.com/lestard/assertj-javafx.

I've already uploaded it to maven central so you can use it with gradle:

testCompile 'eu.lestard:assertj-javafx:0.1.0'

or maven:


Tell me what you think. If you have ideas for new assertions that could be added feel free to add an issue in the issue tracker.


MvvmFX article in JavaMagazin

| tagged as

In the JavaMagazin 06/2014 there is an article about MvvmFX written by Alex and me.

In this article we describe the design pattern Model-View-ViewModel and show how you can apply it to your application with our framework MvvmFX.

Additionally we were showing some of the internals of JavaFX and MvvmFX. For example I'm describing how the loading of FXML files is done in JavaFX and how we hook up into this process to enable the usage of Dependency Injection frameworks.

Another part of the article is the combination of MvvmFX and SynchronizeFX and how we are using it in our own applications at Saxonia Systems.


JUG-GR: Better readable unit tests with AssertJ

| tagged as

Last week I gave a talk about better readable unit tests with AssertJ at the Java User Group Görlitz.

I talked about the benifit of readable unit tests and what frameworks there are for java developers to archive good unit tests.

One of the main elements of unit tests are assertions. In JUnit we have the static methods assertEquals, assertNotNull and so on. But these aren't making a good job when it comes to easy readable code. A better readable way is to use the assertThat method that comes with Hamcrest and is also part of the latest JUnit versions.

The problem with Hamcrest is that it is created out of many static methods which has several drawbacks. The biggest one is the bad IDE support.

In the talk I showed the framework AssertJ which is an alternative to hamcrest. It also uses a static assertThat method as the entry point to an assertion but the rest of the API is implemented as a fluent-API. This way the IDE is able to give suggestions of possible assertion methods based on the type that was given as argument to the assertThat method. Look at the following example:

    String message = "hello world, manuel";


With this example the IDE knows that 'message' is of type String and it can only suggest assertions that are making sense for strings like startsWith or endsWith. For a parameter of type integer the IDE would instead suggest assertions like isPositive or isBetween(int start, int end).

I realy love this style of writing unit tests. In the talk I gave some other interesting examples of what AssertJ can do for you.