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?
JavaFX supports the image formats like Bmp, Gif, Jpeg, Png.
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.
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.
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With