TITLE:GeneralPathなどで星型図形を作成する
Posted by terai at 2009-02-09

GeneralPathなどで星型図形を作成する

GeneralPathなどを使って星型の図形をパネルに描画したり、アイコンを作成します。
  • category: swing folder: GeneralPath title: GeneralPathなどで星型図形を作成する tags: [GeneralPath, Path2D, Polygon, Icon, AffineTransform, Font] author: aterai pubdate: 2009-02-09T18:26:33+09:00 description: GeneralPathなどを使って星型の図形をパネルに描画したり、アイコンを作成します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTNT-1toKI/AAAAAAAAAak/t96zX52eOVg/s800/GeneralPath.png

概要

GeneralPathなどを使って星型の図形をパネルに描画したり、アイコンを作成します。

#screenshot

サンプルコード

#spanend
#spandel
class StarPanel1 extends JPanel{
#spanend
  public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
class StarPanel1 extends JPanel {
#spanend
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    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.moveTo(-w / 4f, -h / 12f);
    p.lineTo(w / 4f, -h / 12f);
    p.lineTo(-w / 6f, h / 4f);
    p.lineTo(0f, -h / 4f);
    p.lineTo(w / 6f, h / 4f);
    p.closePath();
    //</blockquote>
    g2.translate(w/2, h/2);
    g2.translate(w / 2, h / 2);
    g2.setColor(Color.YELLOW);
    g2.fill(p);
    g2.setColor(Color.BLACK);
    g2.draw(p);
    g2.dispose();
  }
}
#spandel
#spanend
#spandel
class StarIcon2 implements Icon{
#spanend
#spanadd

#spanend
#spanadd
class StarIcon2 implements Icon {
#spanend
  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 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));
    double agl = 0d;
    double add = 2 * Math.PI / (VC * 2);
    Path2D 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);
    at = AffineTransform.getRotateInstance(-Math.PI / 2, R2, 0);
    star = new Path2D.Double(p, at);
  }
  public int getIconWidth() {
    return 2*R2;
#spanadd

#spanend
  @Override public int getIconWidth() {
    return 2 * R2;
  }
  public int getIconHeight() {
    return 2*R2;
#spanadd

#spanend
  @Override 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);
#spanadd

#spanend
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.translate(x, y);
    g2.setPaint(Color.YELLOW);
    g2.fill(star);
    g2.setPaint(Color.BLACK);
    g2.draw(star);
    g2.dispose();
  }
}

解説

  • 上段、左
    • GeneralPath(=Path2D.Float) を使用して星型図形を作成
    • %JAVA_HOME%/demo/jfc/Java2D/src/java2d/demos/Lines/Joins.java を参考

解説

  • 上段、左
    • GeneralPath(=Path2D.Float)を使用して星型図形を作成
    • %JAVA_HOME%/demo/jfc/Java2D/src/java2d/demos/Lines/Joins.javaを参考
  • 上段、中
    • Polygonを使用して星型図形を作成
  • 上段、右
    • Fontから星型文字★(U+2605)のアウトラインを取得して描画
  • 下段、左
  • 下段、中
    • Icon#paintIcon(...)メソッドをオーバーライドしてPath2D.Doubleで星型図形を描画
    • 外周の半径: 40px
  • 下段、右
    • Icon#paintIcon(...)メソッドをオーバーライドしてPath2D.Doubleで星型図形を描画
    • 外周の半径: 40px
    • 内周の半径: 20px
  • 上段、中
    • Polygon を使用して星型図形を作成

参考リンク

  • 上段、右
    • フォントから星型(★="\u2605")のアウトラインを取得して描画
  • 下段、中
    • Path2D.Double で、星型図形を作成しアイコンを作成
    • 外側の円の半径は、40px
  • 下段、右
    • Path2D.Double で、星型図形を作成しアイコンを作成
    • 外側の円の半径は、40px、内側の円の半径は、20px

参考リンク

コメント

コメント