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

package example;

import java.awt.*;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.util.stream.Stream;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());

    // [XP Style Icons - Download](https://xp-style-icons.en.softonic.com/)
    ImageIcon nicon = new ImageIcon(getClass().getResource("wi0063-32.png"));
    ImageProducer ip = new FilteredImageSource(nicon.getImage().getSource(), new SelectedImageFilter());
    ImageIcon sicon = new ImageIcon(createImage(ip));
    JToggleButton t1 = new JToggleButton(nicon);
    JToggleButton t2 = new JToggleButton(nicon, true);
    t1.setSelectedIcon(sicon);
    t2.setSelectedIcon(sicon);

    ButtonGroup bg = new ButtonGroup();
    JPanel p = new JPanel(new GridLayout(2, 2));
    p.setBorder(BorderFactory.createTitledBorder("ButtonGroup"));
    Stream.of(new JRadioButton("RadioButton1"), new JRadioButton("RadioButton2"), t1, t2).forEach(b -> {
      bg.add(b);
      p.add(b);
    });

    JButton clear = new JButton("clearSelection");
    clear.addActionListener(e -> bg.clearSelection());

    add(p, BorderLayout.NORTH);
    add(clear, BorderLayout.SOUTH);
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    setPreferredSize(new Dimension(320, 240));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private 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.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class SelectedImageFilter extends RGBImageFilter {
  // public SelectedImageFilter() {
  //   canFilterIndexColorModel = false;
  // }

  @Override public int filterRGB(int x, int y, int argb) {
    // Color color = new Color(argb, true);
    // float[] a = new float[4];
    // color.getComponents(a);
    // return new Color(a[0], a[1], a[2] * .5f, a[3]).getRGB();
    return (argb & 0xFF_FF_FF_00) | ((argb & 0xFF) >> 1);
  }
}

@@ -113,16 +113,53 @@
 .awt.*;%0A
+import java.awt.image.BufferedImage;%0A
 import j
@@ -193,16 +193,16 @@
 Source;%0A
-
 import j
@@ -264,24 +264,107 @@
 mageFilter;%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Optional;%0A
 import java.
@@ -375,32 +375,62 @@
 .stream.Stream;%0A
+import javax.imageio.ImageIO;%0A
 import javax.swi
@@ -533,25 +533,24 @@
 rLayout());%0A
-%0A
     // %5BXP S
@@ -621,79 +621,426 @@
     
-ImageIcon nicon = new ImageIcon(getClass().getResource(%22wi0063-32.png%22)
+String path = %22example/wi0063-32.png%22;%0A    ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    Image image = Optional.ofNullable(cl.getResource(path)).map(url -%3E %7B%0A      try (InputStream s = url.openStream()) %7B%0A        return ImageIO.read(s);%0A      %7D catch (IOException ex) %7B%0A        return makeMissingImage();%0A      %7D%0A    %7D).orElseGet(MainPanel::makeMissingImage);%0A    Icon icon = new ImageIcon(image
 );%0A 
@@ -1089,24 +1089,13 @@
 rce(
-nicon.getI
+i
 mage
-()
 .get
@@ -1147,17 +1147,24 @@
 geIcon s
-i
+electedI
 con = ne
@@ -1189,24 +1189,25 @@
 Image(ip));%0A
+%0A
     JToggleB
@@ -1239,16 +1239,54 @@
 ton(
-n
 icon);%0A
+    t1.setSelectedIcon(selectedIcon);%0A%0A
     
@@ -1322,17 +1322,16 @@
 eButton(
-n
 icon, tr
@@ -1332,33 +1332,33 @@
 on, true);%0A    t
-1
+2
 .setSelectedIcon
@@ -1363,43 +1363,133 @@
 on(s
-i
+electedI
 con);%0A
+%0A
     
-t2.setSelectedIcon(sicon
+JRadioButton r1 = new JRadioButton(%22RadioButton1%22);%0A%0A    JRadioButton r2 = new JRadioButton(%22RadioButton2%22
 );%0A%0A
@@ -1661,74 +1661,14 @@
 .of(
-new JRadioButton(%22RadioButton1%22), new JRadioButton(%22RadioButton2%22)
+r1, r2
 , t1
@@ -1961,24 +1961,24 @@
  5, 5, 5));%0A
-
     setPrefe
@@ -2017,16 +2017,434 @@
 );%0A  %7D%0A%0A
+  private static Image makeMissingImage() %7B%0A    Icon missingIcon = UIManager.getIcon(%22OptionPane.errorIcon%22);%0A    int iw = missingIcon.getIconWidth();%0A    int ih = missingIcon.getIconHeight();%0A    BufferedImage bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);%0A    Graphics2D g2 = bi.createGraphics();%0A    missingIcon.paintIcon(null, g2, (32 - iw) / 2, (32 - ih) / 2);%0A    g2.dispose();%0A    return bi;%0A  %7D%0A%0A
   public

argument 'getClass().getResource(...)' might be null