client gui + headless server

This commit is contained in:
2025-03-04 16:07:35 +01:00
parent e536a45266
commit 76da347fb9
4 changed files with 61 additions and 2 deletions

View File

@@ -8,6 +8,7 @@
plugins { plugins {
// Apply the application plugin to add support for building a CLI application in Java. // Apply the application plugin to add support for building a CLI application in Java.
id 'application' id 'application'
id 'org.openjfx.javafxplugin' version '0.1.0'
} }
repositories { repositories {
@@ -32,9 +33,13 @@ java {
} }
} }
javafx {
modules = ['javafx.graphics', 'javafx.controls', 'javafx.fxml' ]
}
application { application {
// Define the main class for the application. // Define the main class for the application.
mainClass = 'ChatApp' mainClass = 'client.ClientGui'
} }
run { run {
@@ -48,7 +53,7 @@ tasks.named('test') {
useJUnitPlatform() useJUnitPlatform()
} }
tasks.register("server", JavaExec){ tasks.register("admin", JavaExec){
group = "ChatAppConsole" group = "ChatAppConsole"
description = "Runs the server + the admin client" description = "Runs the server + the admin client"
mainClass.set("ChatApp") mainClass.set("ChatApp")
@@ -56,6 +61,14 @@ tasks.register("server", JavaExec){
standardInput = System.in standardInput = System.in
} }
tasks.register("server", JavaExec){
group = "ChatAppConsole"
description = "Runs the headless server"
mainClass.set("ChatAppServer")
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
}
tasks.register("client", JavaExec) { tasks.register("client", JavaExec) {
group = "ChatAppConsole" group = "ChatAppConsole"
description = "Runs the client" description = "Runs the client"

View File

@@ -0,0 +1,15 @@
import java.net.SocketException;
import server.Server;
public class ChatAppServer {
public static void main(String[] args) {
// run ./gradlew server --args="port" to launch the server with a specific port
try {
Server server = new Server(args.length > 0 ? Integer.parseInt(args[0]) : 0);
System.out.println("Server running on port " + server.getRunningPort() + "!");
} catch (SocketException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,28 @@
package client;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ClientGui extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("JavaFX with Gradle");
Label label = new Label("Hello, JavaFX!");
StackPane root = new StackPane(label);
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
}

View File

@@ -149,4 +149,7 @@ public class Server implements PacketHandler {
return false; return false;
} }
public int getRunningPort() {
return this.serverSocket.getLocalPort();
}
} }