// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    JCheckBox check = new JCheckBox("JCheckBox#setIcon(...)");
    check.setIcon(new CheckIcon());
    add(check, BorderLayout.SOUTH);
    add(new JScrollPane(new JTextArea()));
    setPreferredSize(new Dimension(320, 240));
  }

  private static JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("JMenu");
    menuBar.add(menu);
    menu.add(new JCheckBoxMenuItem("default"));
    UIManager.put("CheckBoxMenuItem.checkIcon", new CheckIcon());
    menu.add(new JCheckBoxMenuItem("checkIcon test"));

    JMenu menu2 = new JMenu("JMenu2");
    JCheckBoxMenuItem jcbmi = new JCheckBoxMenuItem("setIcon");
    jcbmi.setIcon(new CheckIcon());
    // jcbmi.setSelectedIcon(new CheckIcon());
    menu2.add(jcbmi);
    menuBar.add(menu);
    menuBar.add(menu2);
    return menuBar;
  }

  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGui();
      }
    });
  }

  public static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.setJMenuBar(createMenuBar());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class CheckIcon implements Icon {
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    if (c instanceof AbstractButton) {
      ButtonModel m = ((AbstractButton) c).getModel();
      Graphics2D g2 = (Graphics2D) g.create();
      g2.translate(x, y);
      g2.setPaint(m.isSelected() ? Color.ORANGE : Color.GRAY);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2.fillOval(0, 2, getIconWidth() - 4, getIconHeight() - 4);
      g2.dispose();
    }
  }

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

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

@@ -602,32 +602,33 @@
 uBar.add(menu);%0A
+%0A
     menu.add(new
@@ -655,24 +655,92 @@
 default%22));%0A
+    Icon defIcon = UIManager.getIcon(%22CheckBoxMenuItem.checkIcon%22);%0A
     UIManage
@@ -847,16 +847,74 @@
 test%22));
+%0A    UIManager.put(%22CheckBoxMenuItem.checkIcon%22, defIcon);
 %0A%0A    JM
@@ -2431,20 +2431,20 @@
 illOval(
-0, 2
+1, 1
 , getIco
@@ -2454,17 +2454,17 @@
 dth() - 
-4
+2
 , getIco
@@ -2475,17 +2475,17 @@
 ght() - 
-4
+2
 );%0A     

CheckBoxMenuItem.checkIcon