Using Netbeans Visual Library in Javafx - java

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();

Related

Blurry render of SwingNode in JavaFX on Windows

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.

Why is my object not displaying in JavaFX?

This should be a relatively simple problem, but it is driving me insane. I am trying to create Mine Sweeper in JavaFX (mostly just for practice) but I can not get even a simple rectangle to display. I had the game running once before, but I am trying to make the game more abstract, and hence easier to code, but I am running into the issue of nothing being displayed.
I eliminated all extraneous code so it is as simple as possible. I am basically trying to create a Rectangle with a certain color and size called Box, add the box to the pane, and display the pane. In order to make Box a node that can be displayed on the pane, I made the Box class extend Rectangle, so that a Box would have the same properties as a Rectangle. But when I run the code, it gives just an empty pane with no box in it.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Minesweeper extends Application {
#Override
public void start(Stage stage) {
Pane pane = new Pane();
Box box = new Box();
pane.getChildren().addAll(box);
// Create the scene
Scene scene = new Scene(pane);
stage.setTitle("Minesweeper");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Box extends Rectangle {
public Box() {
Rectangle box = new Rectangle(100, 100, 100, 100);
box.setFill(Color.BLUE);
}
}
I realized if I put the code from Box into the main Minesweeper class, it will display the box. But Box will have a ton of other properties and therefore needs to be a class on its own.
What am I doing wrong that does not allow the box to be displayed?
Thanks in advance for your help and consideration.
You create a new Rectangle in your Box class. This Rectangle is not added to any Parent container, so it's not visible.
Change your code to:
public Box() {
super(100, 100, 100, 100);
setFill(Color.BLUE);
}

JavaFX and Swing performance issues

We have been having perpetual performance issues when running JavaFX inside a JFXPanel in Swing based applications.
This seems to only be a problem when running on JDK1.7, because whenever it is possible to run JDK1.8 this works perfectly without changing any code.
The symptoms are that the application seems to render fonts in a fuzzy way and also the performance is terrible (multiple seconds to respond to keypress when typing in a TextField).
We are observing the correct rules about EDT, AWT and Platform threads, so I doubt that this can be the issue.
We are stuck having to support JDK1.7 because this is a plug-in for NetBeans which some users will be running on JDK1.7 for various good reasons and we cannot force them to upgrade.
EDIT: Here is a MCVE which recreates the problem
package javaapplication3;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
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(new Runnable() {
#Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
TextField text = new TextField();
Label label = new Label();
VBox box = new VBox();
label.setText("This is a test label");
box.getChildren().add(label);
box.getChildren().add(text);
root.getChildren().add(box);
return (scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}
The tests we are performing have very simple javafx with e.g. only an AnchorPane with a TextField on it and absolutely no code behind it, and just typing in the TextField is painfully slow.
Behavior looks very much like lock contention between the Swing and JavaFX threads, but it does not seem like we can find any explanation or solution.
This is not the answer that you are looking for, but since we have the same problem with Java 7 support, the answer is that Java 7 has reached its end of life:
July 2015: Updates for Java 7 are no longer available to the public.
Oracle offers updates to Java 7 only for customers who have purchased
Java support or have Oracle products that require Java 7.
https://www.java.com/en/download/faq/java_7.xml
There are no "good reasons" if problem solving is as easy as using a different java version. You don't break things by upgrading to Java 8.

Frosted Glass Effect in JavaFX?

I'm making an iOS7-themed JavaFX2/FXML project and I was wondering how I could make a Rectangle object have a iOS7-like frosted glass effect.
I'd also like it to have a small shadow. This is tricky, since you might be able to see the shadow behind the semi-transparent object. I'd just like it to be present around the edges.
Is this possible? Here's a picture showing the desired effect (not including the small drop-shadow):
UPDATE: Here's a continuation of the issue. This is going to look amazing :D.
Sample Solution
Run the program below and scroll or swipe up to show the glass pane.
The purpose of the program is just to sample the techniques involved not to act as a general purpose library for the frost effect.
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.geometry.Rectangle2D;
import javafx.scene.*;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
// slides a frost pane in on scroll or swipe up; slides it out on scroll or swipe down.
public class Frosty extends Application {
private static final double W = 330;
private static final double H = 590;
private static final double BLUR_AMOUNT = 60;
private static final Duration SLIDE_DURATION = Duration.seconds(0.4);
private static final double UPPER_SLIDE_POSITION = 100;
private static final Effect frostEffect =
new BoxBlur(BLUR_AMOUNT, BLUR_AMOUNT, 3);
#Override public void start(Stage stage) {
DoubleProperty y = new SimpleDoubleProperty(H);
Node background = createBackground();
Node frost = freeze(background, y);
Node content = createContent();
content.setVisible(false);
Scene scene = new Scene(
new StackPane(
background,
frost,
content
)
);
stage.setScene(scene);
stage.show();
addSlideHandlers(y, content, scene);
}
// create a background node to be frozen over.
private Node createBackground() {
Image backgroundImage = new Image(
getClass().getResourceAsStream("ios-screenshot.png")
);
ImageView background = new ImageView(backgroundImage);
Rectangle2D viewport = new Rectangle2D(0, 0, W, H);
background.setViewport(viewport);
return background;
}
// create some content to be displayed on top of the frozen glass panel.
private Label createContent() {
Label label = new Label("The overlaid text is clear and the background below is frosty.");
label.setStyle("-fx-font-size: 25px; -fx-text-fill: midnightblue;");
label.setEffect(new Glow());
label.setMaxWidth(W - 20);
label.setWrapText(true);
return label;
}
// add handlers to slide the glass panel in and out.
private void addSlideHandlers(DoubleProperty y, Node content, Scene scene) {
Timeline slideIn = new Timeline(
new KeyFrame(
SLIDE_DURATION,
new KeyValue(
y,
UPPER_SLIDE_POSITION
)
)
);
slideIn.setOnFinished(e -> content.setVisible(true));
Timeline slideOut = new Timeline(
new KeyFrame(
SLIDE_DURATION,
new KeyValue(
y,
H
)
)
);
scene.setOnSwipeUp(e -> {
slideOut.stop();
slideIn.play();
});
scene.setOnSwipeDown(e -> {
slideIn.stop();
slideOut.play();
content.setVisible(false);
});
// scroll handler isn't necessary if you have a touch screen.
scene.setOnScroll((ScrollEvent e) -> {
if (e.getDeltaY() < 0) {
slideOut.stop();
slideIn.play();
} else {
slideIn.stop();
slideOut.play();
content.setVisible(false);
}
});
}
// create a frosty pane from a background node.
private StackPane freeze(Node background, DoubleProperty y) {
Image frostImage = background.snapshot(
new SnapshotParameters(),
null
);
ImageView frost = new ImageView(frostImage);
Rectangle filler = new Rectangle(0, 0, W, H);
filler.setFill(Color.AZURE);
Pane frostPane = new Pane(frost);
frostPane.setEffect(frostEffect);
StackPane frostView = new StackPane(
filler,
frostPane
);
Rectangle clipShape = new Rectangle(0, y.get(), W, H);
frostView.setClip(clipShape);
clipShape.yProperty().bind(y);
return frostView;
}
public static void main(String[] args) { launch(args); }
}
Source image
Save this image parallel to the Java source as a file named ios-screenshot.png and have your build system copy it to the target directory for the binary output of the build.
Answers to additional questions
"JDK 8," would that happen to be a requirement of this?
The sample code above is written against JDK 8. Porting it back to JDK 7 by replacing the lambda calls with anonymous inner classes is pretty trivial.
In general, Java 7 is pretty dated for JavaFX work. I advise upgrading at your earliest convenience to work with a Java 8 minimum version.
initiating your Panes with arguments
More convenient constructors for most parent nodes is a Java 8 feature. You can easily convert the Java 8 format:
StackPane stack = new StackPane(child1, child2);
To Java 7:
StackPane stack = new StackPane();
stack.getChildren().setAll(child1, child2);
will this working if the desktop is behind a frosty pane?
Not directly, you can create a new question for that.
Update: Related Questions
User created: JavaFX effect on background to allow the frosted effect to apply to a window over a desktop background.
Another user created: How do I create a JavaFX transparent stage with shadows on only the border? to apply a halo shadow effect around this window.

Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application

I was looking for a way to integrate a Web-Browser-Component in an existing Swing-Application and found WebView for Java FX 2.0. Furthermore I found a blog post on java.net showing how to integrate a Java FX component into a Swing Application . So I guess it might be doable, but I haven't tried yet.
I'm curious, do you think this is a good approach? Are there any better solutions? Is it even doable? Is maybe something prebundled out there?
The motivation is: I want to integrate some WebBrowser-whatever into an existing Swing-Application, the long-term goal being to get rid of the whole Java Desktop Application at all, replacing it with a web-based solution (the plan is to slowly convert existing aspects into webpages which are then displayed in the WebBrowser-Component until nothing is left of the swing application except for the browser-skeleton). The backend of course remains Java :-)
I haven't tried yet since I simply lack the time to integrate JavaFX with my project (its a job, we're just exploring alternatives fpr the long run), so I better ask before I get burned.
It is very well possible!
One has to install JavaFX 2.0, and somehow manage to have jfxrt.jar in the Classpath.
The following code renders a JFXPanel inside a JFrame. The JFXPanel contains a WebView which loads google.com.
However, at least on my machine, the WebView feels rather sloppy. I'm working on Mac OS X 10.6; JavaFX 2.0 is still in beta for OS X.
Alternatives I found include MozSwing, which looked very promising and feels quite fast actually. Sadly the project is not being developed any further since 2008 and the bundled XUL runner is rather old (no new fancy html 5).
Both approaches are a nightmare to include via maven, you better setup your own local repository.
import java.awt.Dimension;
import java.awt.Point;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class JavaFX {
/* Create a JFrame with a JButton and a JFXPanel containing the WebView. */
private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); // do the layout manually
final JButton jButton = new JButton("Button");
final JFXPanel fxPanel = new JFXPanel();
frame.add(jButton);
frame.add(fxPanel);
frame.setVisible(true);
jButton.setSize(new Dimension(200, 27));
fxPanel.setSize(new Dimension(300, 300));
fxPanel.setLocation(new Point(0, 27));
frame.getContentPane().setPreferredSize(new Dimension(300, 327));
frame.pack();
frame.setResizable(false);
Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
#Override
public void run() {
initFX(fxPanel);
}
});
}
/* Creates a WebView and fires up google.com */
private static void initFX(final JFXPanel fxPanel) {
Group group = new Group();
Scene scene = new Scene(group);
fxPanel.setScene(scene);
WebView webView = new WebView();
group.getChildren().add(webView);
webView.setMinSize(300, 300);
webView.setMaxSize(300, 300);
// Obtain the webEngine to navigate
WebEngine webEngine = webView.getEngine();
webEngine.load("http://www.google.com/");
}
/* Start application */
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}

Categories