Shapes2.java





public class Shapes2 extends JApplet {
  JTextArea  Xout  = new JTextArea(20, 50);

  public Shape randShape() {
    switch((int)(Math.random() * 3)) {
      default:
      case 0: return new Circle();
      case 1: return new Square();
      case 2: return new Triangle();
    }
  }
  public void init() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(new JLabel("Shapes2"));
    cp.add(new JScrollPane(Xout));

    Shape[] s = new Shape[9];
   // Fill up the array with shapes:
    for(int i = 0; i < s.length; i++)
      s[i] = randShape();
    // Make polymorphic method calls:
    for(int i = 0; i < s.length; i++){
      s[i].draw();
      s[i].erase();
    }
  }

class Shape { 
  void draw() {
      Xout.append("Generic.draw()\n");
  }
  void erase() {
      Xout.append("Generic.erase()\n");
  } 
}

class Circle extends Shape {
  void draw() { 
    Xout.append("Circle calling super.draw():-> "); 
    super.draw();
  }
}

class Square extends Shape {
  void draw() { 
    Xout.append("Square.draw()\n"); 
  }
  void erase() { 
    Xout.append("Square.erase()\n"); 
  }
}

class Triangle extends Shape {
  void draw() { 
    Xout.append("Triangle.draw()\n"); 
  }
  void erase() { 
    Xout.append("Triangle.erase()\n");
  }
}



  public static void main(String[] args) {
        Console.run(new Shapes2(), 600, 500);
  }

} ///:
} ///: