Swing/FlatteningPathIterator のバックアップ差分(No.8)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/FlatteningPathIterator へ行く。
- 1 (2013-07-08 (月) 01:45:24)
- 2 (2013-07-28 (日) 18:35:51)
- 3 (2013-12-03 (火) 15:35:17)
- 4 (2014-05-22 (木) 14:29:20)
- 5 (2014-09-17 (水) 02:27:33)
- 6 (2014-10-22 (水) 00:57:59)
- 7 (2015-11-18 (水) 18:18:03)
- 8 (2016-05-27 (金) 13:21:34)
- 9 (2017-04-07 (金) 13:51:51)
- 10 (2017-08-25 (金) 14:17:40)
- 11 (2018-09-13 (木) 15:01:33)
- 12 (2020-09-17 (木) 10:41:35)
- 13 (2022-05-20 (金) 07:36:21)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: FlatteningPathIteratorでShape上の点を取得する tags: [Shape, PathIterator, FlatteningPathIterator] author: aterai pubdate: 2013-07-08T01:45:24+09:00 description: FlatteningPathIteratorを使って平坦化されたShape上の座標点を取得、描画します。 --- * 概要 [#wfe285d1] `FlatteningPathIterator`を使って平坦化された`Shape`上の座標点を取得、描画します。 #download(https://lh4.googleusercontent.com/-3GsdpxueSG8/Udl1tOfisII/AAAAAAAABvc/SBOIf1ZPPUk/s800/FlatteningPathIterator.png) * サンプルコード [#zaf3d2bc] #code(link){{ PathIterator i = new FlatteningPathIterator(shape.getPathIterator(null), 1.0); float[] coords = new float[6]; while (!i.isDone()) { i.currentSegment(coords); g2.fillRect((int) (coords[0] - .5), (int) (coords[1] - .5), 2, 2); i.next(); } }} * 解説 [#he1dd0a1] - `Ellipse2D` -- `new Ellipse2D.Double`で作成した`Shape`を描画 - `Polygon x 2` -- 上記の楕円を`360/60`度ごとに曲線上の座標点を取得し、`Polygon`に変換して直線で描画 #code{{ private static Polygon convertEllipse2Polygon(Ellipse2D e) { Rectangle b = e.getBounds(); int r1 = b.width / 2, r2 = b.height / 2; int x0 = b.x + r1, y0 = b.y + r2; int v = 60; double a = 0.0, d = 2 * Math.PI / v; Polygon polygon = new Polygon(); for (int i = 0; i < v; i++) { polygon.addPoint((int) (r1 * Math.cos(a) + x0), (int) (r2 * Math.sin(a) + y0)); a += d; } return polygon; } }} - `FlatteningPathIterator` -- 上記の楕円から取得した`PathIterator`を`FlatteningPathIterator`で平坦化して曲線上の等間隔な座標点を取得し、`Polygon`に変換して直線で描画 -- 参考: [http://java-sl.com/tip_flatteningpathiterator_moving_shape.html FlatteningPathIterator and moving object along Shape path.] --- via: [http://stackoverflow.com/questions/17272912/converting-an-ellipse2d-to-polygon java - Converting an Ellipse2D to Polygon - Stack Overflow] -- `FlatteningPathIterator`を使う方法なら、どんな`Shape`でもその線上から等間隔な座標点を簡単に取得できる -- %%`FlatteningPathIterator`を使う方法なら、どんな`Shape`でもその線上から等間隔な座標点を簡単に取得できる%% * 参考リンク [#h538446e] - [http://docs.oracle.com/javase/jp/7/api/java/awt/geom/FlatteningPathIterator.html FlatteningPathIterator (Java Platform SE 7)] - [http://java-sl.com/tip_flatteningpathiterator_moving_shape.html FlatteningPathIterator and moving object along Shape path.] -- このサイトの例のように、`Shape`のパスに添ってアニメーションさせる場合などに便利です。 * コメント [#j840d88e] #comment #comment