Tutorial : How To Change Color Space from Swing to JavaFX

At IDRSolutions we are currently in the process of Developing a JavaFX implementation of our PDFViewer. In previous JavaFX articles I have shown :

In this tutorial I will show a way to resolve the issue faced when trying to use Swings Color.getRGB() and apply it to a JavaFX Color Object. I will also show you how to extract the raw integer value from a JavaFX Color Object and how to apply it to a Swing Color Object.

This is an issue you may encounter when you have a Swing version of a GUI and JavaFX version of a GUI which are trying to use the same preferences file for the visual theme.

For example,  you may set the background to red in the Swing GUI, if you want your GUIs to use a shared properties file then the background will also be red when you load up the JavaFX GUI.

Shifting From Swing Color Space to JavaFX

The issue we face when trying to make both Swing Color Objects and JavaFX Color objects use the same value to set a color is that in Swing we have the method Color.getRGB which returns a combined sRGB value. However, in JavaFX we do not have this method, in JavaFX we have Color.getRed(), Color.getGreen() and Color.getBlue(). So we need a way to convert that one swing sRGB value into Red, Green and Blue values for JavaFX.

Todo this we perform a bitshift on the sRGB value. We shift the Red component by 8 bits and the Green by 16bits. We then create a JavaFX Color object using these new RGB values. This is done like so :

int raw = swingColor.getRGB();    //This is the Swing Color objects sRGB value
 
//We get our JavaFX RGB values by bitshifting the Swing sRGB raw value.
int r = ((raw >> 16) & 255);
int g = ((raw >> 8) & 255);
int b = ((raw) & 255);
 
//We create a new JavaFX color using our bit-shifted value.
Color javafxColor= new Color.rgb(r,g,b);

Shifting From JavaFX Color Space to Swing

Shifting the color space from JavaFX to Swing is a similar method, we almost do the reverse as we did when moving from Swing to JavaFX. However in JavaFX the Color.getRed, Color.getGreen and Color.getBlue methods return a value from 0-1.

So we need a way to change those 0-1 values to values in the range of 0 – 255 which can then be bit shifted to produce a single sRGB value which can be used in Swing as Color.setRGB(). This can be done like so :

//We multiply by 255 so our 0-1 values become 0-255 values
int r = (int) (javafxColor.getRed() * 255);
int g = (int) (javafxColor.getGreen() * 255);
int b = (int) (javafxColor.getBlue() * 255);
 
//This is the combined sRGB value which can be passed to a Swing Color object.
int rgb = (r << 16) + (g << 8) + b;


You can see a visual example of this in effect from looking at our Swing GUI (Left) and our JavaFXGUI (Right). Both of which are using the same shared value to set the Colour of the Background.

Swing GUI                                                         JavaFX GUI
Swing  javafx

Thank you for reading, I hope you enjoyed this article and will find it of use. If you have any questions, please post them in the comments section bellow.

You may find some of my other JavaFX articles of interest :

We also have a JavaFX PDF Viewer plugin for NetBeans which you can grab here.

This post is part of our “JavaFX article 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 (TwitterFacebook and Google+) or the Blog RSS.

Ebook Page Link

The following two tabs change content below.
Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

Related Posts:

Nathan

About Nathan Howard

Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

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=""> <strike> <strong>