/* inoffizielle lösung zum awt-bsp vom ue-test am 4.3.02 */

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

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

class MyCanvas extends Canvas {

    float platz, einheit, rand, start_x, start_y, anfang_x;
    int anzahl, bogen, startwinkel;

    MyCanvas() {
        setSize(500,500);
        anzahl = 5;
    }

    public void paint(Graphics g) {
        bogen = 60;
        platz = 500 / anzahl;
        einheit = platz / 10;
        rand = (500 - anzahl * platz) / 2;
        einheit /= 2;                           // zuerst / und dann wieder * ist leider nötig, weil 1.5 * einheit = loss of precision
        anfang_x = start_x = start_y = 3 * einheit;
        einheit *= 2;

        for(int z=0; z<anzahl; z++) {
            for(int s=0; s<anzahl; s++) {
                startwinkel = 0;

                if((z+s)%2 == 0) {
                    for(int i=0; i<3; i++) {
                       g.fillArc(Math.round(start_x), Math.round(start_y), Math.round(6*einheit), Math.round(6*einheit), startwinkel, bogen);
                        startwinkel += 120;
                    }
                }
                else {
                    startwinkel = 60;
                    for(int i=0; i<3; i++) {
                       g.fillArc(Math.round(start_x), Math.round(start_y), Math.round(6*einheit), Math.round(6*einheit), startwinkel, bogen);
                        startwinkel += 120;
                    }
                }

                start_x += platz;
            }

            start_x = anfang_x;
            start_y += platz;
        }
    }

    void neupaint(int anzahl) {
        this.anzahl = anzahl;
        repaint();
    }
}

public class radioaktiv extends Frame implements ActionListener{

    TextField text;
    MyCanvas canvas;

    public radioaktiv() {
        super("Dreiecke");
        setBackground(Color.yellow);
        addWindowListener(new WindowClosingAdapter());
        setLayout(new BorderLayout());

        canvas = new MyCanvas();
        text = new TextField();
        text.addActionListener(this);

        add("Center", canvas);
        add("South", text);

        pack();
        show();
    }

    public static void main(String args[]) {
        new radioaktiv();
    }

    public void actionPerformed(ActionEvent event) {
        try {
            int zahl = Integer.parseInt(text.getText());

            if(zahl == 0) {
                System.out.println("Eine Eingabe von 0 ist nicht wirklich sinnvoll!");
            }
            else {
                canvas.neupaint(zahl);
                text.setText("");
            }
        }
        catch (Exception brauchined) {
            System.out.println("Sie müssen eine Zahl eingeben!");
        }
    }
}

