Icon Interface

An icon is a small fixed size picture, typically used to decorate components.


The Icon Interface Type

public interface Icon
{
int getIconWidth();
int getIconHeight();
void paintComponent(Component c, Graphics g, int x, int y)
}

Icon implementations may use the Component argument to get properties useful for painting, e.g. the foreground or background color.

Interface Types

The Icon Interface Type and Implementing Classes

.

MarsIcon.java


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

/**
   An icon that has the shape of the planet Mars.
*/
public class MarsIcon implements Icon
{
   /**
      Constructs a Mars icon of a given size.
      @param aSize the size of the icon
   */
   public MarsIcon(int aSize)
   {
      size = aSize;
   }

   public int getIconWidth()
   {
      return size;
   }

   public int getIconHeight()
   {
      return size;
   }

   public void paintIcon(Component c, Graphics g, int x, int y)
   {
      Graphics2D g2 = (Graphics2D) g;
      Ellipse2D.Double planet = new Ellipse2D.Double(x, y,
         size, size);
      g2.setColor(Color.RED);
      g2.fill(planet);
   }

   public static void main(String[] args)
   {
       JOptionPane.showMessageDialog(
		null, 
		"Hello, Mars!",
		"Message",
		JOptionPane.INFORMATION_MESSAGE,
		new MarsIcon(50));
       System.exit(0);
   }

   private int size;
}

Results


Maintained by John Loomis, updated Thu Feb 22 22:12:05 2007