• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: CreateFont
title: Fontをファイルから取得
tags: [Font, JTextArea]
author: aterai
pubdate: 2005-03-07T02:07:13+09:00
description: 独自フォント(mona.ttf)をファイルから読み込み、ASCII artを表示します。
description: TrueTypeフォントをファイルから読み込み、ASCII artを表示します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTKOUJYB7I/AAAAAAAAAVo/K2rl3dXD4ic/s800/CreateFont.png
---
* 概要 [#summary]
独自フォント(`mona.ttf`)をファイルから読み込み、`ASCII art`を表示します。
`TrueType`フォントをファイルから読み込み、`ASCII art`を表示します。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTKOUJYB7I/AAAAAAAAAVo/K2rl3dXD4ic/s800/CreateFont.png)

* サンプルコード [#sourcecode]
#code(link){{
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;
}
//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 ioex) {
//        ioex.printStackTrace();
//      }
//    }
//  }
//  return font;
//}
}}

* 解説 [#explanation]
上記のサンプルでは、`Font.createFont(...)`メソッドでフォントを作成しています。
上記のサンプルでは、`Font.createFont(...)`メソッドで`.ttf`ファイルからフォントを作成しています。

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Font.html#createFont-int-java.io.File- Font (Java Platform SE 8)]
- [http://monafont.sourceforge.net/index.html モナーフォント]
- [https://community.oracle.com/thread/1483177 Swing (Archive) - Loading TYPE1 fonts with JRE 1.5.0]

* コメント [#comment]
#comment
- `JDK 1.5.0_01`でずれる? -- &user(aterai); &new{2005-03-07 11:19:11 (月)};
-- 応急処置済み -- &user(aterai); &new{2005-03-07 11:32:44 (月)};
- メモ: [https://bugs.openjdk.java.net/browse/JDK-6313541 Bug ID: 6313541 Fonts loaded with createFont cannot be converted into FontUIResource] -- &user(aterai); &new{2006-05-25 (木) 23:34:18};

#comment