2

I have a Spring Boot App with the following classes in the src/main/java/in/javacoder/overlayapp package

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.2'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.13'
}

javafx {
    version = "17"
    modules = [ 'javafx.controls' ]
}

group = 'in.techpro424'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

CoordinateOverlayAppApplication.java

package in.techpro424.coordinateoverlayapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CoordinateOverlayAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(CoordinateOverlayAppApplication.class, args);
    }

}

CoordinateRestController.java

package in.techpro424.coordinateoverlayapp;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CoordinateRestController {
    public static double xCoord;
    public static double yCoord;
    public static double zCoord;
  
    @PostMapping(value = "/coordinateOverlay")
    public int receiveCoordinates(@RequestParam(name = "xCoordinate") Double xCoordinate, @RequestParam(name = "yCoordinate") Double yCoordinate, @RequestParam(name = "zCoordinate") Double zCoordinate) {
      CoordinateRestController.xCoord = xCoordinate;
      System.out.println(CoordinateRestController.xCoord);

      CoordinateRestController.yCoord = yCoordinate;
      System.out.println(CoordinateRestController.yCoord);

      CoordinateRestController.zCoord = zCoordinate;
      System.out.println(CoordinateRestController.zCoord);

      javafx.application.Application.launch(RenderCoordinateOverlay.class);

      return 1;
    }


}

RenderCoordinateOverlay.java

package in.techpro424.coordinateoverlayapp;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class RenderCoordinateOverlay extends Application {
    double radius = 50;

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        Text coordinates = new Text();
        coordinates.setText("X:" + CoordinateRestController.xCoord + ", Y:" + CoordinateRestController.yCoord + ", Z:" + CoordinateRestController.zCoord);

        root.getChildren().add(coordinates);

        Scene scene = new Scene(root, Color.TRANSPARENT);

        scene.getRoot().setStyle("-fx-background-color: transparent");

        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setAlwaysOnTop(true);

        
    }
    public static void main(String[] args) {
        launch(args);
    }
}

I want to render this overlay on the screen whenever I receive the POST request with the specified params

I tried using javafx.application.Application.launch(RenderCoordinateOverlay.class); in the receiveCoordinates() function I expected the overlay to render the text the moment I sent the POST request However, when I sent the POST request, only a completely transparent window was rendered with this error in the console:

2023-01-27T15:01:27.384+05:30  WARN 13060 --- [JavaFX-Launcher] javafx: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @1ec4c0e8'

The SpringBoot application is running on my PC with a display and I expect the UI to be displayed on the PC. I use Postman to send the post.

  • You're only showing a warning, not an error. Note you can only call `launch()` once, so if you sent a second POST request, you would then get an error. Can you include your pom.xml and specify how you are sending the POST request? – James_D Jan 27 '23 at 14:38
  • 1
    Maybe [startup the JavaFX Platform](https://openjfx.io/javadoc/19/javafx.graphics/javafx/application/Platform.html#startup(java.lang.Runnable)) when [starting the Spring application](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.spring-application.application-events-and-listeners) and [exit the JavaFX platform](https://openjfx.io/javadoc/19/javafx.graphics/javafx/application/Platform.html#exit()) on [Spring application exit](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.spring-application.application-exit). – jewelsea Jan 27 '23 at 15:07
  • 1
    Replace the Application class with a standard class for the UI with no inheritance, ensuring [Platform.setImplicitExit](https://openjfx.io/javadoc/19/javafx.graphics/javafx/application/Platform.html#setImplicitExit(boolean)) is false, and, when needed, send a message to the UI class to display or update your overlay via a [JavaFX Platform runLater](https://openjfx.io/javadoc/19/javafx.graphics/javafx/application/Platform.html#runLater(java.lang.Runnable)) call. – jewelsea Jan 27 '23 at 15:11
  • [Example of using runLater to send messages to a JavaFX app](https://stackoverflow.com/questions/24320014/how-to-call-launch-more-than-once-in-java). Study the [JavaFX Application lifecycle](https://openjfx.io/javadoc/19/javafx.graphics/javafx/application/Application.html). – jewelsea Jan 27 '23 at 15:12
  • @James_D I don't have a `pom.xml`, maybe it's cause I use Gradle? I send the request using Postman – JavaCoder10 Jan 27 '23 at 15:20
  • 1
    @JavaCoder10 yes, the Gradle equivalent of a Maven `pom.xml` is `build.gradle`, which you can provide in the question instead. Also, edit the question to provide some more explanatory context of what you are trying to do. Is the SpringBoot application running on a Server with a display and do you expect the UI to be displayed on the server? Or is the SpringBoot application a standalone client app? What generates the post? What is the lifecycle of the overlay? Should it always be displayed? Does the user close it? Is the overlay updated or replaced on subsequent posts? Why do this? etc – jewelsea Jan 27 '23 at 15:21

0 Answers0