Java Processing 3 PAplet in JavaFX scene as FXNode

前端 未结 4 1014
小鲜肉
小鲜肉 2021-01-13 14:16

I am trying to make a program for visual analyzing Fractal sets. I choose Processing 3 as drawing library and JavaFX for the user interface. There are some screenshots of th

相关标签:
4条回答
  • 2021-01-13 14:31

    To make it work, you have to launch your Processing sketch, not the JavaFX Application.

    Simply do

    PApplet.main(Launcher.class.getName());
    

    Also thanks so much for your help! I had NO idea how I should use the JavaFX stuff that comes with Processing!

    0 讨论(0)
  • 2021-01-13 14:32

    Okay, this is my code, that works! I copied everything and changed the names. !!!I have not tested this modified code, so don't copy paste everything!!! The raw principle should definetly work though.

    If you still have issues or questions, just comment.

    Main

    public class Main {
    
        public static void main(String[] args) {
    
        // Your code starts here, and runs Processing. 
        // This is also, how you normally start Processing sketches.
        PApplet.main(Sketch.class.getName());
        }
    }
    

    Sketch

    public class Sketch extends PApplet{
    
        @Override
        public void settings() {
            size(200, 200, FX2D); // Size doesn't really matter
        }
    
    
        @Override
        public void setup() {
    
        }
    
        @Override
        public void draw() {
    
        }
    
    
    // Processing uses this function to determine, 
    // how to display everything, how to open the canvas...
    // We override the code, that would normally open a window with the normal Processing stuff,
    // to open start new JavaFX application in a new Thread.
    
    
    // micycle's code
        @Override
        protected PSurface initSurface() {
            g = createPrimaryGraphics();
            PSurface genericSurface = g.createSurface();
            PSurfaceFX fxSurface = (PSurfaceFX) genericSurface;
    
            fxSurface.sketch = this;
    
            // Because the JavaFX App is being launched by reflection,
            // we can't pass variables to it via constructor, so
            // we have to access it in static context.
    
            // Here, we give JavaFX the surface.
            ExampleApp.surface = fxSurface;
    
            // New thread started, so JavaFX and Processing don't interrupt each other.
            new Thread(new Runnable() {
                public void run() {
    
                    // JavaFX way of launching a new Application
                    Application.launch(ExampleApp.class);
                }
            }).start();
    
        while (fxSurface.stage == null) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
    
            }
        }
    
            this.surface = fxSurface;
            return fxSurface;
        }
    }
    

    ExampleApp

    public class ExampleApp extends Application {
    
        public Canvas canvas; // The Canvas you will be drawing to 
        public static PSurfaceFX surface; // The Processing surface
    
    
    
        // JavaFX started, this method is being run to set everything up.
        @Override
        public void start(Stage primaryStage) {
    
            // This sets up the canvas, and the drawing region.
            canvas = (Canvas) surface.getNative();
            surface.fx.context = canvas.getGraphicsContext2D();
            surface.stage = primaryStage;
    
    
            // I'm just loading my FXML file. You can do all JavaFX stuff via code, if you want
            try {
    
                // !!My root Container is a BorderPane!!
                BorderPane root = FXMLLoader.load(getClass().getClassLoader().getResource("application.fxml"));
            } catch(IOException e) {
                e.printStackTrace();
            }
    
            // Getting the Anchor pane, that is in the center of my BorderPane
            AnchorPane pane = (AnchorPane) root.getCenter();
    
            // The Anchor pane is being used, so the canvas can fill the parent (Center)
            // Canvases don't have a property to fill it's parent, like most Containers do (Because it isn't a container)
            canvas.widthProperty().bind(pane.widthProperty());
            canvas.heightProperty().bind(pane.heightProperty());
    
            // Adding the canvas to your App
            root.getChildren().add(canvas);
    
            // Launching the Stage
            primaryStage.setTitle("Example App");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
    
    }
    
    0 讨论(0)
  • 2021-01-13 14:32

    Okay, since last time , I changed some elements. The canvas's parent is now just a Pane instead of an AnchorPane.

    The FXML won't help you much... It is just a BorderPane with another Pane in it, but alright...

     <center>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
    
            <Pane maxHeight="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
    
         </children>
      </VBox>
    

    So, what I'm doing is taking the Canvas element, Processing creates and just adding it to the Pane.

    0 讨论(0)
  • 2021-01-13 14:36

    I have devised two approaches: in the first, we bypass Processing's JavaFX stage creation and point Processing to draw into a JavaFX stage loaded from an FXML file; in the second, we replace Processing's default JavaFX scene with one loaded from an FXML file during runtime.

    1. Launching from an FXML

    With the first approach we launch the application like we would a JavaFX app (using Application.launch(Launcher.class);), completely bypassing Processing's JavaFX stage creation code.

    You'll have to download a slightly modified core.jar for this approach to work, where I've changed the visibility of a few members of the PSurfaceFX and PGraphicsFX2D classes from Protected to Public. The changes allow us to launch JavaFX from our own ... extends Application class, while maintaining access to the members that Processing needs to set during the launch to function.

    Processing 3 crashes in FX2D mode when the JDK in use is above Java 8, so I've also made a working version for 8+, since the FXML files usually need at least Java 9 to work.

    • Download core.jar (Java 8 & below)
    • Download core.jar (Above Java 8)

    This is the FXML file I am working with in this example:

    With the modified core.jar added to your project's classpath, override initSurface() in your PApplet class with the following snippet. With this code, we bypass the PApplet's call to initFrame() - this is where processing creates its own JavaFX stage, which we do not want it to do.

    @Override
    protected PSurface initSurface() {
        g = createPrimaryGraphics();
        PSurface genericSurface = g.createSurface();
        PSurfaceFX fxSurface = (PSurfaceFX) genericSurface;
    
        fxSurface.sketch = this;
    
        Launcher.surface = fxSurface;
    
        new Thread(new Runnable() {
            public void run() {
                Application.launch(Launcher.class);
            }
        }).start();
    
        while (fxSurface.stage == null) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
            }
        }
    
        this.surface = fxSurface;
        return fxSurface;
    }
    

    Set the PApplet's renderering mode to FX2D like so:

    @Override
    public void settings() {
        size(0, 0, FX2D);
    }
    

    Put the following, or similar, in your Launcher class. In this example, I have manually found the Node that I want to add the canvas object into. There are better, more programmatic, ways of doing this (such as .lookup() using the fx:id of the desired node -- this can be defined in the FXML file). I have also bound the dimensions of the canvas to those of its parent, so when the divisor separating the Master and View panes is dragged, the Processing canvas resizes accordingly.

    public class Launcher extends Application {
    
        public static PSurfaceFX surface;
    
        @Override
        public void start(Stage primaryStage) throws Exception {
    
            Canvas canvas = (Canvas) surface.getNative(); // boilerplate
            GraphicsContext graphicsContext = canvas.getGraphicsContext2D(); // boilerplate
            surface.fx.context = graphicsContext; // boilerplate
    
            primaryStage.setTitle("FXML/Processing");
    
            VBox root = FXMLLoader.load(new File("c:/Users/Mike/desktop/test.fxml").toURI().toURL());
            SplitPane pane = (SplitPane) root.getChildren().get(1); // Manually get the item I want to add canvas to
            AnchorPane pane2 = (AnchorPane) pane.getItems().get(0); // Manually get the item I want to add canvas to
            pane2.getChildren().add(canvas); // Manually get the item I want to add canvas to
    
            canvas.widthProperty().bind(pane2.widthProperty());
            canvas.heightProperty().bind(pane2.heightProperty());
    
            Scene scene = new Scene(root, 800, 800);
            primaryStage.setScene(scene);
            primaryStage.show();
    
            surface.stage = primaryStage; // boilerplate
        }
    }
    

    This is the result:

    Also see this Github project -- a basic project showing how a Processing sketch and a FXML JavaFX stage may be integrated using this first approach, but includes a JavaFX Controller to populate @FXMLannotated fields (providing an easy way to first get, and then reference, JavaFX objects in code).


    2. Launching, then loading a FXML

    This approach works with vanilla Processing. Here, we launch Processing like normal and then replace the default scene with new scene loaded from an FXML file during runtime. This is a simpler approach (and doesn't require using a modified .jar!) but will make JavaFX/Processing interoperability more difficult because we can't use a JavaFX Controller to get fields via FXML injection.

    Example PDE code:

    import java.util.Map;
    import java.nio.file.Paths;
    
    import javafx.application.Platform;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.SceneAntialiasing;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;
    
    import processing.javafx.PSurfaceFX;
    
    public void setup() {
      size(800, 800, FX2D);
      strokeWeight(3);
    }
    
    protected PSurface initSurface() {
      surface = (PSurfaceFX) super.initSurface();
      final Canvas canvas = (Canvas) surface.getNative();
      final Scene oldScene = canvas.getScene();
      final Stage stage = (Stage) oldScene.getWindow();
    
      try {
        FXMLLoader loader = new FXMLLoader(Paths.get("C:\\path--to--fxml\\stage.fxml").toUri().toURL()); // abs path to fxml file
        final Parent sceneFromFXML = loader.load();
        final Map<String, Object> namespace = loader.getNamespace();
    
        final Scene newScene = new Scene(sceneFromFXML, stage.getWidth(), stage.getHeight(), false, 
          SceneAntialiasing.BALANCED);
        final AnchorPane pane = (AnchorPane) namespace.get("anchorPane"); // get element by fx:id
    
        pane.getChildren().add(canvas); // processing to stackPane
        canvas.widthProperty().bind(pane.widthProperty()); // bind canvas dimensions to pane
        canvas.heightProperty().bind(pane.heightProperty()); // bind canvas dimensions to pane
    
        Platform.runLater(new Runnable() {
          @Override
            public void run() {
            stage.setScene(newScene);
          }
        }
        );
      } 
      catch (IOException e) {
        e.printStackTrace();
      }
      return surface;
    }
    
    public void draw() {
      background(125, 125, 98);
      ellipse(200, 200, 200, 200);
      line(0, 0, width, height);
      line(width, 0, 0, height);
    }
    

    Result:

    …using this FXML file:

    0 讨论(0)
提交回复
热议问题