title: How to add Icon to JPanel
body: <p>So this is going to be very basic, but I can't figure it out. I have a CarIcon, which implements an Icon interface and a Resizable interface (I'll be implementing that later) and I have everything set up, but I can't figure out how to add my icon in to the JPanel or JFrame from main. I mean, I don't have the Component and Graphics information in order to call paintIcon method in my CarIcon class, so what do I do?</p>
  
  <p>Here's some relevant code:</p>
  
  <p>CarIcon</p>
  
  <pre><code>import java.awt.*;
  import java.awt.geom.*;
  
  public class CarIcon implements Icon, Resizable{
  
      private int width;
      /**
       * Construct a car of a given width.
       * @param width: the width of the car
       */
      public CarIcon(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;
          Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6, width -1, width / 6);
          Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3, width / 6, width /6);
          Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3, width / 6, width / 6);
  
          // The bottom of the front windshield
          Point2D.Double r1 = new Point2D.Double(x + width / 6, y + width / 6);
          // The front of the roof
          Point2D.Double r2 = new Point2D.Double(x + width / 3, y);
          // The rear of the roof
          Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y);
          // The bottom of the rear windshield
          Point2D.Double r4 = new Point2D.Double(x + width * 5 /6, y + width / 6);
  
          Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
          Line2D.Double roofTop = new Line2D.Double(r2, r3);
          Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
  
          g2.fill(frontTire);
          g2.fill(rearTire);
          g2.setColor(Color.RED);
          g2.fill(body);
          g2.draw(frontWindshield);
          g2.draw(roofTop);
          g2.draw(rearWindshield);
  
      }
      @Override
      public void resize(int y) {
          // TODO Auto-generated method stub
          width += y;
      }
      @Override
      public void setIconWidth(int x) {
          // TODO Auto-generated method stub
          width = x;
      }
  
  }
  </code></pre>
  
  <p>SliderTester (main)</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  
  public class SliderTester extends JPanel{
  
      private static final int DEFAULT_WIDTH = 400;
      private static final int DEFAULT_HEIGHT = 200;
      final static Icon myCar = new CarIcon(20);
      final static JPanel panel = new JPanel();
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          JFrame frame = new JFrame();
          frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setVisible(true);
      }
  
  
  }
  </code></pre>
  
  <p>Thanks for your help in advance and Happy Easter!</p>
  
  <p>The following won't work:</p>
  
  <pre><code>final static Icon myCar = new CarIcon(20);
  final static JLabel label = new JLabel(myCar);
  </code></pre>
  
  <p>Compiler says "The constructor JLabel(Icon) is undefined"</p>
  
  <p>Another edit:
  Can there be something wrong with my Eclipse? Here is a screen shot where I take your code and paste it in my program and it throws an error:</p>
  
  <p><img src="https://i.stack.imgur.com/9Tbzy.png" alt="enter image description here"></p>