0

my apologies if this is an easy thing for you, but my mind boggles. After several years of not programming at all, I am working on a pet project (2d tile based game engine) where I would like to use Java FX headless in order to make use of the graphics capabilities. I have understood from here and here

that you need to a Java FX Application in order to have the graphics system initialized. So I basically took the ImageViewer example and implemented Runnable:

package net.ck.game.test;

import java.io.BufferedReader;
import java.util.ArrayList;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ImageTest extends Application implements Runnable {

    protected static final Logger logger = (Logger) LogManager.getLogger(ImageTest.class);
    BufferedReader x;
    @Override public void start(@SuppressWarnings("exports") Stage stage) {

        logger.error(Thread.currentThread().getName() + ", executing run() method!");

        Image standardImage = new Image("file:graphics/image1.png");

        logger.error("image height image1: "+ standardImage.getHeight());
        logger.error("image width image1:" + standardImage.getWidth());

        Image movingImage = new Image("file:graphics/image2.png");
        ArrayList<Image> images = new ArrayList<Image>();
        images.add(movingImage);
        images.add(standardImage);
        ImageView iv1 = new ImageView();
        iv1.setImage(standardImage);
        ImageView iv2 = new ImageView();
        iv2.setImage(movingImage);
        Group root = new Group();
        Scene scene = new Scene(root);
        scene.setFill(Color.BLACK);
        HBox box = new HBox();
        box.getChildren().add(iv1);
        box.getChildren().add(iv2);
        root.getChildren().add(box);

        stage.setTitle("ImageView");
        stage.setWidth(415);
        stage.setHeight(200);
        stage.setScene(scene); 
        stage.sizeToScene(); 
        stage.show(); 
    }

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

    @Override
    public void run() 
    {
        Application.launch(ImageTest.class);        
    }
}

When I run this as its own application, this works fine and displays the two images I want it to display.

When I run it like this in the "game" constructor:

public class Game {
private boolean animated;

    public boolean isAnimated() {
        return animated;
    }

    public void setAnimated(boolean animated) {
        this.animated = animated;
    }

public Game() {
        setAnimated(true);


        if (isAnimated() ==  true)
        {
            ImageTest imageTest = new ImageTest();
            new Thread(imageTest).start();
        }
}

There are no errors, ImageTest runs in its own thread, the application window opens, but it is empty. I do not understand this at all, why is that? Can someone pleaese shed some light on this?

UPDATE: I had different working contexts by accident. Fixing this fixed the problem.

Snorik
  • 201
  • 3
  • 15
  • 1
    Are you running this from different working directories? Usually it's best to include resources like these as resources in the classpath and look them up via `SomeClass.class.getResource(resourcePath).toExternalForm()`. If that isn't possible for some reason, it's usually best to leave it up to `File` to do the conversion to URI. Furthermore note that the application instance you create is different to the one actually launched; `Application.launch` uses reflection to create a Application instance based on the stacktrace. – fabian Mar 28 '20 at 09:06
  • 1
    BTW: launching the gui is also possible without using an `Application`, but it changes the lifecycle a bit ( https://openjfx.io/javadoc/13/javafx.graphics/javafx/application/Platform.html#startup(java.lang.Runnable) ): you need to make sure the application javafx toolkit shuts down at the time you want it to. Also note that `Application.launch` does not terminate until the gui is shut down, but that doesn't mean the thread you launch it from becomes the JavaFX application thread. – fabian Mar 28 '20 at 09:10
  • @fabian: thank you, I was being stupid. I had explicitly set a working directory for the ImageTest run configuration in Eclipse and forgot to do that for the main Run Configuration and also for the JUnit configuration. Is that actually a good way to do that or would you recommend a different way? – Snorik Mar 28 '20 at 11:03

1 Answers1

0

UPDATE: I had different working contexts by accident. Fixing this fixed the problem.

Snorik
  • 201
  • 3
  • 15