1  import java.awt.*;
  2  import javax.swing.*;
  3  
  4  /**
  5     An adapter that turns an icon into a JComponent.
  6  */
  7  public class IconAdapter extends JComponent
  8  {
  9     /**
 10        Constructs a JComponent that displays a given icon.
 11        @param icon the icon to display
 12     */
 13     public IconAdapter(Icon icon)
 14     {
 15        this.icon = icon;
 16     }
 17  
 18     public void paintComponent(Graphics g)
 19     {
 20        icon.paintIcon(this, g, 0, 0);
 21     }
 22  
 23     public Dimension getPreferredSize()
 24     {
 25        return new Dimension(icon.getIconWidth(),
 26              icon.getIconHeight());
 27     }
 28  
 29     private Icon icon;
 30  }