0

I'm going through the book "Quick Start Guide to JavaFX" I'm on Chapter 7 and for some reason my image won't load

Here is my code:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter7;


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;

/**
 *
 * @author svetlana
 */
public class Chapter7 extends Application {

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

   @Override
    public void start(Stage primaryStage) {

        Image butterfly = new Image("http://pngimg.com/uploads/butterfly/butterfly_PNG1024.png");
        ImageView imageView = new ImageView();

        imageView.setImage(butterfly);
        imageView.setFitWidth(300);
        imageView.setPreserveRatio(true);


        StackPane root = new StackPane();
        root.getChildren().add(imageView);

        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

These are the errors that NetBeans provides me with

    ant -f /home/svetlana/NetBeansProjects/Chapter7 jfxsa-run
init:
Deleting: /home/svetlana/NetBeansProjects/Chapter7/build/built-jar.properties
deps-jar:
Updating property file: /home/svetlana/NetBeansProjects/Chapter7/build/built-jar.properties
Compiling 1 source file to /home/svetlana/NetBeansProjects/Chapter7/build/classes
compile:
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from /usr/lib/jvm/java-8-oracle/jre/../lib/ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from /usr/lib/jvm/java-8-oracle/jre/../lib/ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 12 files to /home/svetlana/NetBeansProjects/Chapter7/dist/run1372567593
jfx-project-run:
Executing /home/svetlana/NetBeansProjects/Chapter7/dist/run1372567593/Chapter7.jar using platform /usr/lib/jvm/java-8-oracle/jre/bin/java
Deleting directory /home/svetlana/NetBeansProjects/Chapter7/dist/run1372567593
jfxsa-run:
BUILD SUCCESSFUL (total time: 6 seconds)

I'm able to visit the picture if I can go directly from the browser.

  • Person had this same problem the other day. Go [here](http://stackoverflow.com/questions/43285033/how-to-load-images-from-url-in-javafx-if-recieving-http-error-403). – SedJ601 Apr 11 '17 at 20:14
  • The server the image is hosted on will not allow the connection without extra work. – SedJ601 Apr 11 '17 at 20:15
  • 1
    Aside: there are no errors in the output. Just some logging info and a warning. – James_D Apr 11 '17 at 20:16

1 Answers1

0

The reason why here.

@Override
public void start(Stage primaryStage)
{
    try {
        Image butterfly = createImage("http://pngimg.com/uploads/butterfly/butterfly_PNG1024.png");
        ImageView imageView = new ImageView();

        imageView.setImage(butterfly);
        imageView.setFitWidth(300);
        imageView.setPreserveRatio(true);


        StackPane root = new StackPane();
        root.getChildren().add(imageView);

        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
    catch (IOException ex) {
        Logger.getLogger(JavaFXApplication72.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

Image createImage(String url) throws IOException 
{
    URLConnection conn = new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", "Wget/1.13.4 (linux-gnu)");

    try (InputStream stream = conn.getInputStream()) {
        return new Image(stream);
    }
}
Community
  • 1
  • 1
SedJ601
  • 12,173
  • 3
  • 41
  • 59