/*
 *
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
import java.lang.Object;
import java.lang.Object.*;

/**
 */
public
class unordnung extends Applet implements MouseListener {
    /**
     * Dubble Buffering
     */
     private Image dbImage;
     private Graphics dbg;

    /**
     * TEMP
     */
    int text;

    /**
     * Zustandsvariablen
     */
    int curcol = 0;
    int radius = 10;

    /**
     * Farben definieren (Grau für leer)
     */
    Color Farbe[] = {Color.yellow, Color.blue, Color.orange, Color.red, Color.green, Color.gray, Color.black, Color.black};

    /**
     * Spielfeld 
     */
    int Feld[][] = { {7,7,7,0,7,7,7},
                     {7,7,0,0,7,7},
                     {0,0,0,0,0,0,0},
                     {0,0,0,0,0,0},
                     {7,0,0,7,0,0,7},
                     {0,0,0,0,0,0},
                     {0,0,0,0,0,0,0},
                     {7,7,0,0,7,7},
                     {7,7,7,0,7,7,7} };
    int Gewichte[][] = { {7,7,7,0,7,7,7},
                     {7,7,0,0,7,7},
                     {0,0,0,0,0,0,0},
                     {0,0,0,0,0,0},
                     {7,0,0,7,0,0,7},
                     {0,0,0,0,0,0},
                     {0,0,0,0,0,0,0},
                     {7,7,0,0,7,7},
                     {7,7,7,0,7,7,7} };
    int MaxUnord[][] = { {7,7,7,2,7,7,7},
                     {7,7,4,4,7,7},
                     {2,4,5,6,5,4,2},
                     {4,6,5,5,6,4},
                     {7,5,5,7,5,5,7},
                     {4,6,5,5,6,4},
                     {2,4,5,6,5,4,2},
                     {7,7,4,4,7,7},
                     {7,7,7,2,7,7,7} };
    int Gesamtunordnung=0;

    /**
     * Wichtige Punkte des Spielfeldes
     */
    int kantenl=36;
    int khalb=(int) kantenl/2;
    int tw; // Halbe Teilweite eines Spielfeldteils
    int th; // Höhe eines Spielfeldteils

    /**
     * Initialize the applet. Resize and load images.
     */
    public void init() {
	myinit();
	addMouseListener(this);
    }

    public void myinit() {
	// Größen des Applets auslesen
	//int x = (int) (getSize().width);
	//int y = (int) (getSize().height);

	tw = (int)(280/9); //halbe Teilweite
        th = (int)(320/6);  //Teilhoehe
    }

    public void unordnungberechnen() {
	Gesamtunordnung=0;
	for (int i=0;i<9;i++) { //Zeile 
	  int oddrow=i-2*(int)(i/2);
	  for (int j=0;j<7-oddrow;j++) { //Spalte
	    int thiscol=Feld[i][j];
	    int unordnung=0;
	    int rowmax=7-oddrow;
	    int rowmaxu=6+oddrow;
	    //Feld oben links
	    if (((i-1)>=0) && ((j-1+oddrow)>=0) && ((j-1+oddrow)<rowmax)) {
	      if (Feld[i-1][j-1+oddrow]==thiscol) {unordnung++;}
	    }
	    //Feld oben rechts
	    if (((i-1)>=0) && ((j+oddrow)<rowmaxu)) {
	      if (Feld[i-1][j+oddrow]==thiscol) {unordnung++;}
	    }
	    //Feld links
	    if ((j-1)>=0) {
	      if (Feld[i][j-1]==thiscol) {unordnung++;}
	    }
	    //Feld rechts
	    if ((j+1)<rowmax) {
	      if (Feld[i][j+1]==thiscol) {unordnung++;}
	    }
	    //Feld unten links
	    if (((i+1)<9) && ((j-1+oddrow)>=0) && ((j-1+oddrow)<rowmax)) {
	      if (Feld[i+1][j-1+oddrow]==thiscol) {unordnung++;}
	    }
	    //Feld unten rechts
	    if (((i+1)<9) && ((j+oddrow)<rowmaxu)) {
	      if (Feld[i+1][j+oddrow]==thiscol) {unordnung++;}
	    }
	    Gewichte[i][j]=MaxUnord[i][j]-unordnung;
	    if (Feld[i][j]!=7) {Gesamtunordnung+=Gewichte[i][j];}
	  }
	}  

    }

