Blurry render of SwingNode in JavaFX on Windows - java

Overview
Using FlyingSaucer within a JavaFX application, to avoid WebView for various reasons:
doesn't provide direct API access to its scrollbars for synchronous behaviour;
bundles JavaScript, which is a huge bloat for my use case; and
failed to run on Windows.
FlyingSaucer uses Swing, which requires wrapping its XHTMLPanel (a subclass of JPanel) in a SwingNode to use alongside JavaFX. Everything works great, the application renders Markdown in real-time, and is responsive. Here's a demo video of the application running on Linux.
Problem
The text rendering on Windows is blurry. When running in a JFrame, not wrapped by a SwingNode, but still part of the same application shown in the video, the quality of the text is flawless. The screen capture shows the application's main window (bottom), which includes the SwingNode along with the aforementioned JFrame (top). You may have to zoom into the straight edge of the "l" or "k" to see why one is sharp and the other blurry:
This only happens on Windows. When viewing the font on Windows through the system's font preview program, the fonts are antialiased using LCD colours. The application uses grayscale. I suspect that if there is a way to force the rendering to use colour for antialiasing instead of grayscale, the problem may disappear. Then again, when running within its own JFrame, there is no problem and LCD colours are not used.
Code
Here's the code for the JFrame that has a perfect render:
private static class Flawless {
private final XHTMLPanel panel = new XHTMLPanel();
private final JFrame frame = new JFrame( "Single Page Demo" );
private Flawless() {
frame.getContentPane().add( new JScrollPane( panel ) );
frame.pack();
frame.setSize( 1024, 768 );
}
private void update( final org.w3c.dom.Document html ) {
frame.setVisible( true );
try {
panel.setDocument( html );
} catch( Exception ignored ) {
}
}
}
The code for the blurry SwingNode is a little more involved (see full listing), but here are some relevant snippets (note that HTMLPanel extends from XHTMLPanel only to suppress some undesired autoscrolling during updates):
private final HTMLPanel mHtmlRenderer = new HTMLPanel();
private final SwingNode mSwingNode = new SwingNode();
private final JScrollPane mScrollPane = new JScrollPane( mHtmlRenderer );
// ...
final var context = getSharedContext();
final var textRenderer = context.getTextRenderer();
textRenderer.setSmoothingThreshold( 0 );
mSwingNode.setContent( mScrollPane );
// ...
// The "preview pane" contains the SwingNode.
final SplitPane splitPane = new SplitPane(
getDefinitionPane().getNode(),
getFileEditorPane().getNode(),
getPreviewPane().getNode() );
Minimal Working Example
Here's a fairly minimal self-contained example:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.stage.Stage;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.xhtmlrenderer.simple.XHTMLPanel;
import javax.swing.*;
import static javax.swing.SwingUtilities.invokeLater;
import static javax.swing.UIManager.getSystemLookAndFeelClassName;
import static javax.swing.UIManager.setLookAndFeel;
public class FlyingSourceTest extends Application {
private final static String HTML = "<!DOCTYPE html><html><head" +
"><style type='text/css'>body{font-family:serif; background-color: " +
"#fff; color:#454545;}</style></head><body><p style=\"font-size: " +
"300px\">TEST</p></body></html>";
public static void main( String[] args ) {
Application.launch( args );
}
#Override
public void start( Stage primaryStage ) {
invokeLater( () -> {
try {
setLookAndFeel( getSystemLookAndFeelClassName() );
} catch( Exception ignored ) {
}
primaryStage.setTitle( "Hello World!" );
final var renderer = new XHTMLPanel();
renderer.getSharedContext().getTextRenderer().setSmoothingThreshold( 0 );
renderer.setDocument( new W3CDom().fromJsoup( Jsoup.parse( HTML ) ) );
final var swingNode = new SwingNode();
swingNode.setContent( new JScrollPane( renderer ) );
final var root = new SplitPane( swingNode, swingNode );
// ----------
// Here be dragons? Using a StackPane, instead of a SplitPane, works.
// ----------
//StackPane root = new StackPane();
//root.getChildren().add( mSwingNode );
Platform.runLater( () -> {
primaryStage.setScene( new Scene( root, 300, 250 ) );
primaryStage.show();
} );
} );
}
}
Blurry capture from the minimal working example;
zooming in reveals letter edges are heavily antialiased rather than sharp contrasts:
Using a JLabel also exhibits the same fuzzy render:
final var label = new JLabel( "TEST" );
label.setFont( label.getFont().deriveFont( Font.BOLD, 128f ) );
final var swingNode = new SwingNode();
swingNode.setContent( label );
Attempts
Here are most of the ways I've tried to remove the blur.
Java
On the Java side, someone suggested to run the application using:
-Dawt.useSystemAAFontSettings=off
-Dswing.aatext=false
None of the text rendering hints have helped.
Setting the content of the SwingNode within SwingUtilities.invokeLater has no effect.
JavaFX
Someone else mentioned that turning caching off helped, but that was for a JavaFX ScrollPane, not one within a SwingNode. It didn't work.
The JScrollPane contained by the SwingNode has its alignment X and alignment Y set to 0.5 and 0.5, respectively. Ensuring a half-pixel offset is recommended elsewhere. I cannot imagine that setting the Scene to use StrokeType.INSIDE would make any difference, although I did try using a stroke width of 1 to no avail.
FlyingSaucer
FlyingSaucer has a number of configuration options. Various combinations of settings include:
java -Dxr.text.fractional-font-metrics=true \
-Dxr.text.aa-smoothing-level=0 \
-Dxr.image.render-quality=java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC
-Dxr.image.scale=HIGH \
-Dxr.text.aa-rendering-hint=VALUE_TEXT_ANTIALIAS_GASP -jar ...
The xr.image. settings only affect images rendered by FlyingSaucer, rather than how the output from FlyingSaucer is rendered by JavaFX within the SwingNode.
The CSS uses points for the font sizes.
Research
https://stackoverflow.com/a/26227562/59087 -- looks like a few solutions may be helpful.
https://bugs.openjdk.java.net/browse/JDK-8089499 -- doesn't seem to apply because this is using SwingNode and JScrollPane.
https://stackoverflow.com/a/24124020/59087 -- probably not relevant because there is no XML scene builder in use.
https://www.cs.mcgill.ca/media/tech_reports/42_Lessons_Learned_in_Migrating_from_Swing_to_JavaFX_LzXl9Xv.pdf -- page 8 describes shifting by 0.5 pixels, but how?
https://dlsc.com/2014/07/17/javafx-tip-9-do-not-mix-swing-javafx/ -- suggests not mixing JavaFX and Swing, but moving to pure Swing isn't an option: I'd sooner rewrite the app in another language.
Accepted as a bug against OpenJDK/JavaFX:
https://bugs.openjdk.java.net/browse/JDK-8252255
JDK & JRE
Using Bellsoft's OpenJDK with JavaFX bundled. To my knowledge, the OpenJDK has had Freetype support for a while now. Also, the font looks great on Linux, so it's probably not the JDK.
Screen
The following screen specifications exhibit the problem, but other people (viewing on different monitors and resolutions, undoubtedly) have mentioned the issue.
15.6" 4:3 HD (1366x768)
Full HD (1920x1080)
Wide View Angle LED Backlight
ASUS n56v
Question
Why does FlyingSaucer's XHTMLPanel when wrapped within SwingNode become blurry on Windows, and yet displaying the same XHTMLPanel in a JFrame running in the same JavaFX application appears crisp? How can the problem be fixed?
The problem involves SplitPane.

