Swing/ToggleButtonBar のバックアップ差分(No.4)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ToggleButtonBar へ行く。
- 1 (2012-11-23 (金) 04:24:39)
- 2 (2012-12-05 (水) 18:25:53)
- 3 (2012-12-07 (金) 16:31:19)
- 4 (2012-12-11 (火) 17:50:00)
- 5 (2013-01-28 (月) 02:15:06)
- 6 (2013-02-20 (水) 15:14:29)
- 7 (2014-11-22 (土) 03:59:58)
- 8 (2014-11-23 (日) 17:01:31)
- 9 (2015-03-26 (木) 15:41:20)
- 10 (2016-01-16 (土) 01:52:43)
- 11 (2017-06-22 (木) 10:46:56)
- 12 (2017-11-16 (木) 14:51:07)
- 13 (2018-02-24 (土) 19:51:30)
- 14 (2019-06-25 (火) 16:01:21)
- 15 (2021-03-03 (水) 07:24:11)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JRadioButtonを使ってToggleButtonBarを作成
#navi(../)
#tags(JRadioButton, Icon, Path2D)
RIGHT:Posted by &author(aterai); at 2012-11-18
*JRadioButtonを使ってToggleButtonBarを作成 [#v862377c]
``JRadioButton``のアイコンを変更して、``ToggleButtonBar``を作成します。
-&jnlp;
-&jar;
-&zip;
//#screenshot
#ref(https://lh3.googleusercontent.com/-5-1KU3hp2co/UKjlTJyKvRI/AAAAAAAABW8/QuYQcKDeeyM/s800/ToggleButtonBar.png)
**サンプルコード [#pb58be2c]
#code(link){{
enum Location {
FIRST, CENTER, LAST;
}
class ToggleButtonBarCellIcon implements Icon {
private static final Color TL = new Color(1f,1f,1f,.2f);
private static final Color BR = new Color(0f,0f,0f,.2f);
private static final Color ST = new Color(1f,1f,1f,.4f);
private static final Color SB = new Color(1f,1f,1f,.1f);
private Color ssc;
private Color bgc;
private Location l;
public ToggleButtonBarCellIcon() {
this(Location.CENTER);
}
public ToggleButtonBarCellIcon(Location l) {
this.l = l;
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
int r = 8;
int w = c.getWidth();
int h = c.getHeight();
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Path2D.Float p = new Path2D.Float();
switch(l) {
case CENTER:
p.moveTo(x, y);
p.lineTo(x + w, y);
p.lineTo(x + w, y + h);
p.lineTo(x, y + h);
break;
case FIRST:
p.moveTo(x, y + r);
p.quadTo(x, y, x + r, y);
p.lineTo(x + w, y);
p.lineTo(x + w, y + h);
p.lineTo(x + r, y + h);
p.quadTo(x, y + h, x, y + h - r);
break;
case LAST:
p.moveTo(x, y);
p.lineTo(x + w - r, y);
p.quadTo(x + w, y, x + w, y + r);
p.lineTo(x + w, y + h - r);
p.quadTo(x + w, y + h, x + w -r, y + h);
p.lineTo(x, y + h);
break;
}
p.closePath();
Area area = new Area(p);
g2.setPaint(c.getBackground());
g2.fill(area);
ssc = TL;
bgc = BR;
if(c instanceof AbstractButton) {
ButtonModel m = ((AbstractButton)c).getModel();
if(m.isSelected() || m.isRollover()) {
ssc = ST;
bgc = SB;
}
}
g2.setPaint(new GradientPaint(x, y, ssc, x, y+h, bgc, true));
g2.fill(area);
g2.setPaint(BR);
g2.draw(area);
g2.dispose();
}
@Override public int getIconWidth() {
return 80;
}
@Override public int getIconHeight() {
return 20;
}
}
}}
**解説 [#g0ae8b43]
上記のサンプルでは、``JRadioButton``に選択またはロールオーバーでグラデーションが変化する``Icon``を設定し、これらを``ButtonGroup``に追加することで、``ToggleButtonBar``を作成しています。
- 配色は、[http://weboook.blog22.fc2.com/blog-entry-342.html 簡単、きれい!RGBaカラーを使って横メニューを作ってみる|Webpark] をそのまま引用
- ``JRadioButton``のサイズは``Icon``のサイズと等しくなるように、テキストとアイコンは中央揃えで重ねて表示し、``Border``も0に設定
-- ``GridLayout``の水平間隔なども0にして隙間ができないように配置
- 最初の``JRadioButton``は左、最後の``JRadioButton``は右の角を丸める
-- 参考: [[JComboBoxの角を丸める>Swing/RoundedComboBox]]
-- ``JRadioButton#setContentAreaFilled(false)``として、描画をすべてアイコンで行う(角を丸めた時に背景色を描画しないように)
**参考リンク [#g6068810]
- [http://weboook.blog22.fc2.com/blog-entry-342.html 簡単、きれい!RGBaカラーを使って横メニューを作ってみる|Webpark]
- [[CardLayoutを使ってJTabbedPane風のコンポーネントを作成>Swing/CardLayoutTabbedPane]]
- [[JComboBoxの角を丸める>Swing/RoundedComboBox]]
**コメント [#h3173999]
#comment