Swing/WavyLineSeparator のバックアップの現在との差分(No.2)
TITLE:Separatorを波線で表示
Separatorを波線で表示
編集者:Terai Atsuhiro~
作成日:& date;
更新日:2021-11-12 (金) 13:56:05
概要
Icon
で波パターンを作成し、これを順番に並べて波線を描画するJSeparator
を作成します。
Screenshot
Advertisement
概要
波線を使ったSeparatorを作成します。サンプルコード
#spanend
#spanadd
class WavyLineSeparator extends JSeparator {
#spanend
private static final int ICONWIDTH = 3;
private static final Icon WAVY_HLINE = new WavyLineIcon();
private static final Icon WAVY_VLINE = new WavyLineIcon(VERTICAL);
public WavyLineSeparator() {
this(HORIZONTAL);
}
#spandel
#screenshot
#spanend
public WavyLineSeparator(int orientation) {
super(orientation);
if (orientation == HORIZONTAL) {
setBorder(BorderFactory.createEmptyBorder(2, 1, 2, 1));
} else {
setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2));
}
}
#spandel
**サンプルコード [#r8936d7c]
#spanend
class WavyLineSeparator extends JComponent{
public WavyLineSeparator() {
super();
setPreferredSize(new Dimension(100, 3));
setMinimumSize(new Dimension(0, 3));
setMaximumSize(new Dimension(Integer.MAX_VALUE, 3));
setBorder(new MatteBorder(0,0,3,0,new WavyLineIcon()));;
}
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);
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.translate(-x,-y);
}
public int getIconWidth() { return 6; }
public int getIconHeight() { return 3; }
}
}
}
@Override protected void paintComponent(Graphics g) {
// super.paintComponent(g);
// g.setClip(0, 0, getWidth(), getHeight());
int pos;
Insets i = getInsets();
if (getOrientation() == HORIZONTAL) {
for (pos = i.left; getWidth() - pos > 0; pos += WAVY_HLINE.getIconWidth()) {
WAVY_HLINE.paintIcon(this, g, pos, i.top);
}
} else {
for (pos = i.top; getHeight() - pos > 0; pos += WAVY_VLINE.getIconHeight()) {
WAVY_VLINE.paintIcon(this, g, i.left, pos);
}
}
}
-&jnlp;
-&jar;
-&zip;
@Override public Dimension getPreferredSize() {
Insets i = getInsets();
if (getOrientation() == HORIZONTAL) {
return new Dimension(30, ICONWIDTH + i.top + i.bottom);
} else {
return new Dimension(ICONWIDTH + i.left + i.right, 30);
}
}
#spandel
**解説 [#q8db10c5]
#spanend
#spandel
波上のIconを作成して、これをMatteBorderを使って並べています。
#spanend
static class WavyLineIcon implements Icon {
private final Color sfc = UIManager.getColor("Separator.foreground");
private final int orientation;
public WavyLineIcon() {
this.orientation = HORIZONTAL;
}
#spandel
上記のサンプルでは水平方向限定のセパレーターになります。
#spanend
public WavyLineIcon(int orientation) {
this.orientation = orientation;
}
#spandel
//**参考リンク
#spanend
#spandel
**コメント [#l8c965e9]
#spanend
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
AffineTransform oldTransform = g2.getTransform();
g2.setPaint(sfc);
if (orientation == 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);
g2.dispose();
}
#spanadd
#spanend
@Override public int getIconWidth() {
return (orientation == HORIZONTAL) ? ICONWIDTH * 2 : ICONWIDTH;
}
#spanadd
#spanend
@Override public int getIconHeight() {
return (orientation == HORIZONTAL) ? ICONWIDTH : ICONWIDTH * 2;
}
}
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin解説
上記のサンプルでは、水平用の波パターンIcon
を作成し、これを順番に並べてセパレータとして描画しています。垂直用のパターンは水平用を90
度回転して生成しています。