While porting Modellus from javafx1.3 using javafx script to javafx2.0 using java language I ended up creating a class with a few helper methods for converting swing objects to and from javafx objects:
- Converting from java.awt.color to javafx.scene.paint.Color and vice versa
- Converting from java.awt.image.BufferedImage to javafx.scene.image.Image and vice versa
Nothing too fancy but I thought it might come in handy to other people so here’s the code:
public class SwingUtils { public static java.awt.Color toAWTColor(javafx.scene.paint.Color fxColor) { return new java.awt.Color((float)fxColor.getRed(), (float)fxColor.getGreen(), (float)fxColor.getBlue(), (float)fxColor.getOpacity()); } public static javafx.scene.paint.Color fromAWTColor(java.awt.Color awtColor) { return ColorBuilder.create() .red(awtColor.getRed() / 255.0) .green(awtColor.getGreen() / 255.0) .blue(awtColor.getBlue() / 255.0).build(); } // There is a problem with this implementation: transparent pixels on the BufferedImage aren't converted to transparent pixels on the fxImage. public static javafx.scene.image.Image convertToFxImage(java.awt.image.BufferedImage awtImage) { if (Image.impl_isExternalFormatSupported(BufferedImage.class)) { return javafx.scene.image.Image.impl_fromExternalImage(awtImage); } else { return null; } } public static java.awt.image.BufferedImage convertToAwtImage(javafx.scene.image.Image fxImage) { if (Image.impl_isExternalFormatSupported(BufferedImage.class)) { java.awt.image.BufferedImage awtImage = new BufferedImage((int)fxImage.getWidth(), (int)fxImage.getHeight(), BufferedImage.TYPE_INT_ARGB); return (BufferedImage)fxImage.impl_toExternalImage(awtImage); } else { return null; } } }
This will probably be added on Lombard (next javafx version) as shown by this issue: http://javafx-jira.kenai.com/browse/RT-14038