我一直在尝试将PApplet与GUI构建器(2.0)中设计的JavaFX GUI集成 . 创建SwingNode并向其添加草图实例后,在绘制一次后无法更新其框架 .

我对JavaFX的经验很少,而且使用Swing,我不知道如何访问我在 FXMLDocumentController 类之外的GUI构建器中放置的组件 . 我假设草图需要从 JFXP 类初始化,但我不知道如何访问SwingNode的内容 . 理想情况下,我希望能够使用场景构建器[2.0]来创建GUI,而不是非视觉地创建组件 .

任何帮助将不胜感激!

FXML控制器:

import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javax.swing.JPanel;
import processing.core.*;

public class FXMLDocumentController implements Initializable {

    mySketch p = new mySketch();
    @FXML
    public Label label;
    public SwingNode sn;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        JPanel panel = new JPanel();

        panel.add(p);
        p.init();
        sn.setContent(panel);

    }

    static class mySketch extends PApplet {

        int x, y;

        @Override
        public void setup() {
            smooth();
            size(500, 200);
            x = 0;
            y = 0;
            loop();
        }

        @Override
        public void draw() {
            background(255);
            x = x + randInt(0, 5);
            y = y + randInt(0, 5);
            ellipse(mouseX, mouseY, 5, 5);
        }

        @Override
        public void mouseMoved() {

        }
    }

    public static int randInt(int min, int max) {

    // NOTE: Usually this should be a field rather than a method
        // variable so that it is not re-seeded every call.
        Random rand = new Random();

    // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.embed.swing.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="papplettest.FXMLDocumentController">
   <children>
      <SwingNode fx:id="sn" layoutX="160.0" layoutY="100.0" />
   </children>
</AnchorPane>

主要课程:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author hugodarwood
 */
public class JFXP extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);

        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

UPDATE: 经过大量研究(并在Swing中设计了太多应用程序)后,我得出结论认为将PApplet与JAVAFX GUI集成是可能的 . 最好的做法是坚持使用Swing(从处理3.0开始不可能)或者使用JAVAFX学习图形编程 .

Hope is not lost! 这有一个非常脏的解决方法 . 只需使用processing.js编写草图,将其嵌入HTML画布中,然后将JAVAFX WebView组件添加到项目中,将HTML画布设置为WebEngine目标 . 这样就可以继续使用现有的处理草图和JAVAFX UI!