JavaFX 2.2 support for .ico?

I am developing an application that should have a custom icon. The provided icon is the same for all sizes (256x256, 48x48, 32x32), except for 16x16, where the icon is simplified.

I was thinking of the .ico format (where I can store all the differents icons and let the OS show the best), but it does not seem to be supported by javafx.scene.image (I did not find confirmation about this).

This is how I set the icon

stage.getIcons().add(new Image(getClass().getResourceAsStream("/path/to/icon.ico"))); 

In this case, the icon is never displayed. If I convert this icon to a .png image, it works, but it always displays the same icon (even in 16x16).

Is there a way in JavaFX 2.2 to display .ico (even in a hacked way) or do I need to use other image formats?

Update

I split my .ico into several pngs (one for each size) and then downloaded them one at a time.

 stage.getIcons().add(new Image(getClass().getResourceAsStream("/path/to/icon_16x16.png"))); stage.getIcons().add(new Image(getClass().getResourceAsStream("/path/to/icon_256x256.png"))); 

256x256 and 16x16 are two different images, but 16x16 is never displayed in the upper left corner of the application (despite the fact that this is the closest size).

+6
source share
3 answers

Feature Request

See the corresponding function request:

The feature is not currently set aside for release, but you can vote or comment on it if you want.

Download ico files using third-party libraries

At the same time, you can use various utilities to create icons in the java.awt.image.BufferedImage format, and then convert them to JavaFX using SwingFXUtils . haraldK provides a sample of this approach in its answer. Another example is the fav icon fetcher for the willow browser , which uses image4j , although the twelve monkeys haraldK are known to be the best library to use. Another alternative is to port the source of one of the awt-based icon libraries to JavaFX using WritableImage .

Tip

Your reasoning that you do not use png because it always displays 16x16 is a bit strange for me, because the documentation for stage.getIcons() returns a list of images that you can add (you are not limited to adding a single icon). From javadoc:

Gets images of icons that will be used in window decorations and when minimizing them. Images must be of different sizes of the same image, and the best size will be selected, for example. 16x16, 32.32.

Additional question

What to do if, depending on the size, the image is not always the same

Perhaps this is normal to provide different images. If the system interpolates the provided images to create size icons that are not provided, this may cause a problem, but I think it is unlikely that the system would do this. “Images must be of different sizes of the same image” are more oriented than the actual rule. If you need different images of different sizes, try putting several png images for this and see what happens.

+7
source

I do not think JavaFX directly supports the ICO format. I am sure that the list contains only JPEG, GIF and PNG, but I have not yet found an official source that confirms this.

However, you can use my ICO plugin for ImageIO to read the ICO file and convert the image to FX Image using SwingFXUtils.toFXImage(bufferedImage, null) .

Please note that the reader simply returns the icons in the order in which they are in the ICO file, so ImageIO.read(...) will not give you the desired icon (it will read only the first). Instead, you need to read each icon, convert and add all the icons to the scene. And FX will choose the right size for you. :-)

Sort of:

 ImageInputStream stream = ImageIO.createImageInputStream(getClass().getResourceAsStream("/path/to/icon.ico")); ImageReader reader = ImageIO.getImageReaders(stream).next(); reader.setInput(stream); int count = reader.getNumImages(true); List<Image> fxImages = new ArrayList<>(count); for (int i = 0; i < count; i++) { BufferedImage bufferedImage = reader.read(i, null); Image fxImage = SwingFXUtils.toFXImage(bufferedImage, null); fxImages.add(fxImage); } stream.close(); // Remember to close/dispose in a finally block reader.dispose(); // ... stage.getIcons().addAll(fxImages); 
+5
source

Use image4j :

 ArrayList<Image> lImages = new ArrayList<>(); ICODecoder.read(Global.FILE_ICON).stream().forEach((lBufferedImage) -> lImages.add(SwingFXUtils.toFXImage(lBufferedImage, null))); this.getStage().getIcons().addAll(lImages); 
0
source

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


All Articles