TITLE:Separatorを波線で表示

Separatorを波線で表示

編集者:Terai Atsuhiro
作成日:2006-06-19
更新日:2021-11-12 (金) 13:56:05

概要

波線を使ったSeparatorを作成します。

#screenshot

サンプルコード

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();
    setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
    Insets i = getInsets();
    if(orientation==SwingConstants.HORIZONTAL) {
      int w = ICONWIDTH+i.top+i.bottom;
      setPreferredSize(new Dimension(100, w));
      setMinimumSize(new Dimension(0, w));
      setMaximumSize(new Dimension(Integer.MAX_VALUE, w));
    }else{
      int w = ICONWIDTH+i.left+i.right;
      setPreferredSize(new Dimension(w, 10));
      setMinimumSize(new Dimension(w, 0));
      setMaximumSize(new Dimension(w, Integer.MAX_VALUE));
    }
  }
  public void paint(Graphics g) {
    int xpos, ypos;
    int tileW = tileIcon.getIconWidth();
    int tileH = tileIcon.getIconHeight();
    Insets i = getInsets();
    Graphics cg;
    cg = g.create();
    cg.setClip(0, 0, getWidth(), getHeight());
    if(getOrientation()==SwingConstants.HORIZONTAL) {
      for(xpos = i.left; getWidth() - xpos > 0; xpos += tileW) {
        tileIcon.paintIcon(this, cg, xpos, i.top);
      }
    }else{
      for(ypos = i.top; getHeight() - ypos > 0; ypos += tileH) {
        tileIcon.paintIcon(this, cg, i.left, ypos);
      }
    }
    cg.dispose();
  }
  class WavyLineIcon implements Icon {
    public void paintIcon(Component c, Graphics g, int x, int y) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setColor(SystemColor.textText);
      g2.translate(x,y);
      if(getOrientation()==SwingConstants.HORIZONTAL) {
        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 );
      }else{
        g2.drawLine( 0, 0, 0, 0 );
        g2.drawLine( 1, 1, 1, 1 );
        g2.drawLine( 2, 2, 2, 3 );
        g2.drawLine( 1, 4, 1, 4 );
        g2.drawLine( 0, 5, 0, 5 );
      }
      g2.translate(-x,-y);
    }
    public int getIconWidth()  {
      return (getOrientation()==SwingConstants.HORIZONTAL)?
        ICONWIDTH*2:ICONWIDTH;
    }
    public int getIconHeight() {
      return (getOrientation()==SwingConstants.HORIZONTAL)?
        ICONWIDTH:ICONWIDTH*2;
    }
  }
}
  • &jnlp;
  • &jar;
  • &zip;

解説

水平、垂直用の波パターンIconをそれぞれ作成して、これを順番に並べています。

上記のサンプルでは水平方向限定のセパレーターになります。

コメント

  • SwingConstants.HORIZONTALに対応しました。 -- terai