// -*- 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());
    ImageIcon icon = new ImageIcon(getClass().getResource("duke.running.gif"));

    JLabel label1 = new JLabel(icon);
    label1.setEnabled(false);
    label1.setBorder(BorderFactory.createTitledBorder("Default"));

    JLabel label2 = new JLabel(icon) {
      @Override public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
        int info = infoflags;
        if (!isEnabled()) {
          info &= ~FRAMEBITS;
        }
        return super.imageUpdate(img, info, x, y, w, h);
      }
    };
    label2.setEnabled(false);
    label2.setBorder(BorderFactory.createTitledBorder("Override imageUpdate(...)"));

    JLabel label3 = new JLabel(icon);
    label3.setEnabled(false);
    label3.setBorder(BorderFactory.createTitledBorder("setDisabledIcon"));
    ImageIcon i = new ImageIcon(getClass().getResource("duke.running_frame_0001.gif"));
    label3.setDisabledIcon(makeDisabledIcon(i));

    JCheckBox check = new JCheckBox("setEnabled");
    check.addActionListener(e -> {
      JCheckBox c = (JCheckBox) e.getSource();
      label1.setEnabled(c.isSelected());
      label2.setEnabled(c.isSelected());
      label3.setEnabled(c.isSelected());
    });
    JPanel p = new JPanel(new GridLayout(2, 2));
    p.add(label1);
    p.add(label2);
    p.add(label3);
    add(check, BorderLayout.NORTH);
    add(p);
    // setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
    setPreferredSize(new Dimension(320, 240));
  }

  private static Icon makeDisabledIcon(ImageIcon icon) {
    Image img = icon.getImage();
    // BufferedImage source = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    // Graphics g = source.createGraphics();
    // g.drawImage(img, 0, 0, null);
    // g.dispose();
    // ColorConvertOp cc = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    // BufferedImage destination = cc.filter(source, null);
    // return new ImageIcon(destination);
    return new ImageIcon(GrayFilter.createDisabledImage(img));
  }

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

@@ -113,16 +113,187 @@
 .awt.*;%0A
+import java.awt.image.BufferedImage;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.URL;%0Aimport java.util.Optional;%0Aimport javax.imageio.ImageIO;%0A
 import j
@@ -416,49 +416,89 @@
     
-ImageIcon icon = new ImageIcon(getClass()
+ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    URL url = cl
 .get
@@ -499,32 +499,40 @@
 cl.getResource(%22
+example/
 duke.running.gif
@@ -533,19 +533,150 @@
 ng.gif%22)
-)
 ;%0A
+    Icon icon = Optional.ofNullable(url).%3CIcon%3Emap(ImageIcon::new)%0A        .orElseGet(() -%3E UIManager.getIcon(%22html.missingImage%22));
 %0A    JLa
@@ -1379,89 +1379,329 @@
     
-ImageIcon i = new ImageIcon(getClass().getResource(%22duke.running_frame_0001.gif%22)
+String path = %22example/duke.running_frame_0001.gif%22;%0A    Image image = Optional.ofNullable(cl.getResource(path)).map(u -%3E %7B%0A      try (InputStream s = u.openStream()) %7B%0A        return ImageIO.read(s);%0A      %7D catch (IOException ex) %7B%0A        return makeMissingImage();%0A      %7D%0A    %7D).orElseGet(MainPanel::makeMissingImage
 );%0A 
@@ -1744,16 +1744,20 @@
 edIcon(i
+mage
 ));%0A%0A   
@@ -2312,79 +2312,456 @@
 ic I
-con
+mage
  make
-DisabledIcon(ImageIcon icon) %7B%0A    Image img = icon.getImage();
+MissingImage() %7B%0A    Icon missingIcon = UIManager.getIcon(%22html.missingImage%22);%0A    int iw = missingIcon.getIconWidth();%0A    int ih = missingIcon.getIconHeight();%0A    BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);%0A    Graphics2D g2 = bi.createGraphics();%0A    missingIcon.paintIcon(null, g2, (16 - iw) / 2, (16 - ih) / 2);%0A    g2.dispose();%0A    return bi;%0A  %7D%0A%0A  private static Icon makeDisabledIcon(Image img) %7B
 %0A   

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