001package horstmann.ch10_adapter; 002import java.awt.Dimension; 003import java.awt.Graphics; 004 005import javax.swing.Icon; 006import javax.swing.JComponent; 007 008/** 009 An adapter that turns an icon into a JComponent. 010 */ 011@SuppressWarnings("serial") 012public class IconAdapter extends JComponent 013{ 014 /** 015 Constructs a JComponent that displays a given icon. 016 @param icon the icon to display 017 */ 018 public IconAdapter(Icon icon) 019 { 020 this.icon = icon; 021 } 022 023 public void paintComponent(Graphics g) 024 { 025 icon.paintIcon(this, g, 0, 0); 026 } 027 028 public Dimension getPreferredSize() 029 { 030 return new Dimension(icon.getIconWidth(), 031 icon.getIconHeight()); 032 } 033 034 private Icon icon; 035}