Running JavaFX on a server

JavaFX on the server???

Javafx_logo_color

Java FX on a Server? Is it the Future?

JavaFX is heavily promoted as a desktop technology to replace Swing. So it might seem slightly strange to suggest running it on a server. But JavaFX also offers a replacement for Graphics2D. We use Graphics2D extensively for creating images from PDF data. One of the popular uses for our Java PDF library is to convert PDF files to images on the server. Currently we use Graphics2D for this. So can you use JavaFX on your server, and how well does it run?

Reasons to use JavaFX in place of Graphics2D

We have found 3 reasons to use JavaFX:-

1. More Functionality. JavaFX offers a much more polished set of functions that Graphics2D. As an example, General shapes and paths have much more functionality in JavaFX and you do not need to convert them to Areas,

2. Less code. We have found that our JavaFX implementations have much less code than Graphics2D. This is partly because the API is better designed and partly because JavaFX has more functionality. My colleague Simon has a really interesting blog article on how much simpler blend modes are in JavaFX.

3. The Future. Swing will continue to be around but is unlikely to see any improvements. JavaFX is where all the action is and work is happening to make it run on as many platforms as possible.

An actual code example

Here is a simple code skeleton example. We recommend you run JavaFX on Java8.

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.jpedal.PdfDecoderFX;
/**
* Runs on the server as well
*/public class ServerFXExample extends Application{

@Override
public void start(Stage stage) throws Exception {

//Your JavaFX code

Platform.exit();
}

/**
* Main method to call on server
*/public static void main(String[] args) {

launch();

}
}

Some ‘caveats’

We have found only 2 real issues with JavaFX (which are more than out-weighed by the advantages listed above).

1. You will still need to use ImageIO for Image handling. There are Utilities classes to convert JavaFX images into BufferedImage (and back). So you will still need ImageIO (and JAI if you are working with JPEG2000 images). Image support in Java remains an achilles heel and it would be really nice to see some improvements.

2. Slightly slower Performance? We implemented a test case in JavaFX and another in Swing/Graphics2D and found a small hit (which we think is due to startup rather than generally being slower). I will do some more detailed investigations and document in a future article.

Overall, JavaFX is well worth investigating for the server as well as the desktop and we found it much quicker and  easier to develop Java applications with JavaFX. What are your experiences of JavaFX on the server?

This post is part of our “Java Articles Index“ series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

 

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>