JavaFX on the server???
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 than 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 outweighed by the advantages listed above).
1. You will still need to use ImageIO for Image handling. There are Utility 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 documents 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 with JavaFX on the server?
Our software libraries allow you to
Convert PDF files to HTML |
Use PDF Forms in a web browser |
Convert PDF Documents to an image |
Work with PDF Documents in Java |
Read and write HEIC and other Image formats in Java |