Skip to content

Commit 0debbc5

Browse files
Create ProcessingFX.pde
Add standalone sketch example from forum to library
1 parent 5321ee7 commit 0debbc5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

ProcessingFX.pde

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Stand-alone example of using an FXML file from a Processing .pde sketch.
3+
* Does not rely on the ProcessingFX library.
4+
*
5+
* Load a new scene from an FXML file and replace the the default scene Processing
6+
* makes with this new one (during runtime).
7+
*
8+
* https://discourse.processing.org/t/fxml-file-integrate-in-processing/18325/10
9+
*/
10+
11+
import java.util.Map;
12+
import java.nio.file.Paths;
13+
14+
import javafx.application.Platform;
15+
import javafx.fxml.FXMLLoader;
16+
import javafx.scene.Parent;
17+
import javafx.scene.Scene;
18+
import javafx.scene.SceneAntialiasing;
19+
import javafx.scene.canvas.Canvas;
20+
import javafx.scene.layout.AnchorPane;
21+
import javafx.stage.Stage;
22+
23+
import processing.javafx.PSurfaceFX;
24+
25+
public void setup() {
26+
size(800, 800, FX2D);
27+
strokeWeight(3);
28+
}
29+
30+
protected PSurface initSurface() {
31+
surface = (PSurfaceFX) super.initSurface();
32+
final Canvas canvas = (Canvas) surface.getNative();
33+
final Scene oldScene = canvas.getScene();
34+
final Stage stage = (Stage) oldScene.getWindow();
35+
36+
try {
37+
FXMLLoader loader = new FXMLLoader(Paths.get("C:\\path--to--fxml\\stage.fxml").toUri().toURL()); // abs path to fxml file
38+
final Parent sceneFromFXML = loader.load();
39+
final Map<String, Object> namespace = loader.getNamespace();
40+
41+
final Scene newScene = new Scene(sceneFromFXML, stage.getWidth(), stage.getHeight(), false,
42+
SceneAntialiasing.BALANCED);
43+
final AnchorPane pane = (AnchorPane) namespace.get("anchorPane"); // get element by fx:id
44+
45+
pane.getChildren().add(canvas); // processing to stackPane
46+
canvas.widthProperty().bind(pane.widthProperty()); // bind canvas dimensions to pane
47+
canvas.heightProperty().bind(pane.heightProperty()); // bind canvas dimensions to pane
48+
49+
Platform.runLater(new Runnable() {
50+
@Override
51+
public void run() {
52+
stage.setScene(newScene);
53+
}
54+
}
55+
);
56+
}
57+
catch (IOException e) {
58+
e.printStackTrace();
59+
}
60+
return surface;
61+
}
62+
63+
public void draw() {
64+
background(125, 125, 98);
65+
ellipse(200, 200, 200, 200);
66+
line(0, 0, width, height);
67+
line(width, 0, 0, height);
68+
}

0 commit comments

Comments
 (0)