Internal graphics not initialized yet: javafx - java

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?

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();

Actually, JavaFX Stage class is the top level JavaFX container which should be constructed and modified on the JavaFX Application Thread.
Here you are using Stage class but have not embed JavaFx content into Swing application which may show you a 'java.lang.RuntimeException with message Internal graphics not initialized yet.
Here you may use-
JFXPanel jfxPanel = new JFXPanel();
Or you may use in this way too.
JFrame frame = new JFrame("Java Swing And JavaFX");
JFXPanel jfxPanel = new JFXPanel();
frame.add(jfxPanel);
JFXPanel is a component to embed JavaFX content into Swing applications. The content to be displayed is specified with the setScene(javafx.scene.Scene) method that accepts an instance of JavaFX Scene. After the scene is assigned, it gets repainted automatically. All the input and focus events are forwarded to the scene transparently to the developer.
References:
JFXPanel: https://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/JFXPanel.html
Stage: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html

Related

JavaFX Cannot Find Image

just started a new project and am already running in to some issues. I am trying to display an image with javafx but the compiler is complaining that the provided location is invalid. Here is all of my code so far (apart from some imports):
public class main extends Application{
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception{
Stage stage = primaryStage;
String version = "alpha";
stage.setTitle("Space Invaders v. " + version);
VBox vbox = new VBox();
Image pic = new Image("Art\\whiteMonster.jpg");
ImageView image = new ImageView(pic);
vbox.getChildren().add(image);
Rectangle rect = new Rectangle(20,0,200,200);
rect.setFill(Color.BLACK);
vbox.getChildren().add(rect);
//Creating the scene.
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
}
If I change the file path to the images url where I found it in google image search it displays the black rectangle added beneath it (just to test if everything works) but not the image. In the current configuration however it simply throws an error saying the URL is either invalid or that the resource was not found. What makes me think that this should work is this example here: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html
Thanks for any help.
PS. I've been looking at other similar questions here but haven't found anything that helped me and if you feel like you need any other information give me a shout.
-----EDIT-----
Project Structure, Code and File Manager, all images are placed directly in the "Art" folder.
I hope that is all you need.
Try putting the image inside the folder .Drag the image to the "src" and use Image pic = new Image("whiteMonster.jpg");. It should be displayed under the .java files but a little bit in the left.
The solution for me was to Close Project and open the Javafx Project
again
as shown here
This refreshes the images URL for Javafx.
NOTE: I am using Intellij IDEA Java version 16 with Javafx.
Error was: Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found

JavaFx FileChooser without Stage

I am in the process of learnig JavaFx, and I encountered a problem. I was trying to use FileChooser from JavaFx the way I was used to working with JFileChooser from Swing-in the main() method. However I found out that I need a Window object. I've tried searching for a workaround, but I found none. I've also tried null (as you do in JFileChooser) and new Stage(), so those are off the table. I was trying to mimic the JFileChooser.showOpenDialog(). Is there any reasonable way to make it work?
The main method is not executed on the FX Application Thread, so you can't show a FileChooser from it. (You can't really do that in Swing either, unless you are using SwingUtilities.invokeLater(...).)
In JavaFX, the responsibility for starting the application is in the start() method, which is executed on the FX Application Thread. (In many runtime environments, you don't even need a main method in JavaFX applications.)
Just show the file chooser from the start method, where you have access to the primaryStage (or can just pass null, if you like):
public class MyApp extends Application {
public void start(Stage primaryStage) {
FileChooser configFileChooser = new FileChooser();
File configFile = configFileChooser.showOpenDialog(primaryStage);
// ... parse file and create UI, etc...
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Simple Java GUI, cards not appearing

import javax.swing.*;
public class SlideShow {
JFrame slide = new JFrame("Slide Show");
public SlideShow(){
slide.setSize(300,400);
slide.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
slide.setVisible(true);
slide.setLocationRelativeTo(null);
JPanel panel = new JPanel();
JLabel label = new JLabel(new ImageIcon("Images/picture1"));
panel.add(label);
slide.add(panel);
}
public static void main(String[] args){
SlideShow slide = new SlideShow();
}
}
I have to create a simple Java GUI that displays some cards. First, I just wanted to test it by displaying one card. For some reason I can't seem to figure out why nothing is being displayed.
You haven't actually used a proper file name "Images/picture1". Should be something like "Images/picture1.png" with the file format
Also image files, generally should be read from the class path, if you plan on having them embedded to the program. To do so, you will first need to put the file in the class path. With most IDE build configurations it's as simple as placing the image in the src. So
ProjectRoot
src
images
picture1.png
Then you would read it like
new ImageIcon(getClass().getResource("/images/picture1.png"));
A better approach would be to use ImageIO.read(). If the file path is incorrect, it will throw an exception, so you know where you're going wrong
Image image = ImageIO.read(getClass().getResource("/images/picture1.png"));
ImageIcon icon = new ImageIcon(image);
You will need to put it in the try/catch block
Also do what codeNinja said about the setVisible() after adding component. Also preferably pack() the frame, instead of setSize()
You need to set the Frame visible after you add all necessary components to it. Move slide.setVisible(true); Down to the bottom of the constructor like this:
...
slide.add(panel);
slide.setVisible(true);
Alternatively you can add slide.revalidate(); at the bottom of your constructor.

Java SplashScreen in Java Web Start

I am trying to move my Java Swing project to Java Web Start
and I have problem with the splash screen. This application uses Maven.
When I load my application via the command-line or by an external exe,
it displays the splash screen correctly.
final SplashScreen splash = SplashScreen.getSplashScreen();
When I run application via Java Web Start, it always return null.
Yes, I know about splash screen section in JNLP file.
<icon kind="splash" href="splash.png"/>
But this will show splash screen before the application is loaded and not
when the application is running. In other words, it isn't a replacement for the --splash switch.
In the manifest file, I have:
SplashScreen-Image: (URL to resource, file in jar)
This works well only when I run jar file and not in Java Web Start.
Has anybody had this same problem and found any solution for this?
I need a splash screen because the application takes several seconds to start and nothing is displayed for user in this time.
To show a splash screen for JNLP clients, call the start() method passing the splash image path. To remove the splash screen, call the stop() method.
public class ShowSplash
{
private static JWindow splashFrame;
public void start(String splashImagePath) throws Exception
{
JLabel label;
ImageIcon image;
URL url;
splashFrame = new JWindow();
url = ShowSplash.class.getResource(splashImagePath);
image = new ImageIcon(url);
label = new JLabel(image);
splashFrame.add(label, BorderLayout.CENTER);
splashFrame.pack();
splashFrame.setLocationRelativeTo(null);
splashFrame.setVisible(true);
}
public void stop() throws Exception
{
splashFrame.dispose();
splashFrame = null;
}
}
The JWS based splash screen uses totally different functionality (and a different loading philosophy) to the AWT based SplashScreen. The JWS splash is always a loose file that is referenced in the JNLP file. We cannot get access to draw on the JNLP splash.
Thanks for answers, but I have problem with replace SplashRenderer with JWindow.
Here is code:
splashFrame = new JWindow();
splashFrame.setBackground(Color.white);
JPanel splashPanel = new JPanel();
splashPanel.setLayout(new BorderLayout());
JLabel image = new JLabel(img);
splashPanel.add(image);
splashFrame.setContentPane(splashPanel);
splashFrame.pack();
splashFrame.setLocationRelativeTo(null);
splashFrame.setAlwaysOnTop(true);
splashPanel.paintImmediately(0, 0, splashPanel.getSize().width, splashPanel.getSize().height);
splashFrame.setSize(splashPanel.getSize().width,splashPanel.getSize().height);
splashFrame.setLocation(100, 100);
splashFrame.setVisible(true);
The Window is always in back, no metter if I set setAlwyasOnTop or if I set this methods
in background thread. SplashRender in this case always stays on top, but JWindow not.
Currently effect is - When I start APP the Window is not visible during start up, but it's shows when MainFrame appear.

export a JOGL applet and embedd into a html page

It is some time that I'm testing opengl with java and JOGL. I have a good result and I wan to publish it on the web, but I have a problem. I'm in eclipse, and I'm testing an Applet with JOGL.
EDIT: Thanks to Ricket answer it fixed this problem
first of all i have this run time error (but the program works correctly):
java.lang.IllegalArgumentException:
adding a window to a container at
EDIT: but it still doesn't work:
then I found this incredibly clear page
and I did what is said. I open html with the browser, the libs are downloaded, but it stops at Starting applet AppletHelloWorld, as that is the name I gave to my applet.
Maybe I am missing something like a main function or exporting the jar properly?
This is my main code:
public class AppletHelloWorld extends Applet
{
public static void main(String[] args)
{
JFrame fr=new JFrame();
fr.setBounds(0,0,1015,600);
fr.add(new AppletHelloWorld());
fr.setVisible(true);
}
public void init()
{
setLayout(null);
MyJOGLProject canvas = new MyJOGLProject(); //MyJOGLProject extends JFrame
canvas.run(); // this do setVisible(true)
} //....
Just as the error says, you're trying to add a window to a container. A JFrame is a window. You can't add a JFrame to anything, including a Container. I think perhaps you either don't know what a JFrame is, or don't know what a Container is.
Ideally you would have MyJOGLProject extend GLEventListener instead. Then your init function would make a new GLCanvas, add an instance of MyJOGLProject to it (via addGLEventListener), and then add the GLCanvas to your applet.
Alternatively, if you're okay with the applet popping up a JFrame, then simplify your init method:
public void init() {
setLayout(null);
MyJOGLProject canvas = new MyJOGLProject();
canvas.setVisible(true);
}
That should do it.
Use JApplet. I think that's the reason why it fails.
(Use Webstart with JNLP in NetBeans)

Categories