Swing/ToggleButtonBar のバックアップの現在との差分(No.3)
TITLE:JRadioButtonを使ってToggleButtonBarを作成
Posted by aterai at 2012-11-18
JRadioButtonを使ってToggleButtonBarを作成
`JRadioButton
のアイコンを変更して、
ToggleButtonBar
`を作成します。
-
category: swing
folder: ToggleButtonBar
title: JRadioButtonを使ってToggleButtonBarを作成
tags: [JRadioButton, Icon, Path2D]
author: aterai
pubdate: 2012-11-19T00:07:37+09:00
description: JRadioButtonのアイコンを変更して、ToggleButtonBarを作成します。
image:
hreflang:
href: https://java-swing-tips.blogspot.com/2012/11/make-togglebuttonbar-with-jradiobuttons.html lang: en
概要
JRadioButton
のアイコンを変更して、ToggleButtonBar
を作成します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
enum Location {
#spanend
FIRST, CENTER, LAST;
#spandel
}
#spanend
#spandel
#spanend
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);
#spandel
#spanend
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;
}
#spanadd
#spanend
@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);
Container parent = c.getParent();
if (parent == null) {
return;
}
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:
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Path2D p = new Path2D.Float();
#spanadd
#spanend
if (c == parent.getComponent(0)) {
// JButton.segmentPosition: first
// :first-child
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:
} else if (c == parent.getComponent(parent.getComponentCount() - 1)) {
// JButton.segmentPosition: last
// :last-child
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.quadTo(x + w, y + h, x + w - r, y + h);
p.lineTo(x, y + h);
break;
} else {
// JButton.segmentPosition: middle
p.moveTo(x, y);
p.lineTo(x + w, y);
p.lineTo(x + w, y + h);
p.lineTo(x, y + h);
}
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()) {
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.setPaint(new GradientPaint(x, y, ssc, x, y + h, bgc, true));
g2.fill(area);
g2.setPaint(BR);
g2.draw(area);
g2.dispose();
}
#spanadd
#spanend
@Override public int getIconWidth() {
return 80;
}
#spanadd
#spanend
@Override public int getIconHeight() {
return 20;
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、`JRadioButton
に選択またはロールオーバーでグラデーションが変化する
Icon
を設定し、これらを
ButtonGroup
に追加することで、
ToggleButtonBar
`を作成しています。
解説
上記のサンプルでは、JRadioButton
に選択またはロールオーバーでハイライトが変化するIcon
を設定し、これらをButtonGroup
に追加することでToggleButtonBar
を作成しています。
- 配色は、簡単、きれい!RGBaカラーを使って横メニューを作ってみる|Webpark をそのまま引用
- `
JRadioButton
のサイズは
Icon
のサイズと等しくなるように、テキストとアイコンは中央揃えで重ねて表示し、
Border
`も0に設定- `
GridLayout
`の水平間隔なども0にして隙間ができないように配置
- `
- 最初の`
JRadioButton
は左、最後の
JRadioButton
`は右の角を丸める - 配色は簡単、きれい!RGBaカラーを使って横メニューを作ってみる|Webparkから引用
-
JRadioButton
のサイズはIcon
のサイズと等しくなるようにテキストとアイコンは中央揃えで重ねて表示し、Border
も0
に設定-
GridLayout
の水平間隔なども0
にして隙間ができないように配置
-
- 最初の
JRadioButton
は左、最後のJRadioButton
は右の角を丸める- 参考: JComboBoxの角を丸める
- `
JRadioButton#setContentAreaFilled(false)
`として、描画をすべてアイコンで行う(角を丸めた時に背景色を描画しないように) - 角を丸めた時に背景色を描画しないようにするため
JRadioButton#setContentAreaFilled(false)
を設定して描画をすべてアイコンで行う