0

I've been trying for a while now, following various documentations but I just cannot get any images to show up on JavaFX.

Here is my code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
//stage and stuff
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//images
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class KingsGame extends Application {

    public static ImageView iv = new ImageView();
    Button btn = new Button();
    public static Image test = new Image("http://puu.sh/vOk8i/754c8fee68.png");

    @Override
    public void start(Stage primaryStage) {
        //stackpane
        StackPane root = new StackPane();
        root.getChildren().add(iv);
        //scene
        Scene scene = new Scene(root, 1280, 720);

        primaryStage.setTitle("test program lol");
        primaryStage.setScene(scene);
        primaryStage.show();
        //---actual game---
        drawMainMenu();
    }

    public void helloTest() {
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
    }

    public static void drawMainMenu() {
        iv.setImage(test);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Whenever I run the program, all I get is this: http://puu.sh/vOko6/b669cfc20b.png

The strange thing is, when I initially tested this, I used this (https://osu.ppy.sh/ss/8062698) image link as my test image and it somehow worked even though there wasn't even a .jpg or .png extension. It was a game screenshot and I simply just used the link the game gave me to test. When I switched it to another test link, it just broke.

How can I get ALL image links to work?

Pang
  • 9,564
  • 146
  • 81
  • 122
Enkrypton
  • 39
  • 1
  • 2
  • 15
  • You should remove all occurrences of `static` and initialize your Image in the `start` method. User interface objects should not be static, as they need to be created and managed in the UI thread. – VGR May 13 '17 at 03:23
  • i thought declaring all my images outside of a method would allow me to use it in multiple methods since i wanted an individual method in charge of printing each individual image. Ill try that and maybe a work around so i can still use separate methods per image. thanks. – Enkrypton May 13 '17 at 14:04

1 Answers1

1

It looks like an access problem.

If you print the exception returned with this instruction : System.err.println(test.getException());

you get this : java.io.IOException: Server returned HTTP response code: 403 for URL: http://puu.sh/vOk8i/754c8fee68.png

The site probably authorizes only the browser clients

  • if this is the issue, do you have any other image hosting suggestions that will work with JavaFX? – Enkrypton May 13 '17 at 15:34
  • @Enkrypton You can work around this by [creating an Image from an InputStream](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html#Image-java.io.InputStream-) instead of from a String. You can obtain an InputStream by creating a URL, opening a URLConnection, and calling something like `setRequestProperty("User-Agent", "Wget/1.16 (linux-gnu)")` on the connection before you call the connection’s [getInputStream()](https://docs.oracle.com/javase/8/docs/api/java/net/URLConnection.html#getInputStream--) method. – VGR May 13 '17 at 17:58