/* AWT Zielscheibe: Inoffizielle Ausarbeitung von Andreas Reichard */

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

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

class MyCanvas extends Canvas {
    int anzahl;
    float breite, pos_x, pos_y, dm, rand_links, rand_oben;
    
    MyCanvas() {
        setSize(400, 400);
        anzahl = 6;        
    }    
    
    public void paint(Graphics g) {
        pos_x = pos_y = rand_links = rand_oben = 0;
        
        if(getSize().width < getSize().height) {
            dm = getSize().width;
            rand_oben = (getSize().height - dm) / 2;
        }
        else if(getSize().width > getSize().height) {
            dm = getSize().height;
            rand_links = (getSize().width - dm) / 2;            
        }   
        else {
            dm = getSize().height; // canvashöhe und -breite sind gleich, d.h. egal, ob man als durchmesser des äußersten kreises width oder height nimmt
        }    
        
        breite = dm / (4 * anzahl - 1);
        
        for(int i=0; i<anzahl; i++) {
            g.setColor(Color.red);
            g.fillOval(Math.round(pos_x + rand_links), Math.round(pos_y + rand_oben), Math.round(dm), Math.round(dm));
            
            pos_x += breite;
            pos_y += breite;
            dm -= breite*2;
            
            g.setColor(Color.white);
            g.fillOval(Math.round(pos_x + rand_links), Math.round(pos_y + rand_oben), Math.round(dm), Math.round(dm));
            
            pos_x += breite;
            pos_y += breite;
            dm -= breite*2;
        }                    
    } 
    
    public void neupaint(int anzahl) {
        this.anzahl = anzahl;
        repaint();
    }    
}    

public class zielscheibe extends Frame implements ActionListener {

    Button mehr, weniger;
    MyCanvas canvas;    
    Panel panel;
    
    public zielscheibe() {
        super("AWT Zielscheibe");
        addWindowListener(new WindowClosingAdapter());
        setLayout(new BorderLayout());
        
        canvas = new MyCanvas();
        add("Center", canvas);
        
        mehr = new Button("mehr");
        mehr.addActionListener(this);
        weniger = new Button("weniger");
        weniger.addActionListener(this);
        
        panel = new Panel();
        panel.setLayout(new GridLayout(2,1));
        panel.add(mehr);
        panel.add(weniger);
        add("East", panel);
        
        pack();
        show();        
    }
    
    public static void main(String[] nix) {
        new zielscheibe();
    }    
    
    public void actionPerformed(ActionEvent event) {
        String source = event.getActionCommand();
        int anzahl, zz = (int) (Math.random()*2 + 1);        
        
        if(source.equals("mehr")) {
            anzahl = canvas.anzahl + zz;
            
            if(anzahl>13) {
                anzahl = 13;
            }    
        }    
        else {
            anzahl = canvas.anzahl - zz;
            
            if(anzahl<2) {
                anzahl = 2;
            }
        }
        
        canvas.neupaint(anzahl);
    }
    
}

