Swing/CreateFont のバックアップの現在との差分(No.11)
TITLE:Fontをファイルから取得
Fontをファイルから取得
編集者:Terai Atsuhiro~
作成日:2005-03-07
更新日:2023-06-16 (金) 08:17:14
概要
TrueType
フォントをファイルから読み込み、ASCII art
を表示します。
概要
独自フォント(mona.ttf)をファイルから読み込み、ASCII artを表示します。Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
private static Font makeFont(String resourcePath) {
Font font = null;
InputStream is = null;
try {
is = MainPanel.class.getResourceAsStream(resourcePath);
font = (Font.createFont(Font.TRUETYPE_FONT, is)).deriveFont(12.0f);
is.close();
}catch(IOException ioe) {
ioe.printStackTrace();
throw new InternalError(ioe.getMessage());
}catch(FontFormatException ffe) {
ffe.printStackTrace();
throw new InternalError(ffe.getMessage());
}finally{
if(is!=null) {
try{
is.close();
}catch(IOException ioex) {
ioex.printStackTrace();
throw new InternalError(ioex.getMessage());
}
}
}
return font;
}
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
Font makeFont(URL url) {
#spanend
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;
#spanadd
}
#spanend
#spanadd
// Font makeFont(URL url) {
#spanend
#spanadd
// Font font = null;
#spanend
#spanadd
// InputStream is = null;
#spanend
#spanadd
// try {
#spanend
#spanadd
// is = url.openStream();
#spanend
#spanadd
// font = (Font.createFont(Font.TRUETYPE_FONT, is)).deriveFont(12f);
#spanend
#spanadd
// is.close();
#spanend
#spanadd
// } catch (IOException ioe) {
#spanend
#spanadd
// ioe.printStackTrace();
#spanend
#spanadd
// } catch (FontFormatException ffe) {
#spanend
#spanadd
// ffe.printStackTrace();
#spanend
#spanadd
// } finally {
#spanend
#spanadd
// if (is != null) {
#spanend
#spanadd
// try {
#spanend
#spanadd
// is.close();
#spanend
#spanadd
// } catch (IOException ioex) {
#spanend
#spanadd
// ioex.printStackTrace();
#spanend
#spanadd
// }
#spanend
#spanadd
// }
#spanend
#spanadd
// }
#spanend
#spanadd
// return font;
#spanend
#spanadd
// }
#spanend
- &jnlp;
- &jar;
- &zip;
解説
Font.createFont()でフォントを作成しています。解説
上記のサンプルでは、Font.createFont(...)
メソッドで.ttf
ファイルからフォントを作成しています。
上記のサンプルで使用しているフォントはモナーフォントを、ASCIIアートは2chから引用しています。
参考リンク
参考リンク
コメント
-
JDK 1.5.0_01
でずれる? -- aterai- 応急処置済み -- aterai
- メモ: Bug ID: 6313541 Fonts loaded with createFont cannot be converted into FontUIResource -- aterai
コメント
- 1.5.0_01でずれる? -- terai
- 応急処置済み -- terai
- メモ:Bug ID: 6313541 Fonts loaded with createFont cannot be converted into FontUIResource -- terai