There are a few options that you might try although I have to admit that I do not know FlyingSaucer and its API.
FlyingSaucer has different renderers. Thus it might be possible to avoid the Swing/AWT rendering completely by using this library instead in order to do all the rendering directly in JavaFX. https://github.com/jfree/fxgraphics2d
Another possibility is to let FlyingSaucer render into an image which can the be displayed in JavaFX very efficiently via direct buffers. See the AWTImage code in my repository here: https://github.com/mipastgt/JFXToolsAndDemos

I wasn't able to reproduce the issue myself, so there may be some issue in the combination of JDK/JavaFX version you are using. It's also possible that the issue only arises with a specific combination of display size and screen scaling.
My setup is the following:
JavaFX 14
OpenJDK 14
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.jsoup.nodes.Document;
import org.xhtmlrenderer.simple.XHTMLPanel;
import javax.swing.*;
public class FlyingSourceTest extends Application {
private final static String HTML_PREFIX = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<body>\n";
private static final String HTML_CONTENT =
"<p style=\"font-size:500px\">TEST</p>";
private final static String HTML_SUFFIX = "<p style='height=2em'> </p></body></html>";
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
primaryStage.setTitle("Hello World!");
XHTMLPanel mHtmlRenderer = new XHTMLPanel();
mHtmlRenderer.getSharedContext().getTextRenderer().setSmoothingThreshold(0);
SwingNode mSwingNode = new SwingNode();
JScrollPane mScrollPane = new JScrollPane(mHtmlRenderer);
String htmlContent = HTML_PREFIX + HTML_CONTENT + HTML_SUFFIX;
Document jsoupDoc = Jsoup.parse(htmlContent);
org.w3c.dom.Document w3cDoc = new W3CDom().fromJsoup(jsoupDoc);
mHtmlRenderer.setDocument(w3cDoc);
mSwingNode.setContent(mScrollPane);
// AnchorPane anchorPane = new AnchorPane();
// anchorPane.getChildren().add(mSwingNode);
// AnchorPane.setTopAnchor(mSwingNode, 0.5);
// AnchorPane.setLeftAnchor(mSwingNode, 0.5);
// mSwingNode.setTranslateX(0.5);
// mSwingNode.setTranslateY(0.5);
StackPane root = new StackPane();
root.getChildren().add(mSwingNode);
Platform.runLater(() -> {
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
});
});
}
}

