ResistorIcon

ResistorIcon.java


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

/**
   An icon that has the shape of a resistor.
*/
public class ResistorIcon implements Icon
{
   /**
      Constructs a resistor of a given width.
      @param width the width of the car
   */
   public ResistorIcon(int aWidth)
   {
      width = aWidth;
   }
   
   public int getIconWidth()
   {
      return width;
   }

   public int getIconHeight()
   {
      return width / 2;
   }

   public void paintIcon(Component c, Graphics g, int x, int y)
   {
       Graphics2D g2 = (Graphics2D) g;

      double w = 2.5/7.0;
      double h = 1.25;

      Path2D.Double path = new Path2D.Double(Path2D.WIND_NON_ZERO,10);
      
      path.moveTo(-7*w,0);
      path.lineTo(-6*w,0);
      path.lineTo(-5*w,h);
      path.lineTo(-3*w,-h);
      path.lineTo(-w,h);
      path.lineTo(w,-h);
      path.lineTo(3*w,h);
      path.lineTo(5*w,-h);
      path.lineTo(6*w,0);
      path.lineTo(7*w,0);
      AffineTransform m = g2.getTransform();
      g2.translate(x,y);
      g2.scale(width/7,width/7);
      g2.translate(2.5,2.2);
      BasicStroke pen = new BasicStroke(0.2f, 
	BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
      g2.setStroke(pen);
      g2.setColor(Color.red);
      g2.draw(path);
      g2.setTransform(m);
      /*
      int i;
      for (i=0; i<npts; i++) {
	  pts[i].scale(0.1).translate(loc);
	  point2::convert(pts[i],cpts[i]);
      }
      */
   }

   private int width;

   public static void main(String[] args)
   {
       JOptionPane.showMessageDialog(
		 null, 
		 "Sample Resistor",
		 "Message",
		 JOptionPane.INFORMATION_MESSAGE,
		 new ResistorIcon(120));
       System.exit(0);
   }
}



Results


Maintained by John Loomis, updated Fri Feb 23 16:55:34 2007