Swing/FontSilhouette のバックアップソース(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/FontSilhouette へ行く。
--- title: Fontのアウトラインから輪郭を取得する tags: [Font, PathIterator, Icon, AffineTransform, Shape, JLabel] author: aterai pubdate: 2013-09-16T00:01:42+09:00 description: Fontから取得した字形の輪郭を抽出し、縁取りや内部の塗り潰しなどを行います。 --- * 概要 [#g07a2f18] `Font`から取得した字形の輪郭を抽出し、縁取りや内部の塗り潰しなどを行います。このサンプルは、[http://stackoverflow.com/questions/18686199/fill-unicode-characters-in-labels java - 'Fill' Unicode characters in labels - Stack Overflow]に投稿されているコードを参考にしています。 #download(https://lh5.googleusercontent.com/-kzMG9iEHFz4/UjWgNdHCh1I/AAAAAAAAB2A/0gpKBcNqz44/s800/FontSilhouette.png) * サンプルコード [#u80cc8b3] #code(link){{ public static Area getOuterShape(Shape shape) { Area area = new Area(); double[] coords = new double[6]; PathIterator pi = shape.getPathIterator(null); Path2D.Double path = null; while(!pi.isDone()) { int pathSegmentType = pi.currentSegment(coords); if(pathSegmentType == PathIterator.SEG_MOVETO) { if(area.isEmpty() || !area.contains(coords[0], coords[1])) { path = new Path2D.Double(); path.moveTo(coords[0], coords[1]); } }else if(path==null) { pi.next(); continue; }else if(pathSegmentType == PathIterator.SEG_LINETO) { path.lineTo(coords[0], coords[1]); }else if(pathSegmentType == PathIterator.SEG_QUADTO) { path.quadTo(coords[0], coords[1], coords[2], coords[3]); }else if(pathSegmentType == PathIterator.SEG_CUBICTO) { path.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); }else if(pathSegmentType == PathIterator.SEG_CLOSE) { path.closePath(); area.add(new Area(path)); path = null; }else{ System.err.println("Unexpected value! " + pathSegmentType); } pi.next(); } return area; } }} * 解説 [#k5b39527] 上記のサンプルの下二行は、チェスの駒の字形から輪郭を取得し、それを使って縁取り、内部の塗り潰しを行う`Icon`を`JLabel`に配置して表示しています。 字形(`Shape`)の輪郭は、`Shape#getPathIterator(...)`で字形からパスを取得し、開始点が一番外側にあるパスの集合を`Path2D`に変換、`Area`に追加することで作成しています。 * 参考リンク [#l4dba66c] - [http://stackoverflow.com/questions/18686199/fill-unicode-characters-in-labels java - 'Fill' Unicode characters in labels - Stack Overflow] * コメント [#f643624a] #comment - `src.zip`などがダウンロードできない状態(上げ忘れ?)だったのを修正。 -- &user(aterai); &new{2013-12-06 (金) 16:37:48}; - [http://blog.livedoor.jp/kinisoku/archives/4204798.html 漢字の線に囲まれた部分だけを塗りつぶした画像で何の四字熟語か当てるスレ:キニ速] のような問題を作成するツールを作ってみた。アンチエイリアスのせいでフチがボケてしまう。 -- &user(aterai); &new{2014-10-16 (金) 19:00:48}; #ref(https://lh5.googleusercontent.com/-sr96UEeFY_Y/VD-Wf1hu50I/AAAAAAAACQE/tWLZYYV0Y8o/s800/FontSilhouette2.png) #gist(7f9a0abb8fd17e62390a) #comment