We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I have a probem with nesting Processing inside JavaFX. I've got a heavy-GUI application which I would not like to rewrite in Swing neither split into parts and add to sketch itself. Reading the forum I was able to find out how to extract sketch canvas and put it inside JPanel:
public void initialize(SwingNode swingNode) {
this.swingNode = swingNode;
PSurface pSurface = this.initSurface();
pSurface.setSize(640, 360);
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas) pSurface.getNative();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(smoothCanvas, BorderLayout.CENTER);
panel.setSize(640, 360);
swingNode.setContent(panel);
pSurface.startThread();
}
@Override
public void settings() {
size(640, 360);
}
@Override
public void setup() {
background(0);
}
@Override
public void draw() {
background(0);
stroke(255);
ellipse(56, 46, 55, 55);
}
The problem is that when I put the JPanel on the SwingNode - JPanel does not render or an exception is thrown:
Exception in thread "Animation Thread" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:74)
at sun.java2d.pipe.hw.AccelTypedVolatileImage.<init>(AccelTypedVolatileImage.java:56)
at sun.java2d.d3d.D3DGraphicsConfig.createCompatibleVolatileImage(D3DGraphicsConfig.java:295)
at sun.awt.windows.TranslucentWindowPainter$VIWindowPainter.getBackBuffer(TranslucentWindowPainter.java:266)
at sun.awt.windows.WWindowPeer.getTranslucentGraphics(WWindowPeer.java:627)
at sun.awt.windows.WComponentPeer.getGraphics(WComponentPeer.java:582)
at sun.java2d.d3d.D3DGraphicsConfig.flip(D3DGraphicsConfig.java:194)
at sun.awt.windows.WComponentPeer.flip(WComponentPeer.java:1011)
at java.awt.Component$FlipBufferStrategy.flip(Component.java:4087)
at java.awt.Component$FlipBufferStrategy.show(Component.java:4213)
at processing.awt.PSurfaceAWT.render(PSurfaceAWT.java:305)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1558)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
Has someone managed to successfully embed the sketch inside JavaFX GUI?
Answers
Hi, You should take a look at : https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/embed-swing.htm#BCGCDEBD