Swing/FontSilhouette のバックアップ(No.11)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/FontSilhouette へ行く。
- 1 (2013-12-06 (金) 16:37:48)
- 2 (2014-10-16 (木) 19:00:59)
- 3 (2014-10-16 (木) 20:05:26)
- 4 (2014-12-01 (月) 17:17:12)
- 5 (2016-01-16 (土) 00:25:11)
- 6 (2017-03-30 (木) 14:16:12)
- 7 (2017-04-07 (金) 13:51:51)
- 8 (2018-03-14 (水) 17:17:54)
- 9 (2020-03-19 (木) 19:57:02)
- 10 (2021-09-26 (日) 09:46:04)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
- category: swing folder: FontSilhouette title: Fontのアウトラインから輪郭を取得する tags: [Font, PathIterator, Icon, AffineTransform, Shape, JLabel] author: aterai pubdate: 2013-09-16T00:01:42+09:00 description: Fontから取得した字形の輪郭を抽出し、縁取りや内部の塗り潰しなどを行います。 image:
概要
Font
から取得した字形の輪郭を抽出し、縁取りや内部の塗り潰しなどを行います。このサンプルは、java - 'Fill' Unicode characters in labels - Stack Overflowに投稿されているコードを参考にしています。
Screenshot
Advertisement
サンプルコード
private static Area getOuterShape(Shape shape) {
Area area = new Area();
Path2D path = new Path2D.Double();
PathIterator pi = shape.getPathIterator(null);
double[] coords = new double[6];
while (!pi.isDone()) {
int pathSegmentType = pi.currentSegment(coords);
switch (pathSegmentType) {
case PathIterator.SEG_MOVETO:
path.moveTo(coords[0], coords[1]);
break;
case PathIterator.SEG_LINETO:
path.lineTo(coords[0], coords[1]);
break;
case PathIterator.SEG_QUADTO:
path.quadTo(coords[0], coords[1], coords[2], coords[3]);
break;
case PathIterator.SEG_CUBICTO:
path.curveTo(coords[0], coords[1], coords[2],
coords[3], coords[4], coords[5]);
break;
case PathIterator.SEG_CLOSE:
path.closePath();
area.add(new Area(path));
path.reset();
break;
default:
System.err.println("Unexpected value! " + pathSegmentType);
break;
}
pi.next();
}
return area;
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、チェスの駒の字形から輪郭を取得し、それを使って縁取り、内部の塗り潰しを行うIcon
をJLabel
に配置して表示しています。
字形(Shape
)の輪郭はShape#getPathIterator(...)
でPathIterator
を取得し、パスの集合をPath2D
に変換、Area
に追加することで作成しています。