a couple of weeks ago I started programming in Scala and using Akka Actors. I'm trying to implement a scala programm that uses an Akka ActorSystem to serve multiple UI's. I am currently trying to setup an ScalaFx app and connect it through an internal actor to my actor system. Therefore I coded the following code to setup my actor system and my controller:
object Game {
// Main class of the game in which the actor system gets started
def main(args: Array[String]): Unit = {
val actorSystem = ActorSystem.create("gameActorSystem")
val controller = actorSystem.actorOf(Props[Controller], "controller")
var gui = actorSystem.actorOf(Props(new GuiApp.GuiActor(controller)), "guiActor")
println("Start Game")
gui ! StartGame
gui ! "Hello ScalaFx App."
}
}
class Controller extends Actor {
private val observers = scala.collection.mutable.SortedSet.empty[ActorRef]
override def receive: Receive = {
case StartGame => startGame()
case RegisterObserver => observers += sender(); sender ! PrintMessage("Subscription from: [" + sender().toString() + "]")
}
... controller code
}
Then I implemented a simple ScalaFx App:
object GuiApp extends JFXApp {
class GuiActor(controller: ActorRef) extends Actor {
controller ! RegisterObserver
override def receive: Receive = {
case StartGame => start()
case s:String => Platform.runLater{
label.text = "Message: " + s
}
}
}
def start(): Unit = {
val args = Array.empty[String]
main(args)
}
val label = new Label("Loading ...")
stage = new PrimaryStage {
title = "ScalaFX Game"
scene = new Scene {
root = new BorderPane {
padding = Insets(25)
center = label
}
}
}
}
So what I'm basically trying to do is to start my scala program, then register the akka system and register the controller and the gui actor. After registering the gui actor I want to start the GUI and then tell the GUI to set the "Hello ScalaFx App." String onto the GUI.
It's working so far, that the ScalaFx GUI starts up, but the initial content of the GUI with "Loading ..." is not being replaced by the "Hello ScalaFx App." String. Can somebody tell me if my approach is correct in any way or am I doing something entirely wrong?