• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Separatorを波線で表示
#navi(../)
*Separatorを波線で表示 [#g6af9d8e]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-06-19~
更新日:&lastmod;

#contents

**概要 [#jc12c5ca]
波線を使ったSeparatorを作成します。

#screenshot

**サンプルコード [#r8936d7c]
#code{{
 class WavyLineSeparator extends JSeparator{
   private final static int ICONWIDTH = 3;
   private final Icon tileIcon;
   public WavyLineSeparator() {
     this(SwingConstants.HORIZONTAL);
   }
   public WavyLineSeparator(int orientation) {
     super(orientation);
     tileIcon = new WavyLineIcon();
     if(orientation==SwingConstants.HORIZONTAL) {
       setBorder(BorderFactory.createEmptyBorder(2,1,2,1));
       int w = ICONWIDTH+getInsets().top+getInsets().bottom;
       setPreferredSize(new Dimension(100, w));
       setMinimumSize(new Dimension(0, w));
       setMaximumSize(new Dimension(Integer.MAX_VALUE, w));
     }else{
       setBorder(BorderFactory.createEmptyBorder(1,2,1,2));
       int w = ICONWIDTH+getInsets().left+getInsets().right;
       setPreferredSize(new Dimension(w, 30));
       setMinimumSize(new Dimension(w, 0));
       setMaximumSize(new Dimension(w, Integer.MAX_VALUE));
     }
   }
   public void paintComponent(Graphics g) {
     //super.paintComponent(g);
     int xpos, ypos;
     int tileW = tileIcon.getIconWidth();
     int tileH = tileIcon.getIconHeight();
     Insets i = getInsets();
     if(getOrientation()==SwingConstants.HORIZONTAL) {
       for(xpos = i.left; getWidth() - xpos > 0; xpos += tileW) {
         tileIcon.paintIcon(this, g, xpos, i.top);
       }
     }else{
       for(ypos = i.top; getHeight() - ypos > 0; ypos += tileH) {
         tileIcon.paintIcon(this, g, i.left, ypos);
       }
     }
   }
   class WavyLineIcon implements Icon {
     private final Color sfc = UIManager.getColor("Separator.foreground");
     public void paintIcon(Component c, Graphics g, int x, int y) {
       Graphics2D g2 = (Graphics2D)g;
       AffineTransform oldTransform = g2.getTransform();
       g2.setColor(sfc);
       g2.setPaint(sfc);
       if(getOrientation()==SwingConstants.VERTICAL) {
         g2.translate(x+getIconWidth(), y);
         g2.rotate(Math.PI/2);
       }else{
         g2.translate(x,y);
       }
       g2.drawLine( 0, 2, 0, 2 );
       g2.drawLine( 1, 1, 1, 1 );
       g2.drawLine( 2, 0, 3, 0 );
       g2.drawLine( 4, 1, 4, 1 );
       g2.drawLine( 5, 2, 5, 2 );
       g2.setTransform(oldTransform); 
     }
     public int getIconWidth()  {
       return (getOrientation()==SwingConstants.HORIZONTAL)?
         ICONWIDTH*2:ICONWIDTH;
     }
     public int getIconHeight() {
       return (getOrientation()==SwingConstants.HORIZONTAL)?
         ICONWIDTH:ICONWIDTH*2;
     }
   }
 }
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#q8db10c5]
水平用の波パターンIconを作成して、これを順番に並べてセパレータとして描画しています。垂直用のパターンは水平用を回転して生成しています。

//**参考リンク
**コメント [#l8c965e9]
- SwingConstants.VERTICAL に対応しました。 -- [[terai]] &new{2006-06-19 (月) 14:39:49};

#comment