import java.awt.*;
import java.awt.event.*;

 /**
 * Title: EP - VO Prüfung 11.03.2002
 * Description: Stapel
 * @author Krikkit<br>
 * {@link <a href="http://krikkit.joeh.org" target="_top">http://krikkit.joeh.org</a> }
 * @version 0.9 <br>
 * This document is copyright (C) 2001 Krikkit, and it's free.
 * You can distribute it under the terms of the GNU General Public License,
 * which you can get at {@link <a href="http://www.gnu.org/copyleft/gpl.html" target="_top">http://www.gnu.org/copyleft/gpl.html</a> }
 */

class WindowClosingAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
        System.exit(0);
    }
}

class MyCanvas extends Canvas {
    int randomb, randomh;
    double schrumpf;
    int canvasbreite, canvashoehe, pos_x, pos_y;

    MyCanvas()
    {
      setSize(400, 400);
      randomb = 200 + (int)(Math.random()*200);
      randomh = 20 + (int)(Math.random()*30);
      schrumpf = ((double)(5 + (int)(Math.random()*4)))/10;
    }

    public void paint(Graphics g)
    {
      canvashoehe = getSize().height;
      canvasbreite = getSize().width;
      pos_x = canvasbreite/2;
      pos_y = canvashoehe;
      int rechtbreite = randomb;
      int rechthoehe = randomh;
      int gesamthoehe=0;

      for(int i=0; gesamthoehe < canvashoehe && rechthoehe > 2; i++)
      {
        g.setColor(Color.red);
        g.fillRect(pos_x-rechtbreite/2,pos_y-rechthoehe,rechtbreite,rechthoehe);
        pos_y -= rechthoehe;
        gesamthoehe += rechthoehe;
        rechtbreite = (int)(rechtbreite*schrumpf);
        rechthoehe = (int)(rechthoehe*schrumpf);
      }

    }

    public void neupaint(int randomb, int randomh, double schrumpf) {
        this.randomb = randomb;
        this.randomh = randomh;
        this.schrumpf = schrumpf;
        repaint();
    }
}

public class stapel extends Frame implements ActionListener {

    Button numoi;
    MyCanvas canvas;

    public stapel() {
        super("Stapel");
        addWindowListener(new WindowClosingAdapter());
        setLayout(new BorderLayout());

        canvas = new MyCanvas();
        add("Center", canvas);

        numoi = new Button("noch einmal");
        numoi.addActionListener(this);
        add("North", numoi);

        pack();
        show();
    }

    public static void main(String[] nix) {
        new stapel();
    }

    public void actionPerformed(ActionEvent event) {
        int anzahl, zz = (int) (Math.random()*2 + 1);
        int randomb, randomh;
        double schrumpf;
        randomb = 200 + (int)(Math.random()*200);
        randomh = 20 + (int)(Math.random()*30);
        schrumpf = ((double)(5 + (int)(Math.random()*4)))/10;

        canvas.neupaint(randomb, randomh, schrumpf);
    }

}