The issue has been accepted as a bug against OpenJDK/JavaFX:
https://bugs.openjdk.java.net/browse/JDK-8252255
Neither of Mipa's suggestions would work in practice. FlyingSaucer is tightly integrated with a JScrollPane, which precludes the possibility of forcing FlyingSaucer to render onto a JavaFX-based panel.
Another possibility is to go the opposite direction: create a Swing application and embed JavaFX controls, such as using a JFXPanel; however, it would seem more prudent to accept the blurry behaviour until the bug is bashed.

Related

JavaFX ScrollPane receiving ScrollEvents on mouse enter

I have a JavaFX application with a ScrollPane that handles resizing of nodes upon scrollEvents. However when the JavaFX stage (or Window) is not focused, I get an odd behaviour that I think might be a JFX bug, though wondering if anyone has encountered or managed to resolve it.
To replicate the issue, if you lose focus on the JavaFX window and perform some scrolling using the mouse-wheel on another window (eg your browser), and then relatively quickly move your mouse back to re-enter the JavaFX window (without clicking, scrolling or focusing upon the JavaFX window) the JavaFX window receives a bunch of scrollEvents despite no mouse-wheel action being performed.
I'm wondering if anyone has encountered this and worked out a way to somehow filter these odd scrollEvents out as it results in some strange zooming action that is unexpected given the lack of mouse-wheel scrolling!
I'm using Java & JavaFX 17 (OpenJFX), see below sample application that demonstrates, thanks!
public class ScrollEventIssueApplication extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane ();
borderPane .setPrefWidth(600);
borderPane .setPrefHeight(600);
Pane content = new Pane();
content.setPrefWidth(1000);
content.setPrefHeight(1800);
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setPrefWidth(700);
scrollPane.setPrefHeight(700);
content.setOnScroll(event -> {
System.out.println("Scroll event received: " + event.getDeltaY());
});
borderPane.setCenter(scrollPane);
Scene scene = new Scene(borderPane, 1800, 900);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It appears that this may be a platform-dependent feature of the installed pointing device and driver. I could occasionally reproduce the effect on Mac OS X 12 with JavaFX 17, but only when I also accidentally raked an errant finger or two across the mouse's multi-touch surface.
For reference, I tested the following simpler variation. Note code to enumerate the OS and Java version numbers, as well as changes to the viewport dimensions in order to show the scroll bars when set to AS_NEEDED by default.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/** #see https://stackoverflow.com/q/72485336/230513 */
public class ScrollTest extends Application {
private static final double W = 640;
private static final double H = 480;
#Override
public void start(Stage stage) {
Pane content = new StackPane();
content.getChildren().addAll(new Rectangle(W, H, Color.BLUE),
new Circle(Math.min(W, H) / 2, Color.LIGHTBLUE));
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setPrefViewportWidth(2 * W / 3);
scrollPane.setPrefViewportHeight(2 * H / 3);
content.setOnScroll(event -> System.out.println(
"dx dy: " + event.getDeltaX() + " " + event.getDeltaY()));
stage.setScene(new Scene(scrollPane));
stage.show();
}
public static void main(String[] args) {
System.out.println(System.getProperty("os.name")
+ " v" + System.getProperty("os.version")
+ "; Java v" + System.getProperty("java.version")
+ "; JavaFX v" + System.getProperty("javafx.runtime.version"));
launch(args);
}
}
I realised the issue was the mouse I was using (a Logitech MX Vertical Advanced Ergonomic) had a "smooth scrolling" option on the mouse wheel that meant to it would "continue" some small scroll events after the mouse wheel had stopped scrolling which were causing this issue. Turning off that option in the Logitech Options application resolved the issue.

