// -*- 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 image = new ImageIcon(getClass().getResource("16x16.png"));
    JLabel label1 = new JLabel(image);
    JTextField field1 = new JTextField("1111111111111111") {
      @Override public void updateUI() {
        super.updateUI();
        add(label1);
      }
    };

    int w = image.getIconWidth();
    Insets m = field1.getMargin();
    field1.setMargin(new Insets(m.top, m.left + w, m.bottom, m.right));
    label1.setCursor(Cursor.getDefaultCursor());
    label1.setBorder(BorderFactory.createEmptyBorder());
    label1.setBounds(m.left, m.top, w, image.getIconHeight());

    JLabel label2 = new JLabel(image);
    label2.setCursor(Cursor.getDefaultCursor());
    label2.setBorder(BorderFactory.createEmptyBorder());
    JTextField field2 = new JTextField("2222222222222222222222222222222222222") {
      @Override public void updateUI() {
        super.updateUI();
        removeAll();
        SpringLayout l = new SpringLayout();
        setLayout(l);
        Spring fw = l.getConstraint(SpringLayout.WIDTH, this);
        Spring fh = l.getConstraint(SpringLayout.HEIGHT, this);
        SpringLayout.Constraints c = l.getConstraints(label2);
        c.setConstraint(SpringLayout.WEST, fw);
        c.setConstraint(SpringLayout.SOUTH, fh);
        add(label2);
      }
    };
    m = field2.getMargin();
    field2.setMargin(new Insets(m.top + 2, m.left, m.bottom, m.right + w));

    Box box = Box.createVerticalBox();
    box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    box.add(makeTitledPanel("Default", new JTextField("000000000000")));
    box.add(Box.createVerticalStrut(5));
    box.add(makeTitledPanel("add Image(JLabel)", field1));
    box.add(Box.createVerticalStrut(5));
    box.add(makeTitledPanel("SpringLayout", field2));

    add(box, BorderLayout.NORTH);
    setPreferredSize(new Dimension(320, 240));
  }

  private static Component makeTitledPanel(String title, Component c) {
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createTitledBorder(title));
    p.add(c);
    return p;
  }

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

@@ -128,192 +128,698 @@
 java
-x.swing.*;%0A%0Apublic final class MainPanel extends JPanel %7B%0A  private MainPanel() %7B%0A    super(new BorderLayout());%0A    ImageIcon image = new ImageIcon(getClass().getResource(%2216x16.png%22)
+.awt.image.BufferedImage;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Optional;%0Aimport javax.imageio.ImageIO;%0Aimport javax.swing.*;%0A%0Apublic final class MainPanel extends JPanel %7B%0A  private MainPanel() %7B%0A    super(new BorderLayout());%0A    String path = %22example/16x16.png%22;%0A    ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    BufferedImage 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%0A    Icon icon = new ImageIcon(image
 );%0A 
@@ -845,28 +845,27 @@
 new JLabel(i
-mage
+con
 );%0A    JText
@@ -1033,20 +1033,19 @@
 nt w = i
-mage
+con
 .getIcon
@@ -1306,20 +1306,19 @@
 op, w, i
-mage
+con
 .getIcon
@@ -1361,20 +1361,19 @@
 JLabel(i
-mage
+con
 );%0A    l
@@ -2806,16 +2806,429 @@
 p;%0A  %7D%0A%0A
+  private static BufferedImage makeMissingImage() %7B%0A    Icon missingIcon = UIManager.getIcon(%22html.missingImage%22);%0A    int w = missingIcon.getIconWidth();%0A    int h = missingIcon.getIconHeight();%0A    BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);%0A    Graphics2D g2 = bi.createGraphics();%0A    missingIcon.paintIcon(null, g2, 8 - w / 2, 8 - h / 2);%0A    g2.dispose();%0A    return bi;%0A  %7D%0A%0A
   public

use try-with-resources