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

package example;

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

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    Point hotSpot = new Point(16, 16);

    BufferedImage bi1 = makeStringBufferedImage("?");
    JButton label1 = new JButton("?");
    label1.setCursor(getToolkit().createCustomCursor(bi1, hotSpot, "?"));

    BufferedImage bi2 = makeOvalBufferedImage();
    JButton label2 = new JButton("Oval");
    label2.setCursor(getToolkit().createCustomCursor(bi2, hotSpot, "oval"));
    label2.setIcon(new ImageIcon(bi2));

    Icon icon = new GreenBlueIcon();
    BufferedImage bi3 = makeIconBufferedImage(icon);
    JButton label3 = new JButton("Rect");
    label3.setCursor(getToolkit().createCustomCursor(bi3, hotSpot, "rect"));
    label3.setIcon(icon);

    JPanel p = new JPanel(new GridLayout(3, 1, 5, 5));
    p.add(makeTitledPanel("String", label1));
    p.add(makeTitledPanel("drawOval", label2));
    p.add(makeTitledPanel("paintIcon", label3));
    p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    add(p);
    setPreferredSize(new Dimension(320, 240));
  }

  public static BufferedImage makeStringBufferedImage(String str) {
    BufferedImage bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setPaint(Color.BLACK);
    g2.drawString(str, 16, 28);
    g2.dispose();
    return bi;
  }

  private static BufferedImage makeOvalBufferedImage() {
    BufferedImage bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setPaint(Color.RED);
    g2.drawOval(8, 8, 16, 16);
    g2.dispose();
    return bi;
  }

  private static BufferedImage makeIconBufferedImage(Icon icon) {
    int w = icon.getIconWidth();
    int h = icon.getIconHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    icon.paintIcon(null, g2, 0, 0);
    g2.dispose();
    return bi;
  }

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

class GreenBlueIcon implements Icon {
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.translate(x, y);
    g2.setPaint(Color.GREEN);
    g2.fillRect(8, 8, 8, 8);
    g2.setPaint(Color.BLUE);
    g2.fillRect(16, 16, 8, 8);
    g2.dispose();
  }

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

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

@@ -380,21 +380,22 @@
 JButton 
-label
+button
 1 = new 
@@ -412,21 +412,22 @@
 %22);%0A    
-label
+button
 1.setCur
@@ -545,21 +545,22 @@
 JButton 
-label
+button
 2 = new 
@@ -576,29 +576,30 @@
 Oval%22);%0A    
-label
+button
 2.setCursor(
@@ -658,21 +658,22 @@
 ));%0A    
-label
+button
 2.setIco
@@ -798,21 +798,22 @@
 JButton 
-label
+button
 3 = new 
@@ -829,29 +829,30 @@
 Rect%22);%0A    
-label
+button
 3.setCursor(
@@ -911,21 +911,22 @@
 ));%0A    
-label
+button
 3.setIco
@@ -1026,21 +1026,22 @@
 tring%22, 
-label
+button
 1));%0A   
@@ -1075,21 +1075,22 @@
 wOval%22, 
-label
+button
 2));%0A   
@@ -1129,13 +1129,14 @@
 n%22, 
-label
+button
 3));

better variable name