Swing/GeneralPath のバックアップソース(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/GeneralPath へ行く。
- 1 (2009-02-09 (月) 18:26:33)
- 2 (2009-02-16 (月) 20:31:04)
- 3 (2009-03-16 (月) 22:12:51)
- 4 (2009-03-17 (火) 11:48:07)
- 5 (2009-06-22 (月) 12:26:55)
- 6 (2009-07-24 (金) 17:24:08)
- 7 (2010-03-08 (月) 11:59:18)
- 8 (2010-03-08 (月) 13:40:46)
- 9 (2011-05-07 (土) 02:36:54)
- 10 (2013-01-12 (土) 22:04:57)
- 11 (2014-11-22 (土) 03:59:58)
- 12 (2015-01-09 (金) 14:20:39)
- 13 (2015-03-26 (木) 15:40:30)
- 14 (2016-01-16 (土) 00:25:31)
- 15 (2016-01-27 (水) 18:22:26)
- 16 (2016-06-03 (金) 13:37:31)
- 17 (2017-09-07 (木) 21:22:08)
- 18 (2018-11-20 (火) 15:52:55)
- 19 (2019-03-01 (金) 16:11:49)
- 20 (2020-12-16 (水) 11:03:09)
- 21 (2023-05-16 (火) 16:52:18)
- 22 (2023-12-25 (月) 23:01:26)
TITLE:GeneralPathなどで星型図形を作成する #navi(../) RIGHT:Posted by [[terai]] at 2009-02-09 *GeneralPathなどで星型図形を作成する [#x65498c5] GeneralPathなどを使って星型の図形をパネルに描画したり、アイコンを作成します。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#zd63173c] #code{{ class StarPanel1 extends JPanel{ public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; int w = getWidth(); int h = getHeight(); //<blockquote cite="%JAVA_HOME%/demo/jfc/Java2D/src/java2d/demos/Lines/Joins.java"> GeneralPath p = new GeneralPath(); p.moveTo(- w / 4.0f, - h / 12.0f); p.lineTo(+ w / 4.0f, - h / 12.0f); p.lineTo(- w / 6.0f, + h / 4.0f); p.lineTo(+ 0.0f, - h / 4.0f); p.lineTo(+ w / 6.0f, + h / 4.0f); p.closePath(); //</blockquote> g2.translate(w/2, h/2); g2.setColor(Color.YELLOW); g2.fill(p); g2.setColor(Color.BLACK); g2.draw(p); } } }} #code{{ class StarIcon2 implements Icon{ private static final int R1 = 20; private static final int R2 = 40; //double R1 = R2*Math.sin(Math.PI/10.0)/Math.cos(Math.PI/5.0); //=15.0; private static final int VC = 5; //16; private final AffineTransform at; private final Shape star; public StarIcon2() { double agl = 0.0; double add = 2*Math.PI/(VC*2); Path2D.Double p = new Path2D.Double(); p.moveTo(R2*1, R2*0); for(int i=0;i<VC*2-1;i++) { agl+=add; if(i%2==0) p.lineTo(R1*Math.cos(agl), R1*Math.sin(agl)); else p.lineTo(R2*Math.cos(agl), R2*Math.sin(agl)); } p.closePath(); at = AffineTransform.getRotateInstance(-Math.PI/2,R2,0); star = new Path2D.Double(p, at); } public int getIconWidth() { return 2*R2; } public int getIconHeight() { return 2*R2; } public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2d = (Graphics2D) g; g2d.translate(x, y); g2d.setPaint(Color.YELLOW); g2d.fill(star); g2d.setPaint(Color.BLACK); g2d.draw(star); g2d.translate(-x, -y); } } }} **解説 [#n8e3b936] -上段、左 --GeneralPath(=Path2D.Float) を使用して星型図形を作成 --%JAVA_HOME%/demo/jfc/Java2D/src/java2d/demos/Lines/Joins.java を参考 -上段、中 --Polygon を使用して星型図形を作成 -上段、右 --フォントから星型(★="\u2605")のアウトラインを取得して描画 -下段、左 --10個の頂点を予め計算してGeneralPathで星型を作成 --[[ついにベールを脱いだJavaFX:第9回 アニメーションを用いてより魅力的に[応用編]|gihyo.jp … 技術評論社>http://gihyo.jp/dev/serial/01/javafx/0009?page=2]] を参考 -下段、中 --Path2D.Double で、星型図形を作成しアイコンを作成 --外側の円の半径は、40px -下段、右 --Path2D.Double で、星型図形を作成しアイコンを作成 --外側の円の半径は、40px、内側の円の半径は、20px **参考リンク [#i362dcf8] -%JAVA_HOME%/demo/jfc/Java2D/src/java2d/demos/Lines/Joins.java -[[ついにベールを脱いだJavaFX:第9回 アニメーションを用いてより魅力的に[応用編]|gihyo.jp … 技術評論社>http://gihyo.jp/dev/serial/01/javafx/0009?page=2]] -[[Java2D Shapes project.>http://java-sl.com/shapes.html]] -[[プログラマメモ2: [java]扇形っぽいのを描く>http://programamemo2.blogspot.com/2008/12/java.html]] -[[PathIteratorからSVGを生成>Swing/PathIterator]] **コメント [#r9db5d6f] #comment