I'm working on an app that uses JavaFX as GUI to control a PApplet that runs in a different window.
I've managed to make the two things appear and work, but when I try to load files in the PApplet
class, I get a warning that says "The sketch path is not set"
and an error like this: "java.lang.RuntimeException: Files must be loaded inside setup() or after it has been called."
I'm guessing that I might not have initialized the PApplet properly.
Here is my javafx.Application class
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("application.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Clusters");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PApplet.main("application.Controller");
launch(args);
}
}
Here is my PApplet class
public class Controller extends PApplet {
private ArrayList<File> files;
private ArrayList<PImage> images;
public Controller() {
}
public void settings() {
size(640, 360);
}
public void setup() {
background(0);
images = new ArrayList<PImage>();
files = new ArrayList<File>();
}
public void draw() {
}
public void importImages() {
// Open File Chooser
FileChooser dialog = new FileChooser();
dialog.setTitle("Import Images");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image files (*.jpg, *.png, *.tiff)", "*.jpg", "*.png", "*.tiff");
dialog.getExtensionFilters().add(extFilter);
List<File> imported_files = dialog.showOpenMultipleDialog(new Stage());
System.out.println(imported_files);
for (File f: imported_files) {
images.add(loadImage(f.getAbsolutePath()));
System.out.println(f.getName() + " loaded");
}
}
}
This is how I link the importImages() method to the FXXML file
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="file_open" mnemonicParsing="false" text="Open" />
<MenuItem fx:id="file_save" mnemonicParsing="false" text="Save" />
<MenuItem fx:id="file_save_as" mnemonicParsing="false" text="Save As" />
<MenuItem fx:id="file_import" mnemonicParsing="false" onAction="#importImages" text="Import" />
</items>
</Menu>