JavaFX displaying multiple images in a grid

enter image description hereI am trying to make a simple program view 4 images in the grid area. I get one there without problems, but as soon as I try to add a second, I run into some problems. Here is my code:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        GridPane gridPane = new GridPane();

        gridPane.add(new ImageView(new Image ("http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif")), 1,1);
        gridPane.add(new ImageView(new Image ("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif")), 2,2);

        Scene scene = new Scene(gridPane, 1000, 500);
        primaryStage.setTitle("Flags");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

I think this may be a problem with the row and column after the image, but I tried several things and was not successful. Any help is appreciated. Thanks

+4
source share
2 answers

flakes are right, but this is not the image itself, it is its accessibility. If I open the URL of the Chinese flag in the browser, everything will be fine, but try the modified code:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Image imgUsa = new Image ("http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif");
        Image imgChina = new Image ("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif");

        ImageView ivUsa = new ImageView(imgUsa);
        ImageView ivChina = new ImageView(imgChina);


        TextField errorText = new TextField();
        if (imgChina.isError()) {
            errorText.setText(imgChina.getException().getMessage());
        }

        VBox root = new VBox(ivUsa, ivChina, errorText);

        Scene scene = new Scene(root, 1000, 500);
        primaryStage.setTitle("Flags");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

... what I get in TextField:

HTTP: 403 URL: http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif

HTTP 403 -

, , .

+2

. gif. , , , . " " gif url - . , -, .

:

, :

gridPane.add( new ImageView(new Image(new URL( "http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif").openStream())), 0,
                0);
gridPane.add(new ImageView(new Image(new URL("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif").openStream())),0, 1);

, gif java.io.IOException HTTP: 403 URL...

, , "" gif HTTP-, . "", Https, .

403 Java, -?

:

URL url = new URL("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

conn.connect();

GridPane gridPane = new GridPane();

gridPane.add(new ImageView(new Image(new URL("http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif").openStream())), 0,0);
gridPane.add(new ImageView(new Image(conn.getInputStream())), 0, 1);

@flakes @tomorrow: P

0

Source: https://habr.com/ru/post/1686171/


All Articles