While trying to load images from URLs there was some strange behavior in the image size being inconsistent the images only have full size if loaded in background and then waiting for the progress. To analyze the situation i created the JUnit test below. The expected size in background mode is not consistent with the size of the loaded images.
Why does the image size depend on whether the image is loaded in background or not?
@Test
public void testImage() throws InterruptedException {
// do not exit on close of last window
// https://stackoverflow.com/a/10217157/1497139
Platform.setImplicitExit(false);
/// https://stackoverflow.com/a/38883432/1497139
// http://www.programcreek.com/java-api-examples/index.php?api=com.sun.javafx.application.PlatformImpl
com.sun.javafx.application.PlatformImpl.startup(() -> {
});
String urls[] = {
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Flag_of_Germany.svg/320px-Flag_of_Germany.svg.png",
"https://via.placeholder.com/50x50" };
int expectedWidth[][] = { { 160, 25 }, { 320, 50 } };
int expectedHeight[][] = { { 96, 251 }, { 192, 50 } };
boolean backgrounds[] = { false, true };
int i = 0;
for (boolean background : backgrounds) {
int j=0;
for (String url : urls) {
Image image = new Image(url, background);
assertNotNull(image);
if (background)
while (image.getProgress() < 1)
Thread.sleep(40);
assertEquals("w " + i+ ","+j, expectedWidth[i][j], image.getWidth(), 0.1);
assertEquals("h " + i+ ","+j, expectedHeight[i][j], image.getHeight(), 0.1);
j++;
}
i++;
}
}