如何使用FontAwesomeIconView对象作为舞台的图标? [英] How to use a FontAwesomeIconView object as a Stage's icon?

查看:266
本文介绍了如何使用FontAwesomeIconView对象作为舞台的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个应用程序中对许多图标使用 FontAwesomeFX .我也想将它们用作我的Stage的图标.

I am using FontAwesomeFX for many icons throughout my application. I would like to use them as icons for my Stage as well.

由于FontAwesomeIconView扩展了GlyphIcon而又扩展了Text,所以我不能直接将其用作Image.

Since FontAwesomeIconView extends GlyphIcon which extends Text, I can not use it as an Image directly.

是否可以通过Text对象创建可用的Image?

Is there a way to create a usable Image from a Text object?

我尝试使用snapshot,但是我不熟悉该方法,并且最终在舞台上显示了标准的空"图标.

I have tried to use snapshot, but I am not familiar with that method and end up with the standard "empty" icon on my stage.

这是我到目前为止拥有的MCVE:

Here is the MCVE I have so far:

import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FontAwesomeIconExample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        // Simple Interface
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        // Convert the FontAwesomeIconView node to an Image
        FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.ID_CARD_ALT);
        WritableImage icon = iconView.snapshot(new SnapshotParameters(), null);

        primaryStage.getIcons().add(icon);

        // Show the stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(100);
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Sample");
        primaryStage.show();
    }
}

结果:

是否可以将节点快照用作阶段图标?还是为此目的转换FontAwesomeIconView的更好方法?

Is it possible to use a node snapshot as a stage icon? Or is there a better method of converting the FontAwesomeIconView for this purpose?

我已经尝试了Sedrick和JKostikiadis的方法,并且都为我提供了相似的结果.

I have tried both Sedrick's and JKostikiadis's methods and both give similar results for me.

Sedrick:

    // Convert the FontAwesomeIconView node to an Image
    FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.AMAZON);
    WritableImage icon = iconView.snapshot(new SnapshotParameters(), null);
    java.awt.image.BufferedImage bImage = SwingFXUtils.fromFXImage(icon, null);
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    try {
        javax.imageio.ImageIO.write(bImage, "png", s);
    } catch (IOException e) {
        e.printStackTrace();
    }

JKostikiadis:

    FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.AMAZON);
    WritableImage writableImg = iconView.snapshot(null, null);
    Image img = SwingFXUtils.toFXImage(SwingFXUtils.fromFXImage(writableImg, null), null);
    primaryStage.getIcons().add(img);

我相信他们基本上都完成了同一件事(Image的大小导致了不同的显示图标).

I believe they both accomplish essentially the same thing (with the sizing of the Image resulting in different displayed icons).

我现在不明白的是,为什么这是它为我而不是他们产生的图标...

What I do not understand now, is why this is the icon it is producing for me and not them...

我正在运行Windows 7和JDK 1.8.0_161.

I am running Windows 7 and JDK 1.8.0_161.

推荐答案

除了Sedrick的回答,另一个(较短的)解决方案可能是只创建一个Image(javafx程序包)并添加阶段图标:

In addition to Sedrick's answer, another (shorter) solution could be to just create an Image (javafx package) and add it stage icons :

FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.AMAZON);
WritableImage writableImg = iconView.snapshot(null, null);
Image img = SwingFXUtils.toFXImage(SwingFXUtils.fromFXImage(writableImg, null), null);
primaryStage.getIcons().add(img);

PS相对的帖子可以在这里找到:快照图像可以'不能用作舞台图标

P.S A relative post could be found here : Snapshot Image can't be used as stage icon

修改:

出于某种原因,FontawesomeFx的版本8.15要求将FontAwesomeIconView添加到场景中以初始化其内容,而对于最新版本的FontawesomeFx则不需要这样做.因此,我认为,您必须升级FontawesomeFx版本或在场景上添加FontAwesomeIconView,然后再拍摄快照.像这样:

For some reason, the version 8.15 of the FontawesomeFx require the FontAwesomeIconView to be added on a Scene in order to initialize its content, something that is not necessary to do on the newest version of FontawesomeFx. So, in my opinion, you have to either upgrade your FontawesomeFx version or to add the FontAwesomeIconView on a Scene and after to take the snapshot. Like this :

FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.ID_CARD_ALT);
Scene scene = new Scene(new StackPane(iconView));
WritableImage writableImg = iconView.snapshot(null, null);
Image img = SwingFXUtils.toFXImage(SwingFXUtils.fromFXImage(writableImg, null), null);

以下是结果(使用8.5版本):

Here is the result (using 8.5 version):

这篇关于如何使用FontAwesomeIconView对象作为舞台的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