Best way to render overlined text with custom font settings

I'm writing a Swing application and I'm trying to figure out a way to display text while being able to control the following text properties:
font family and size
text overline
lower index
Of course the more flexible the approach, the better, but those are strictly mandatory for me.
What I already tried:
using JTextPane and setting text-decoration: overline attribute -
doesn't work, because JTextPane html/css engine is dated and doesn't support this
using JTextPane with setting upper border via css -
doesn't work, because it can only be applied to html nodes with
display:block (such as divs), and I need this inline (setting display: inline-block is not
supported in JTextPane, so I can't use that)
using WebView from JavaFX - this is where I'm currently at, but I can't figure out how to load a custom font programmatically. It only seems to be working when the font is loaded at OS level (e.g. when the font file is present in ~/.fonts on Linux)
This is a working example. Mostly taken from Integrating JavaFX into Swing Applications.
Mind the font file, which you might have to specify yourself (here's the file I used):
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
private static void initAndShowGUI() {
JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Platform.runLater(() -> initFX(fxPanel));
}
private static void initFX(JFXPanel fxPanel) {
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
var wb = new WebView();
// ???
var f = Font.loadFont("lmroman10-regular.otf", 10);
assert f != null;
wb.getEngine().loadContent(
"<html><body>" +
"<span style=\"" +
"text-decoration: overline; " +
"font-size: 24px;" +
"font-family: 'LM Roman 10';\">" +
"foo" +
"</span>" +
"bar" +
"</body></html>");
return new Scene(wb);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> initAndShowGUI());
}
}

Print dialog does not come to the fore

