代码之家  ›  专栏  ›  技术社区  ›  Zephyr

如何使用FontAwesomeIconView对象作为阶段的图标?

  •  3
  • Zephyr  · 技术社区  · 7 年前

    我正在使用 FontAwesomeFX 在我的应用程序中有很多图标。我想用他们作为我的图标 Stage

    自从 FontAwesomeIconView 延伸 GlyphIcon 延伸到 Text Image 直接。

    有没有办法创建一个可用的 对象

    snapshot ,但我不熟悉这种方法,最后在我的舞台上出现了标准的“空”图标。

    以下是我迄今为止的MCVE:

    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();
        }
    }
    

    结果是:

    screenshot


    是否可以将节点快照用作阶段图标?或者有没有更好的方法来转换 FontAwesomeIconView公司

    编辑:


    塞德里克的:

        // 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();
        }
    

    screenshot


    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);
    

    screenshot


    产生不同的显示图标)。

    我现在不明白的是,为什么这是它为我而不是他们制作的图标。。。

    我正在运行Windows7和JDK1.8.0¡。

    2 回复  |  直到 7 年前
        1
  •  2
  •   JKostikiadis    7 年前

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

    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);
    

    Snapshot Image can't be used as stage icon

    编辑 :

    出于某种原因,8.15版的FontawesomeFx要求在场景中添加FontAwesomeIconView以初始化其内容,而在最新版本的FontawesomeFx上不需要这样做。所以,在我看来,你要么升级你的FontawesomeFx版本,要么在一个场景和之后添加FontAwesomeIconView来拍摄快照。这样地:

    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版本):

    enter image description here

        2
  •  3
  •   SedJ601    7 年前

    我刚想出来。目标是获得 InputStream . 我可以用 SwingFXUtils.fromFXImage(icon, null); ImageIO.write(bImage, "png", s); 得到一个 byte array . 我曾经 字节数组 得到一个 .

    import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
    import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.embed.swing.SwingFXUtils;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.SnapshotParameters;
    import javafx.scene.image.Image;
    import javafx.scene.image.WritableImage;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javax.imageio.ImageIO;
    
    public class FontAwesomeIconExample extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            try {
                // 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.AMAZON);
                WritableImage icon = iconView.snapshot(new SnapshotParameters(), null);
                BufferedImage bImage = SwingFXUtils.fromFXImage(icon, null);
                ByteArrayOutputStream s = new ByteArrayOutputStream();
                ImageIO.write(bImage, "png", s);
                InputStream is = new ByteArrayInputStream(s.toByteArray());
    
                primaryStage.getIcons().add(new Image(is, 16, 16, false, false));
    
                // Show the stage
                primaryStage.setWidth(300);
                primaryStage.setHeight(100);
                primaryStage.setScene(new Scene(root));
                primaryStage.setTitle("Sample");
                primaryStage.show();
            } catch (IOException ex) {
                Logger.getLogger(FontAwesomeIconExample.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    
    }
    

    enter image description here enter image description here

    这在javadoc中值得注意。

    图像应为同一图像的不同尺寸,并将选择最佳尺寸,如16x16、32x32。