    public void destroy() {
        removeMouseListener(this);
    }

    /**
     * Paint it.
     */
    public void paint(Graphics g) {
	
	Dimension d = getSize();
	g.setColor(Color.black);
	g.fillRect(60,60,340,340);
        g.setColor(Color.orange);
        g.fill3DRect(75, 20, 140, 20, true);  //Gesamtunordnung
        g.setColor(Color.black);
        g.drawString("Gesamtunordnung: "+Gesamtunordnung, 77, 38);

	//Grundfarben am Rand zeichnen
	int currad=20;
	for (int i=0;i<6;i++) {
	  g.setColor(Farbe[i]);
	  if (i==curcol) {
	    g.fillOval(10,65+55*i,currad+10,currad+10);
	  } else {
	    g.fillOval(15,70+55*i,currad,currad);
	  }
	}
	//Feld zeichnen
	for (int i=0;i<9;i++) { //Zeilennummern
	  int oddrow=i-2*(int)(i/2);
	  for (int j=0;j<7-oddrow;j++) { //Spaltennummern
	    //Farbe setzen  
	    g.setColor(Farbe[Feld[i][j]]);
	    g.fillOval(60+(int)(j*kantenl+oddrow*kantenl/2),60+i*kantenl,kantenl,kantenl);
	    g.setColor(Color.black);
	    g.drawString(""+Gewichte[i][j],60+khalb+(int)(j*kantenl+oddrow*kantenl/2),60+khalb+i*kantenl);
	  }
	}
    }

    /** Update - Method, implements double buffering */
    public void update (Graphics g)
    {
    // initialize buffer
    if (dbImage == null)
    {
    dbImage = createImage (this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics ();
    
    }
    
    // clear screen in background
    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    // draw elements in background
    dbg.setColor (getForeground());
    paint (dbg);
    
    // draw image on the screen
    g.drawImage (dbImage, 0, 0, this);
    
    }
    
    /**
     * The user has clicked in the applet. Figure out where
     * and see if a legal move is possible. If it is a legal
     * move, respond with a legal move (if possible).
     */
    public void mouseReleased(MouseEvent e) {
	int x = e.getX();
	int y = e.getY();

	if ((x < 50) && (y < 400) && (e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
	  int clickedteil = (int) (y-65)/55;
	  curcol = clickedteil;
	  repaint();
	} else if (((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) ) {
	  //rechte Maustaste - Farbe auf null setzen?
	  int clickedzeile = (int) (y-60)/kantenl;
	  int clickedspalte = 0;
	  int oddzeile = clickedzeile-2*(int)(clickedzeile/2);
	  if (oddzeile==1) {
	    clickedspalte = (int) (x-60-(kantenl/2))/kantenl;
	  } else {
	    clickedspalte = (int) (x-60)/kantenl;
	  }
	  if (Feld[clickedzeile][clickedspalte]!=7) {Feld[clickedzeile][clickedspalte]=0;}
	  unordnungberechnen();
	  repaint();
	} else if (((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) ) {
	  //linke - Maustaste
	  int clickedzeile = (int) (y-60)/kantenl;
	  int clickedspalte = 0;
	  int oddzeile = clickedzeile-2*(int)(clickedzeile/2);
	  if (oddzeile==1) {
	    clickedspalte = (int) (x-60-(kantenl/2))/kantenl;
	  } else {
	    clickedspalte = (int) (x-60)/kantenl;
	  }
	  if (Feld[clickedzeile][clickedspalte]!=7) {Feld[clickedzeile][clickedspalte]=curcol;}
	  unordnungberechnen();
	  repaint();
        } else if ((x > 235) && (x < 285) && (y > 20) && (y < 40)) {
	  // Autofit ein und aus schalten
	  repaint();
	} else if ((x > 155) && (x < 215) && (y > 20) && (y < 40)) {
	  // Testen
          repaint();
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public String getAppletInfo() {
	return "Ein neuer Versuch";
    }
}

