Polymorphism

Recall that the JOptionPane.showMessageDialog calls for an Icon argument.

A Variable of Interface Type

.

Polymorphism

Benefits of Polymorphism

Drawing Shapes

See: Graphics2D tutorial

Drawing Rectangles and Ellipses

Drawing Ellipses

.

Drawing Line Segments

Relationship Between Shape Classes

.

Drawing Text

.

Filling Shapes

.

CarIcon.java


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

/**
   An icon that has the shape of a car.
*/
public class CarIcon implements Icon
{
   /**
      Constructs 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);
   }

   private int width;

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



IconTest.java


import javax.swing.*;

public class IconTest
{
   public static void main(String[] args)
   {
      JOptionPane.showMessageDialog(
         null, 
         "Hello, Car!",
         "Message",
         JOptionPane.INFORMATION_MESSAGE,
         new CarIcon(100));
      System.exit(0);
   }
}

Results


Maintained by John Loomis, updated Thu Feb 22 22:34:44 2007