Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal graphics not initialized yet: javafx

Tags:

java

image

javafx

I'm trying to write a javaFx application whit multiple images inside a window.
The short story is that I have an enum class named Candy and each candy has some properties and a path to the image file representing it.
In the constructor of my javafx.application class (Table) I want to fill an array list with those images, so I wrote this so far:

public class Table extends Application {

    ArrayList<Image> images;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("CandyFx");
        primaryStage.show();
    }

    public Table() {
        images = new ArrayList<Image>();
        for (Candy candy : Candy.values()) {
            File file = new File (candy.getImagePath());
            Image image = new Image(file.toURI().toString());
            images.add(image);
        }
    }
}

Now every time I want to create an instance of Table class, the application throws a java.lang.RuntimeException: Internal graphics not initialized yet.
How can I initial graphics which it seems I did not?

like image 302
AliLotfi Avatar asked Jan 08 '15 11:01

AliLotfi


People also ask

Does JavaFX support PNG?

JavaFX supports the image formats like Bmp, Gif, Jpeg, Png.

How do I add an image to JavaFX GUI?

Create a FileInputStream representing the image you want to load. Instantiate the Image class bypassing the input stream object created above, as a parameter to its constructor. Instantiate the ImageView class. Set the image to it by passing above the image object as a parameter to the setImage() method.

What is primary stage in JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.


2 Answers

First of all if you are using linux ,GTK 2.18 is required to run JavaFX .try to install

libswt-gtk-3-java

This exception will thrown whenever your screen is null .Try to create your images inside start method. Just before the primaryStage.show();.

Take a look at this link too

http://cr.openjdk.java.net/~vadim/RT-33475/webrev.00/modules/graphics/src/main/java/com/sun/glass/ui/Screen.java.html

like image 129
Saeed Masoumi Avatar answered Oct 03 '22 05:10

Saeed Masoumi


I have no idea how it exactly works, but when we first create a JFXPanel in our start we don't get the errors anymore.

JFXPanel jfxPanel = new JFXPanel();
like image 45
Lingkar Avatar answered Oct 03 '22 06:10

Lingkar