package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import javax.swing.*;
import javax.swing.text.*;

public final class MainPanel extends JPanel {
    private final JTextPane textpane = new JTextPane();
    public MainPanel() {
        super(new BorderLayout());

        Font font = makeFont(getClass().getResource("mona.ttf"));
        //Document doc = makeDocument(getClass().getResource("bar.utf8.txt"), "UTF-8");
        if (Objects.nonNull(font)) {
            System.out.println(font.toString());
            textpane.setFont(font.deriveFont(10f));
            //textpane.setDocument(doc);
        }

        URL url = getClass().getResource("bar.utf8.txt");
        try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) {
             textpane.read(reader, "text");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        add(new JScrollPane(textpane));
        setPreferredSize(new Dimension(320, 240));
    }

    //         TreeMap fontMap = new TreeMap();
    //         fontMap.put(font.getFamily(), font);
    //         StyleContext sc = new StyleContext();
    //         Style style = sc.addStyle("Mona Style", null);
    //         StyleConstants.setFontFamily(style, font.getFamily());
    //         StyleConstants.setFontSize(style, 12);
    //         FontDocument doc = new FontDocument(sc);
    //         doc.setLogicalStyle(0, style);
    //         textpane.setDocument(doc);

//     private static Font makeFont(URL url) {
//         Font font = null;
//         InputStream is = null;
//         try {
//             is = url.openStream();
//             font = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(12f);
//             is.close();
//         } catch (IOException ioe) {
//             ioe.printStackTrace();
//         } catch (FontFormatException ffe) {
//             ffe.printStackTrace();
//         } finally {
//             if (is != null) {
//                 try {
//                     is.close();
//                 } catch (IOException ex) {
//                     ex.printStackTrace();
//                 }
//             }
//         }
//         return font;
//     }

    private static Font makeFont(URL url) {
        Font font = null;
        try (InputStream is = url.openStream()) {
            font = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(12f);
        } catch (IOException | FontFormatException ex) {
            ex.printStackTrace();
        }
        return font;
    }

//     private static Document makeDocument(URL url, String encoding) {
//         DefaultStyledDocument doc = new DefaultStyledDocument();
//         try (Reader reader = new InputStreamReader(url.openStream(), encoding);
//              Scanner scanner = new Scanner(reader)) {
//             while (scanner.hasNextLine()) {
//                 doc.insertString(doc.getLength(), String.format("%s%n", scanner.nextLine()), null);
//             }
// //             char[] buff = new char[4096];
// //             int nch;
// //             while ((nch = reader.read(buff, 0, buff.length)) != -1) {
// //                 doc.insertString(doc.getLength(), new String(buff, 0, nch), null);
// //             }
//             //reader.close();
//         } catch (IOException | BadLocationException ex) {
//             ex.printStackTrace();
//         }
//         return doc;
//     }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

@@ -206,22 +206,23 @@
 a.util.O
-bjects
+ptional
 ;%0Aimport
@@ -435,28 +435,16 @@
 %0A       
- Font font =
  makeFon
@@ -484,132 +484,26 @@
 f%22))
-;%0A        //Document doc = makeDocument(getClass().getResource(%22bar.utf8.txt%22), %22UTF-8%22);%0A        if (Objects.nonNull
+.ifPresent
 (font
-))
+ -%3E
  %7B%0A 
@@ -639,24 +639,24 @@
 ument(doc);%0A
-
         %7D%0A%0A 
@@ -644,32 +644,34 @@
 (doc);%0A        %7D
+);
 %0A%0A        URL ur
@@ -2252,36 +2252,46 @@
  private static 
+Optional%3C
 Font
+%3E
  makeFont(URL ur
@@ -2299,34 +2299,8 @@
 ) %7B%0A
-        Font font = null;%0A
     
@@ -2349,39 +2349,51 @@
 ) %7B%0A            
-font = 
+return Optional.of(
 Font.createFont(
@@ -2431,16 +2431,17 @@
 ont(12f)
+)
 ;%0A      
@@ -2550,20 +2550,32 @@
  return 
-font
+Optional.empty()
 ;%0A    %7D%0A

use Optional#ifPresent(...) instead of if (Objects.nonNull(o)) {...}