import java.awt.*;
import java.awt.event.*;

enum ZeichenModus {LINIE, RECHTECK, ELLIPSE};

class Figur{
    int x1, y1, x2, y2;
    int x, y, w, h;
    ZeichenModus zModus;
    boolean gefüllt;
    Figur(ZeichenModus zm, boolean g, int px1, int py1, int px2, int py2){
        zModus = zm; gefüllt = g;
        x1 = px1; y1 = py1; x2 = px2; y2 = py2;
        w = Math.abs(px1 - px2);
        h = Math.abs(py1 - py2);
        x = Math.min(px1, px2);
        y = Math.min(py1, py2);
        }
        
    void draw(Graphics g){    
        if (zModus == ZeichenModus.LINIE) g.drawLine(x1, y1, x2, y2);
        else if (zModus == ZeichenModus.RECHTECK) 
          if (gefüllt)g.fillRect(x, y, w, h); else g.drawRect(x, y, w, h);
        else if (zModus == ZeichenModus.ELLIPSE)
          if (gefüllt)g.fillOval(x, y, w, h); else g.drawOval(x, y, w, h);
        }    
}

class GeoListenElement{
    Figur figur;
    GeoListenElement  next;
    GeoListenElement(Figur f){figur = f; next = null;}
    GeoListenElement(Figur f, GeoListenElement n){figur = f; next = n;}
}

class GeoListe{
    private GeoListenElement anfang, ende;
    GeoListe(){}
    
    void addFigur(Figur f){
        GeoListenElement gl = new GeoListenElement(f);
        if (anfang == null) ende = anfang = gl;
        else {
            ende.next = gl;
            ende = gl;
            }
        }
    
    void drawListe(Graphics g){
       for (GeoListenElement gl = anfang; gl != null; gl = gl.next) 
         gl.figur.draw(g);
       }
}

class GrafikFenster extends Frame{

    private static final long serialVersionUID = 7526471155622776147L;
    private static Button stopButton     = new Button("Stop Button");
    private static Button linieButton    = new Button("Linien malen");
    private static Button RechteckButton = new Button("Rechtecke malen");
    private static Button ellipsenButton = new Button("Ellipsen malen");
    private static Button fillButton = new Button("Gefüllt ?");
   
    private ZeichenModus zModus = ZeichenModus.LINIE;
    private boolean gefüllt = false;
    private int x1;
    private int y1;
    
    private GeoListe geoListe = new GeoListe();
    

    GrafikFenster(String title, int breite, int höhe){	
      super(title);
      setLayout(new FlowLayout());
      
      stopButton.addActionListener(new ActionListener() {    
	        public void actionPerformed(ActionEvent event) { fensterBeenden(); }
		    });
      add(stopButton);
      
      linieButton.addActionListener(new ActionListener() {    
	        public void actionPerformed(ActionEvent event) { zModus = ZeichenModus.LINIE; }
		    });
		  add(linieButton );

      RechteckButton.addActionListener(new ActionListener() {    
	        public void actionPerformed(ActionEvent event) { zModus = ZeichenModus.RECHTECK; }
		    });
		  add(RechteckButton );

      ellipsenButton.addActionListener(new ActionListener() {    
	        public void actionPerformed(ActionEvent event) { zModus = ZeichenModus.ELLIPSE; }
		    });
		  add(ellipsenButton );
		  
		  fillButton.addActionListener(new ActionListener() {    
	        public void actionPerformed(ActionEvent event) { gefüllt = !gefüllt; }
		    });
		  add(fillButton );

      addMouseListener(new MouseAdapter() {    
        public void mousePressed(MouseEvent e){ x1 = e.getX(); y1 = e.getY(); }
	      public void mouseReleased(MouseEvent e){
           int x2 = e.getX(); int y2 = e.getY();
           Graphics g = getGraphics();
           g.setColor(Color.blue);
           Figur f = new Figur(zModus, gefüllt, x1, y1, x2, y2);
           f.draw(g);
           geoListe.addFigur(f);
           }
	    });
           
      addWindowListener(new WindowAdapter() {    
        public void windowClosing(WindowEvent e) { fensterBeenden(); }
	    });
	    
      setBackground(Color.lightGray);
      setSize(breite, höhe);
	    setVisible(true);
      }

    public void paint(Graphics g) {
      g.setColor(Color.red);  
	    geoListe.drawListe(g);
	    }	

    void fensterBeenden() {		        
	  dispose();
	  }	  
}

class FensterTester {

    public static void main(String args[]) {
        GrafikFenster mainFenster = new GrafikFenster("Fenstermaler",800,600);
        }
}

