• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JRadioButtonMenuItemのチェックアイコンを変更する
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2010-11-08
*JRadioButtonMenuItemのチェックアイコンを変更する [#i1788d15]
JRadioButtonMenuItemのチェックアイコンを変更します。
---
category: swing
folder: RadioButtonMenuItemIcon
title: JRadioButtonMenuItemのチェックアイコンを変更する
tags: [JRadioButtonMenuItem, Icon, UIManager]
author: aterai
pubdate: 2010-11-08T16:26:30+09:00
description: JRadioButtonMenuItemのチェックアイコンを変更します。
image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTRVf_tDmI/AAAAAAAAAhA/1F6GcDuJmcg/s800/RadioButtonMenuItemIcon.png
---
* 概要 [#summary]
`JRadioButtonMenuItem`のチェックアイコンを変更します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTRVf_tDmI/AAAAAAAAAhA/1F6GcDuJmcg/s800/RadioButtonMenuItemIcon.png)

//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTRVf_tDmI/AAAAAAAAAhA/1F6GcDuJmcg/s800/RadioButtonMenuItemIcon.png)

**サンプルコード [#a5678c05]
* サンプルコード [#sourcecode]
#code(link){{
//com.sun.java.swing.plaf.windows.WindowsIconFactory.java
// com.sun.java.swing.plaf.windows.WindowsIconFactory.java
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; }

  @Override public int getIconWidth()  {
    return 12;
  }

  @Override public int getIconHeight() {
    return 12;
  }
}

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; }

  @Override public int getIconWidth()  {
    return 12;
  }

  @Override public int getIconHeight() {
    return 12;
  }
}
}}

**解説 [#s98aef9d]
上記のサンプルでは、WindowsLookAndFeel(Java1.6.0)で、JRadioButtonMenuItemのチェックアイコンがすこし歪?なので以下のように修正しています。
* 解説 [#explanation]
上記のサンプルでは、`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`を使用するチェックアイコンを設定

**参考リンク [#cb51cade]
-[[JCheckBoxMenuItemのチェックアイコンを変更する>Swing/CheckBoxMenuItemIcon]]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JRadioButtonMenuItem.html JRadioButtonMenuItem (Java Platform SE 8)]
- [[JCheckBoxMenuItemのチェックアイコンを変更する>Swing/CheckBoxMenuItemIcon]]

**コメント [#xa0be7a9]
- Windows 7 でテストするとアイコンの歪みはないようです。修正されてたのかも? -- [[aterai]] &new{2012-08-08 (水) 19:51:09};
* コメント [#comment]
#comment
- `Windows 7`でテストするとアイコンの歪みはないので、修正された模様。 -- &user(aterai); &new{2012-08-08 (水) 19:51:09};

#comment