Swing/WavyLineSeparator のバックアップの現在との差分(No.5)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/WavyLineSeparator へ行く。
- 1 (2006-06-01 (木) 09:28:20)
- 2 (2006-06-19 (月) 06:28:53)
- 3 (2006-06-19 (月) 14:39:12)
- 4 (2006-06-21 (水) 18:42:49)
- 5 (2006-06-22 (木) 18:04:41)
- 6 (2007-01-11 (木) 12:11:35)
- 7 (2007-03-14 (水) 11:31:49)
- 8 (2007-03-28 (水) 18:43:22)
- 9 (2007-10-16 (火) 15:56:39)
- 10 (2009-11-05 (木) 18:24:47)
- 11 (2010-11-15 (月) 14:55:16)
- 12 (2013-02-28 (木) 14:33:31)
- 13 (2014-11-18 (火) 16:09:46)
- 14 (2014-12-01 (月) 17:26:21)
- 15 (2016-01-29 (金) 14:02:05)
- 16 (2017-06-30 (金) 13:52:10)
- 17 (2018-06-01 (金) 14:42:37)
- 18 (2020-05-30 (土) 01:08:58)
- 19 (2021-11-12 (金) 13:56:05)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:Separatorを波線で表示 #navi(../) *Separatorを波線で表示 [#g6af9d8e] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2006-06-19~ 更新日:&lastmod; --- category: swing folder: WavyLineSeparator title: Separatorを波線で表示 tags: [JSeparator, Icon] author: aterai pubdate: 2006-06-19T06:28:53+09:00 description: Iconで波パターンを作成し、これを順番に並べて波線を描画するJSeparatorを作成します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWkeY23gI/AAAAAAAAApc/r6W1VFeeAYA/s800/WavyLineSeparator.png --- * 概要 [#summary] `Icon`で波パターンを作成し、これを順番に並べて波線を描画する`JSeparator`を作成します。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWkeY23gI/AAAAAAAAApc/r6W1VFeeAYA/s800/WavyLineSeparator.png) **概要 [#jc12c5ca] 波線を使ったSeparatorを作成します。 * サンプルコード [#sourcecode] #code(link){{ class WavyLineSeparator extends JSeparator { 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); } #screenshot public WavyLineSeparator(int orientation) { super(orientation); if (orientation == HORIZONTAL) { setBorder(BorderFactory.createEmptyBorder(2, 1, 2, 1)); } else { setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2)); } } **サンプルコード [#r8936d7c] 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; } } } @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); } } **解説 [#q8db10c5] 水平、垂直用の波パターンIconをそれぞれ作成して、これを順番に並べています。 static class WavyLineIcon implements Icon { private final Color sfc = UIManager.getColor("Separator.foreground"); private final int orientation; public WavyLineIcon() { this.orientation = HORIZONTAL; } %%上記のサンプルでは水平方向限定のセパレーターになります。%% public WavyLineIcon(int orientation) { this.orientation = orientation; } //**参考リンク **コメント [#l8c965e9] - %%SwingConstants.HORIZONTAL%% SwingConstants.VERTICAL に対応しました。 -- [[terai]] &new{2006-06-19 (月) 14:39:49}; @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(); } @Override public int getIconWidth() { return (orientation == HORIZONTAL) ? ICONWIDTH * 2 : ICONWIDTH; } @Override public int getIconHeight() { return (orientation == HORIZONTAL) ? ICONWIDTH : ICONWIDTH * 2; } } } }} * 解説 [#explanation] 上記のサンプルでは、水平用の波パターン`Icon`を作成し、これを順番に並べてセパレータとして描画しています。垂直用のパターンは水平用を`90`度回転して生成しています。 * 参考リンク [#reference] - [[JPanelの背景に画像を並べる>Swing/BackgroundImage]] - [[TitledBorderとMatteBorderを使用してTitledSeparatorを作成する>Swing/TitledSeparator]] * コメント [#comment] #comment - `SwingConstants.VERTICAL`に対応。 -- &user(aterai); &new{2006-06-19 (月) 14:39:49}; #comment