By PrinterJob of JavaFx can call the Print Dialog. My problem is that the dialog when calling does not come to the fore.
Here is my example:
import javafx.application.Application;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Printexample extends Application
{
#Override
public void start( final Stage primaryStage )
{
final PrinterJob job = PrinterJob.createPrinterJob( Printer.getDefaultPrinter() );
final Button b = new Button( "Print Dialog" );
b.setOnAction( event -> job.showPrintDialog( primaryStage ) );
final BorderPane pane = new BorderPane( b );
primaryStage.setMinWidth( 400 );
primaryStage.setMinHeight( 300 );
primaryStage.setTitle( "Print" );
final Scene scene = new Scene( pane );
primaryStage.setScene( scene );
primaryStage.centerOnScreen();
primaryStage.addEventFilter( KeyEvent.KEY_PRESSED, event ->
{
if ( event.getCode().equals( KeyCode.ESCAPE ) )
{
primaryStage.close();
}
} );
primaryStage.show();
}
public static void main( final String[] args )
{
launch( args );
}
}
The second problem: The frame is not modal, therefore it can lead to errors.
Information: I use Java 8_92.
Probably a current limitation of JavaFX as described by JDK-8088395.
So you have these options:
Wait this to eventually be fixed in an update or JavaFX 9.
Write yourself a custom dialog and then communicate with the print APIs to populate it, as suggested in JDK-8098009.
Block your scene using a overlay, show the print dialog and then remove the overlay. You'll also need to prevent the window closing while the scene is blocked.
Use AWT Print Dialog (kludge, you've been warned), e.g.:
java.awt.print.PrinterJob printJob = PrinterJob.getPrinterJob();
Button b = new Button("Print Dialog");
b.setOnAction(event -> {
JFrame f = new JFrame();
printJob.printDialog();
// Stage will be blocked(non responsive) until the printDialog returns
});
These Problem happen to me today with a spring boot application and the same Code under Windows 10.
I write here the solution, that for me works, because it was the same issue.
Spring is setting by default the java.awt.headless Property with true.
In the Implementation of showPrintDialog Method there is a bug checking about headless returning true but not showing the dialog.
The solution for me was setting the headless property in Spring as false as described Run Spring Boot Headless by Efe Kahraman
I think you might be missing a peice of code to send the stage to the front.
Try adding: stage.toFront();
Source: http://www.java2s.com/Code/Java/JavaFX/Movingstagewindowtofront.htm

Using Netbeans Visual Library in Javafx

I am trying to use the Net beans Visual Library in Javafx and I am referring to example here https://platform.netbeans.org/graph/examples.html.
I am particularly using the DemoGraphscene.java in the javaone.demo4 package. While I am using am using the example in my javafx project, I am not sure how to display the graph.
Here is what I have written in my controller class:
public class GraphicalViewController implements Initializable {
/**
* Initializes the controller class.
*/
#FXML
private Pane pane1;
#FXML
private Label label;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
GraphScene scene = new DemoGraphScene ();
String helloNodeID = "Hello";
String worldNodeID = "World";
String edge = "edge";
Widget hello = scene.addNode (helloNodeID);
Widget world = scene.addNode (worldNodeID);
scene.addEdge (edge);
scene.setEdgeSource(edge, helloNodeID);
scene.setEdgeTarget(edge, worldNodeID);
hello.setPreferredLocation (new Point (100, 100));
world.setPreferredLocation (new Point (400, 200));
pane1.getChildren().add(scene.getView());
}
}
So I have (argument mismatch; JComponent cannot be converted to Node) in the line pane1.getChildren().add(scene.getView());
How do I come about this problem?
I did this:
SwingNode swingScene = new SwingNode();
SwingNode swingScene1 = new SwingNode();
swingScene.setContent(new JButton("Click me!"));
swingScene1.setContent(scene.getView());
pane1.getChildren().add(swingScene1);
When I do pane1.getChildren().add(swingScene1), I see nothing is displayed, but pane1.getChildren().add(swingScene) does show the button.
Use a SwingNode
The NetBeans Visual Library is Swing based. If you want to use it in JavaFX, you need to wrap the Swing component in a JavaFX node.
#FXML Pane pane1;
. . .
GraphScene scene = new DemoGraphScene();
. . .
SwingNode swingScene = new SwingNode();
swingScene.setContent(scene.getView());
. . .
pane1.getChildren().add(swingScene);
Notes of Confusion
It is especially confusing because the visual library works with a scene and the JavaFX library also works with a scene, but they are different scenes from different libraries, so you need to wrap the Visual Library scene view in a JavaFX node in order to display it in a JavaFX scene.
Additionally, when mixing Swing and JavaFX code, ensure you execute Swing code on the Swing event dispatch thread and JavaFX code on the JavaFX application thread - otherwise things could go badly wrong.
I'm not a big fan of mixing Swing and JavaFX, usually I recommend that if you want to use Swing, then write a 100% Swing App and if you want to use JavaFX, write a 100% JavaFX App.
Executable Sample
Here is an executable sample. The sample relies on the source code at: https://platform.netbeans.org/graph/examples.html. To use it, download the sample project from that link, paste the sample code below into the
NetBeans project and right-click to run it.
import java.awt.Dimension;
import java.awt.Point;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.Widget;
public class FXGraphDemo extends Application {
#Override
public void start(final Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
Scene scene = new Scene(new Group(swingNode), 400, 300);
stage.setScene(scene);
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final GraphScene graphScene = new DemoGraphScene();
String helloNodeID = "Hello";
String worldNodeID = "World";
String edge = "edge";
Widget hello = graphScene.addNode (helloNodeID);
Widget world = graphScene.addNode (worldNodeID);
graphScene.addEdge (edge);
graphScene.setEdgeSource(edge, helloNodeID);
graphScene.setEdgeTarget(edge, worldNodeID);
hello.setPreferredLocation (new Point (100, 100));
world.setPreferredLocation (new Point (300, 200));
final JComponent sceneView = graphScene.createView();
final JScrollPane panel = new JScrollPane (sceneView);
panel.getHorizontalScrollBar().setUnitIncrement (32);
panel.getHorizontalScrollBar().setBlockIncrement (256);
panel.getVerticalScrollBar().setUnitIncrement (32);
panel.getVerticalScrollBar().setBlockIncrement (256);
panel.setPreferredSize(new Dimension (400, 300));
swingNode.setContent(panel);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Caveat
The sample (often, but not always) has a issue when it initially paints, it will flash black for a second and log a NullPointerExcepton when trying to calculate a Rectangle (for reasons unbeknownst to me - seems a bit like a race condition somewhere). After that though, it seems to display and work OK. To get around the black flash and NullPointerException, you can display the graph in a JFrame rather than a SwingNode, but then it shows in its own separate window rather than being embedded in a JavaFX scene.
NetBeans Visual Library JavaFX port
https://github.com/theanuradha/visual-library-fx/releases
https://github.com/theanuradha/visual-library-fx/tree/master/org-netbeans-api-visualfx/src/test/java/javaone/demo4
DemoGraphScene scene = new DemoGraphScene();
String helloNodeID = "Hello";
String worldNodeID = "World";
String edge = "edge";
Widget hello = scene.addNode(helloNodeID);
Widget world = scene.addNode(worldNodeID);
scene.addEdge(edge);
scene.setEdgeSource(edge, helloNodeID);
scene.setEdgeTarget(edge, worldNodeID);
hello.setPreferredLocation(new Point(0, 0));
world.setPreferredLocation(new Point(400, 200));
final SceneNode sceneView = scene.createView();

CollorAdjust with SwingX's ImageView?

I have a question about adjusting contrast, saturation and hue of an image that's loaded to jXImageView from swingx library.
I have the ColorAdjust methods.
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.3);
colorAdjust.setHue(-0.03);
colorAdjust.setBrightness(0.2);
colorAdjust.setSaturation(0.2);
When the user click on the "Enhancement" button, the image should change a bit, but how to do that? Remember: I'm using the jXImageView.
I've increased the contrast already by using this code:
float brightenFactor = 1.5f;
BufferedImage imagem = (BufferedImage) jXImageView2.getImage();
RescaleOp op = new RescaleOp(brightenFactor, 0, null);
imagem = op.filter(imagem, imagem);
jXImageView2.updateUI();
Edit
I tryied:
BufferedImage imagem = (BufferedImage) jXImageView2.getImage();
Image image = SwingFXUtils.toFXImage(imagem, null);//<--ERROR on that line (incompatible types: writable image cannot be converted to Image)
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.3);
colorAdjust.setHue(-0.03);
colorAdjust.setBrightness(0.2);
colorAdjust.setSaturation(0.2);
ImageView imageView = new ImageView(image);//<--ERROR on taht line no suitable constructor for ImageView(java.awt.Image)
imageView.setFitWidth(imagem.getWidth());
imageView.setPreserveRatio(true);
imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
jXImageView2.setImage(imagem);
...but without successful.
Sample solution
Image on the left is the original image.
Image on the right is the adjusted image (which has had the color desaturated to make the image monochrome).
This solution works by:
Converting the Swing/AWT BufferedImage into a JavaFX Image.
Using the JavaFX ColorAdjust effect to modify the image.
A snapshot of the color adjusted image is taken to create a new JavaFX image.
The new JavaFX image is converted back to a new Swing/AWT BufferedImage.
Because the solution mixes two different toolkits, the following considerations were applied when creating it:
Be careful of imports used to ensure that the correct class is being used for a given toolkit call; e.g., both JavaFX and Swing/AWT have Color and Image classes, so it is necessary to ensure that the fully qualified class for a given toolkit is used in the right context - passing a Swing Image directly to a JavaFX API would be wrong and vice-versa.
Be careful of threading rules. Snapshots of JavaFX scenes must be made on the JavaFX application thread. Execution of Swing APIs must be made on the Swing event dispatch thread. Various utilities of the respective toolkits (e.g., SwingUtilities and the JavaFX Platform class) are used to ensure threading constraints of the given toolkits are satisfied.
The JavaFX toolkit must be initialized before it can be used. Normally this is done implicitly when your application extends the JavaFX Application class. However Swing applications do not extend the JavaFX application class. So, perhaps somewhat counter-intuitively and poorly documented, a JFXPanel must be instantiated to initialize the JavaFX toolkit before the toolkit is used.
Notes
This solution is crafted to fit the particular requirements of the question (which is a Swing application which needs to make some color adjustments). If you only wish to adjust image colors from within JavaFX and not use Swing, then more straight-forward solutions exist and are preferred.
Calling System.exit is generally enough to shut the JavaFX toolkit down. The sample application calls Platform.exit to explicitly shut the JavaFX toolkit down, but in this case the explicit call to Platform.exit is probably unnecessary.
This means that the ColorAdjuster in the solution can be used from a Swing program without the Swing program explicitly importing any JavaFX classes (although, internally, the ColorAdjuster will import those classes and the system must meet the normal minimum requirements to run both the Swing and JavaFX toolkits). Reducing mixing of imports to a single toolkit per class where possible is desirable because mixing imports within a single class for a mixed JavaFX/Swing application is a good source of tedious errors, due to potential name clashes and threading related headaches.
ColorAdjuster.java
Image color adjusting utility.
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.SnapshotParameters;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javax.swing.SwingUtilities;
import java.awt.image.BufferedImage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/** Uses JavaFX to adjust the color of an AWT/Swing BufferedImage */
public class ColorAdjuster {
// Instantiation of a JFXPanel is necessary otherwise the JavaFX toolkit is not initialized.
// The JFXPanel doesn't actually need to be used, instantiating it in the constructor is enough to trigger toolkit initialization.
private final JFXPanel fxPanel;
public ColorAdjuster() {
// perhaps this check is not necessary, but I feel a bit more comfortable if it is there.
if (!SwingUtilities.isEventDispatchThread()) {
throw new IllegalArgumentException(
"A ColorAdjuster must be created on the Swing Event Dispatch thread. " +
"Current thread is " + Thread.currentThread()
);
}
fxPanel = new JFXPanel();
}
/**
* Color adjustments to the buffered image are performed with parameters in the range -1.0 to 1.0
*
* #return a new BufferedImage which has colors adjusted from the original image.
**/
public BufferedImage adjustColor(
BufferedImage originalImage,
double hue,
double saturation,
double brightness,
double contrast
) throws ExecutionException, InterruptedException {
// This task will be executed on the JavaFX thread.
FutureTask<BufferedImage> conversionTask = new FutureTask<>(() -> {
// create a JavaFX color adjust effect.
final ColorAdjust monochrome = new ColorAdjust(0, -1, 0, 0);
// convert the input buffered image to a JavaFX image and load it into a JavaFX ImageView.
final ImageView imageView = new ImageView(
SwingFXUtils.toFXImage(
originalImage, null
)
);
// apply the color adjustment.
imageView.setEffect(monochrome);
// snapshot the color adjusted JavaFX image, convert it back to a Swing buffered image and return it.
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setFill(Color.TRANSPARENT);
return SwingFXUtils.fromFXImage(
imageView.snapshot(
snapshotParameters,
null
),
null
);
});
Platform.runLater(conversionTask);
return conversionTask.get();
}
}
ColorAdjustingSwingAppUsingJavaFX.java
Test harness:
import javafx.application.Platform;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class ColorAdjustingSwingAppUsingJavaFX {
private static void initAndShowGUI() {
try {
// This method is invoked on Swing thread
JFrame frame = new JFrame();
// read the original image from a URL.
URL url = new URL(
IMAGE_LOC
);
BufferedImage originalImage = ImageIO.read(url);
// use JavaFX to convert the original image to monochrome.
ColorAdjuster colorAdjuster = new ColorAdjuster();
BufferedImage monochromeImage = colorAdjuster.adjustColor(
originalImage,
0, -1, 0, 0
);
// add the original image and the converted image to the Swing frame.
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(
new JLabel(
new ImageIcon(originalImage)
)
);
frame.getContentPane().add(
new JLabel(
new ImageIcon(monochromeImage)
)
);
// set a handler to close the application on request.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// shutdown the JavaFX runtime.
Platform.exit();
// exit the application.
System.exit(0);
}
});
// display the Swing frame.
frame.pack();
frame.setLocation(400, 300);
frame.setVisible(true);
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
ColorAdjustingSwingAppUsingJavaFX::initAndShowGUI
);
}
// icon source: http://www.iconarchive.com/artist/aha-soft.html
// icon license: Free for non-commercial use, commercial usage: Not allowed
private static final String IMAGE_LOC =
"http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Pizza-icon.png";
}
You need to convert the BufferedImage to a javafx.scene.image.Image, you can use something like...
Image image = SwingFXUtils.toFXImage(imagem, null);
Then you can apply the ColorAdjust...
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.1);
colorAdjust.setHue(-0.05);
colorAdjust.setBrightness(0.1);
colorAdjust.setSaturation(0.2);
ImageView imageView = new ImageView(image);
imageView.setFitWidth(image.getWidth());
imageView.setPreserveRatio(true);
imageView.setEffect(colorAdjust);
Then convert it back again...
imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
This idea is stolen from jewelsea / SaveAdjustedImage.java. What I don't know is, if the ImageView needs to be realised on the screen first all not...
Updated
Just so you are aware, you are crossing two different UI frameworks, like they say in the films, "don't cross the streams!"
JavaFX has a much more tightly controlled set of requirements then Swing does, this is both a good and bad thing.
What you MUST do, is get the JavaFX code to run within it's event thread. This is more tricky than it sounds (and seems to need to be), for example...
Original | Color adjustments (taken from the JavaDocs example) | Monochrome...
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
public class Test extends Application {
public static void main(String[] args) {
Application.launch();
}
#Override
public void start(Stage stage) throws Exception {
try {
System.out.println("Load image...");
BufferedImage imagem = ImageIO.read(new File("..."));
Image image = SwingFXUtils.toFXImage(imagem, null);
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setHue(0);
colorAdjust.setSaturation(-1);
colorAdjust.setBrightness(0);
colorAdjust.setContrast(0);
// colorAdjust.setHue(-0.05);
// colorAdjust.setSaturation(0.2);
// colorAdjust.setBrightness(0.1);
// colorAdjust.setContrast(0.1);
ImageView imageView = new ImageView(image);
imageView.setFitWidth(image.getWidth());
imageView.setPreserveRatio(true);
imageView.setEffect(colorAdjust);
System.out.println("Convert and save...");
imagem = SwingFXUtils.fromFXImage(imageView.snapshot(null, null), null);
ImageIO.write(imagem, "png", new File("ColorAdjusted.png"));
} catch (IOException exp) {
exp.printStackTrace();
} finally {
Platform.exit();
}
}
}
The next thing is trying to work out how you would get this to work as a utility class...

Categories