Swing/RadioButtonMenuItemIcon のバックアップの現在との差分(No.1)
TITLE:JRadioButtonMenuItemのチェックアイコンを変更する
Posted by aterai at 2010-11-08
JRadioButtonMenuItemのチェックアイコンを変更する
JRadioButtonMenuItemのチェックアイコンを変更します。- category: swing folder: RadioButtonMenuItemIcon title: JRadioButtonMenuItemのチェックアイコンを変更する tags: [JRadioButtonMenuItem, Icon, UIManager] author: aterai pubdate: 2010-11-08T16:26:30+09:00 description: JRadioButtonMenuItemのチェックアイコンを変更します。 image:
概要
JRadioButtonMenuItem
のチェックアイコンを変更します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
//com.sun.java.swing.plaf.windows.WindowsIconFactory.java
#spanend
#spanadd
// com.sun.java.swing.plaf.windows.WindowsIconFactory.java
#spanend
class RadioButtonMenuItemIcon1 implements Icon, UIResource, Serializable {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
if(b.isSelected()) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillRoundRect(x+3,y+3, getIconWidth()-6, getIconHeight()-6, 4, 4);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
if (b.isSelected()) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillRoundRect(
x + 3, y + 3, getIconWidth() - 6, getIconHeight() - 6, 4, 4);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2.dispose();
}
}
@Override public int getIconWidth() { return 12; }
@Override public int getIconHeight() { return 12; }
#spanadd
#spanend
@Override public int getIconWidth() {
return 12;
}
#spanadd
#spanend
@Override public int getIconHeight() {
return 12;
}
}
#spanadd
#spanend
class RadioButtonMenuItemIcon2 implements Icon, UIResource, Serializable {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
if(b.isSelected()) {
//g.fillRoundRect(x+3,y+3, getIconWidth()-6, getIconHeight()-6, 4, 4);
g.fillOval(x+2,y+2, getIconWidth()-5, getIconHeight()-5);
//g.fillArc(x+2,y+2,getIconWidth()-5, getIconHeight()-5, 0, 360);
if (b.isSelected()) {
g.fillOval(x + 2, y + 2, getIconWidth() - 5, getIconHeight() - 5);
}
}
@Override public int getIconWidth() { return 12; }
@Override public int getIconHeight() { return 12; }
#spanadd
#spanend
@Override public int getIconWidth() {
return 12;
}
#spanadd
#spanend
@Override public int getIconHeight() {
return 12;
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、WindowsLookAndFeel(Java1.6.0)で、JRadioButtonMenuItemのチェックアイコンがすこし歪?なので以下のように修正しています。解説
上記のサンプルでは、UIManager.put("RadioButtonMenuItem.checkIcon", icon)
を使用してJRadioButtonMenuItem
のチェックアイコンを変更しています。
- default
-
default
- デフォルト
- ANTIALIASING
- com.sun.java.swing.plaf.windows.WindowsIconFactory.javaのアイコンをg2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)でアンチエイリアス
- fillOval
- fillRoundRectではなく、fillOvalを使用
-
Java1.6.0
のWindowsLookAndFeel
でJRadioButtonMenuItem
のチェックアイコンが歪になる場合がある
-
ANTIALIASING
-
com.sun.java.swing.plaf.windows.WindowsIconFactory
のアイコンをg2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
でアンチエイリアス
-
-
fillOval
-
fillRoundRect
ではなくfillOval
を使用するチェックアイコンを設定
-