2

We want to have a circle filled with a profile picture, loading the URL of the image.

We have tried loading the image with a URL on Windows, Linux, and MacOS. Turns out that the image processing works on Linux and MacOS, but doesn't work on Windows.

Circle circle = new Circle(25);
Image image = new Image("https://i.imgur.com/itElfV3.jpg");
circle.setFill(new ImagePattern(image));

We expect to have the circle with the profile picture inside it. But the result is a NullPointerException on Windows. The error we then get is:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Image must be non-null
    at com.sun.prism.paint.ImagePattern.<init>(ImagePattern.java:44)
    at com.sun.javafx.tk.quantum.QuantumToolkit.createImagePatternPaint(QuantumToolkit.java:905)
    at com.sun.javafx.tk.Toolkit.getPaint(Toolkit.java:657)
    at javafx.scene.paint.ImagePattern.acc_getPlatformPaint(ImagePattern.java:291)
    at javafx.scene.paint.Paint$1.getPlatformPaint(Paint.java:51)
    at javafx.scene.shape.Shape.updatePGShape(Shape.java:916)
    at javafx.scene.shape.Shape.impl_updatePeer(Shape.java:965)
    at javafx.scene.shape.Circle.impl_updatePeer(Circle.java:333)
    at javafx.scene.Node.impl_syncPeer(Node.java:503)
    at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2304)
    at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2313)
    at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2313)
    at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2313)
    at javafx.scene.Scene$ScenePulseListener.synchronizeSceneNodes(Scene.java:2280)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2419)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

The code does work on Windows if we load the image locally, but since we want users to be able to upload a new URL to their own profile pictures, we would like to use a link.

Ali Azim
  • 160
  • 1
  • 15
  • Possible duplicate of [*How to disable or bypass Hardware Graphics Acceleration(Prism) in JavaFX*](https://stackoverflow.com/q/18754803/230513). – trashgod Mar 25 '19 at 09:41
  • Works fine on Windows 7. It is pretty peculiar because if image was null from very beginning the NullPointerException should be thrown from javafx.scene.paint.ImagePattern(Image image), instead it is thrown from com.sun.prism.paint.ImagePattern(Image image, float x, float y, float width, float height, boolean proportional, boolean isMutable). My advice is to debug the image through the stack calls and find the place where it becomes null. – Przemek Krysztofiak Mar 25 '19 at 09:43

1 Answers1

1

Your code should be work, but i don't know why not work, instead you can use InputStream to get the image from URL, here the example

Image image = null;
    try{
        URL url = new URL("https://i.imgur.com/itElfV3.jpg");
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        image = new Image(inputStream);

    }catch (IOException e){
        e.printStackTrace();
    }

    Circle circle = new Circle(25);
    circle.setFill(new ImagePattern(image));
NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30
  • There's a shorthand for getting an `InputStream` for an `URL` this way: `InputStream inputStream = url.openStream();` – fabian Mar 25 '19 at 12:20