Swing/WavyLineSeparator のバックアップの現在との差分(No.10)
TITLE:Separatorを波線で表示
Posted by terai at 2006-06-19
Separatorを波線で表示
波線を使ったSeparatorを作成します。- category: swing folder: WavyLineSeparator title: Separatorを波線で表示 tags: [JSeparator, Icon] author: aterai pubdate: 2006-06-19T06:28:53+09:00 description: Iconで波パターンを作成し、これを順番に並べて波線を描画するJSeparatorを作成します。 image:
概要
Icon
で波パターンを作成し、これを順番に並べて波線を描画するJSeparator
を作成します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spandel
class WavyLineSeparator extends JSeparator{
#spanend
private final static int ICONWIDTH = 3;
private final Icon tileIcon;
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#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(SwingConstants.HORIZONTAL);
this(HORIZONTAL);
}
#spanadd
#spanend
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));
if (orientation == HORIZONTAL) {
setBorder(BorderFactory.createEmptyBorder(2, 1, 2, 1));
} else {
setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2));
}
}
@Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
int xpos, ypos;
int tileW = tileIcon.getIconWidth();
int tileH = tileIcon.getIconHeight();
#spanadd
#spanend
@Override protected void paintComponent(Graphics g) {
// super.paintComponent(g);
// g.setClip(0, 0, getWidth(), getHeight());
int pos;
Insets i = getInsets();
if(getOrientation()==SwingConstants.HORIZONTAL) {
for(xpos = i.left; getWidth() - xpos > 0; xpos += tileW) {
tileIcon.paintIcon(this, g, xpos, i.top);
if (getOrientation() == HORIZONTAL) {
for (pos = i.left; getWidth() - pos > 0; pos += WAVY_HLINE.getIconWidth()) {
WAVY_HLINE.paintIcon(this, g, pos, i.top);
}
}else{
for(ypos = i.top; getHeight() - ypos > 0; ypos += tileH) {
tileIcon.paintIcon(this, g, i.left, ypos);
} else {
for (pos = i.top; getHeight() - pos > 0; pos += WAVY_VLINE.getIconHeight()) {
WAVY_VLINE.paintIcon(this, g, i.left, pos);
}
}
}
class WavyLineIcon implements Icon {
#spanadd
#spanend
@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);
}
}
#spanadd
#spanend
static class WavyLineIcon implements Icon {
private final Color sfc = UIManager.getColor("Separator.foreground");
//1.5 @Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D)g;
private final int orientation;
public WavyLineIcon() {
this.orientation = HORIZONTAL;
}
#spanadd
#spanend
public WavyLineIcon(int orientation) {
this.orientation = orientation;
}
#spanadd
#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(getOrientation()==SwingConstants.VERTICAL) {
g2.translate(x+getIconWidth(), y);
g2.rotate(Math.PI/2);
}else{
g2.translate(x,y);
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.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();
}
//1.5 @Override
public int getIconWidth() {
return (getOrientation()==SwingConstants.HORIZONTAL)?
ICONWIDTH*2:ICONWIDTH;
#spanadd
#spanend
@Override public int getIconWidth() {
return (orientation == HORIZONTAL) ? ICONWIDTH * 2 : ICONWIDTH;
}
//1.5 @Override
public int getIconHeight() {
return (getOrientation()==SwingConstants.HORIZONTAL)?
ICONWIDTH:ICONWIDTH*2;
#spanadd
#spanend
@Override public int getIconHeight() {
return (orientation == HORIZONTAL) ? ICONWIDTH : ICONWIDTH * 2;
}
}
}
解説
水平用の波パターンIconを作成して、これを順番に並べてセパレータとして描画しています。垂直用のパターンは水平用を回転して生成しています。解説
上記のサンプルでは、水平用の波パターンIcon
を作成し、これを順番に並べてセパレータとして描画しています。垂直用のパターンは水平用を90
度回転して生成しています。
コメント
- SwingConstants.VERTICAL に対応しました。 -- terai