I'm using Webcam Capture API in java to access my webcam. Webcam Capture API is built on Swing, I know that, however I want to combine the Webcam Swing class with my JavaFX class. The JavaFX class displays a rectangle on the screen. My goal is: I run my JavaFX class which displays the rectangle on the screen. At some point (e.g. mouse click) I want to start the Webcam. The Webcam is setup to look at the screen and should then do certain things with the images of the rectangle.
JavaFX class:
public class JavaFXDisplay extends Application {
@Override
public void start(Stage primaryStage) {
WebcamCapture wc = new WebcamCapture();
StackPane root = new StackPane();
Rectangle rectangle = new Rectangle();
rectangle.setWidth(500);
rectangle.setHeight(500);
Scene scene = new Scene(root, 1000, 1000);
root.getChildren().addAll(rectangle);
primaryStage.setScene(scene);
primaryStage.show();
scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
wc.doSomething();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Swing class:
public class WebcamCapture extends JFrame implements Runnable, ThreadFactory {
private static final long serialVersionUID = 6441489157408381878L;
private Executor executor = Executors.newSingleThreadExecutor(this);
private Webcam webcam = null;
private WebcamPanel panel = null;
private JTextArea textarea = null;
public WebcamCapture() {
super();
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension size = WebcamResolution.QVGA.getSize();
webcam = Webcam.getWebcams().get(0);
webcam.setViewSize(size);
panel = new WebcamPanel(webcam);
panel.setPreferredSize(size);
textarea = new JTextArea();
textarea.setEditable(false);
textarea.setPreferredSize(size);
add(panel);
add(textarea);
pack();
setVisible(true);
}
public void doSomething() {
executor.execute(this);
}
@Override
public void run() {
do {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
BufferedImage image = null;
if (webcam.isOpen()) {
if ((image = webcam.getImage()) == null) {
continue;
}
doSomeStuff;
}
} while (true);
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "example-runner");
t.setDaemon(true);
return t;
}
public static void main(String[] args) {
new WebcamCapture();
}
}
However my JavaFX class is not starting/displaying. What is wrong with my code?