
import java.util.*;
import java.awt.*;
import java.applet.Applet;

public class PottsGibbs extends Applet implements Runnable {
    int frameNumber = -1;
    int delay;
    Thread animatorThread;
    boolean frozen = false;
    Color[] color;
    Random rng = new Random();
    int squareWidth = 1;
    int m;
    int n;
    int[][] graph;
    double beta;

    public String getAppletInfo() {
        String info = "PottsGibbs, Version 0.0\n" +
            "Author: Charles Geyer (charlie@stat.umn.edu)\n" +
            "  some code derived from the Java Tutorial and\n" +
            "  Copyright 1995, 1996 by Sun Microsystems\n" +
            "  other code derived from Java in a Nutshell and\n" +
            "  Copyright 1996 by O'Reilly & Associates\n" +
            "  the rest is public domain";
        return info;
    }

    public String[][] getParameterInfo() {
        String[][] info = {
            // Parameter Name   Type or Range   Description
            {"fps",	"integer",	"frames per second"},
            {"ncolor",	"integer",	"number of colors"},
            {"color1",	"hexadecimal integer",	"1st color"},
            {"colorn",	"hexadecimal integer",	"n-th color"},
            {"squareWidth",	"integer",	"number of browser pixels per side of Potts model pixel"}
        };
        return info;
    }

    protected Color getColorParameter(String name) {
        String value = getParameter(name);
        int intvalue = -1;
        try {
            intvalue = Integer.parseInt(value, 16);
        } catch (NumberFormatException e) {}
	if (intvalue == -1) {
            return new Color(rng.nextFloat(), rng.nextFloat(), rng.nextFloat());
	} else {
            return new Color(intvalue);
        }
    }

    public void init() {
        String str;
        int fps = 10;
	int ncolor = 0;

        //How many milliseconds between frames?
        str = getParameter("fps");
        try {
            if (str != null) {
                fps = Integer.parseInt(str);
            }
        } catch (NumberFormatException e) {}
        delay = (fps > 0) ? (1000 / fps) : 100;

        //Number of colors?
        str = getParameter("ncolor");
        try {
            if (str != null) {
                ncolor = Integer.parseInt(str);
            }
        } catch (NumberFormatException e) {}

        //Number of pixels per pixel?
        str = getParameter("squareWidth");
        try {
            if (str != null) {
                squareWidth = Integer.parseInt(str);
            }
        } catch (NumberFormatException e) {}

	//Default: two colors, black and white.
	if (ncolor <= 0) {
            ncolor = 2;
            color = new Color[2];
            color[0] = Color.black;
            color[1] = Color.white;
        } else {
            color = new Color[ncolor];
            for (int i=0; i<ncolor; i++) {
                color[i] = getColorParameter("color" + (i + 1));
            }
        }

	Dimension d = this.size();
	m = d.width / squareWidth;
	n = d.height / squareWidth;

        beta = Math.log(1.0 + Math.sqrt(ncolor));

        System.out.println("number of colors = " + ncolor);
        for (int i=0; i<ncolor; i++) {
            System.out.println("color" + (i + 1) + " = " + color[i]);
        }
        System.out.println("width of pixels = " + squareWidth);
        System.out.println("m = " + m);
        System.out.println("n = " + n);
        System.out.println("beta = " + beta);
        System.out.println("delay = " + delay + " (milliseconds)");

        graph = new int[m][n];
	for (int i=0; i<m; i++) {
	    for (int j=0; j<n; j++) {
                graph[i][j] = 0;
            }
        }
    }

    public void start() {
        if (frozen) { 
            //Do nothing.  The user has requested that we 
            //stop changing the image.
        } else {
            //Start animating!
            if (animatorThread == null) {
                animatorThread = new Thread(this);
            }
            animatorThread.start();
        }
    }

    public void stop() {
        //Stop the animating thread.
        animatorThread = null;
    }

    public boolean mouseDown(Event e, int x, int y) {
        if (frozen) {
            frozen = false;
            start();
        } else {
            frozen = true;
            stop();
        }
        return true;
    }

    public void run() {
        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        //Remember the starting time.
        long startTime = System.currentTimeMillis();

        //This is the animation loop.
        while (Thread.currentThread() == animatorThread) {
            //Advance the animation frame.
            frameNumber++;

            //Display it.
            repaint();

            //Delay depending on how far we are behind.
            try {
                startTime += delay;
                Thread.sleep(Math.max(0, 
                                      startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    //Draw the current frame of animation.
    public void paint(Graphics g) {
	for (int i=0; i<m; i++) {
	    for (int j=0; j<n; j++) {
                g.setColor(color[graph[i][j]]);
                g.fillRect(squareWidth * i, squareWidth * j, squareWidth, squareWidth);
            }
        }
    }

    //Update the current frame of animation.
    public void update(Graphics g) {
        int i = (int) (rng.nextFloat() * m);
        int j = (int) (rng.nextFloat() * n);
        int[] neighborColor;
        neighborColor = new int[color.length];

	for (int k=0; k<color.length; k++)
            neighborColor[k] = 0;

        if (i - 1 >= 0)
           neighborColor[graph[i-1][j]]++;
        if (i + 1 < m)
           neighborColor[graph[i+1][j]]++;
        if (j - 1 >= 0)
           neighborColor[graph[i][j-1]]++;
        if (j + 1 < n)
           neighborColor[graph[i][j+1]]++;

        double[] probs;
        probs = new double[color.length];
	double sumprobs = 0.0;
	for (int k=0; k<color.length; k++) {
		probs[k] = Math.exp(beta * neighborColor[k]);
                sumprobs += probs[k];
        }
        double[] cumprobs;
        cumprobs = new double[color.length];
	for (int k=0; k<color.length; k++) {
            probs[k] /= sumprobs;
            if (k == 0)
                cumprobs[k] = probs[k];
            else
                cumprobs[k] = cumprobs[k-1] + probs[k];
        }
        double u = rng.nextDouble();
        int k;
	for (k=0; k<color.length; k++) {
            if (u <= cumprobs[k]) break;
            if (k == color.length - 1) break;
        }
        graph[i][j] = k;

/*
        System.out.println("Updating pixel (" + i + "," + j + ")");
        if (i - 1 >= 0)
           System.out.println("-/  neighbor color : " + graph[i-1][j]);
        if (i + 1 < m)
           System.out.println("+/  neighbor color : " + graph[i+1][j]);
        if (j - 1 >= 0)
           System.out.println(" /- neighbor color : " + graph[i][j-1]);
        if (j + 1 < n)
           System.out.println(" /+ neighbor color : " + graph[i][j+1]);
	for (int l=0; l<color.length; l++)
            System.out.println("neighbor color[" + l + "] = " + neighborColor[l] + ", probs[" + l + "] = " + probs[l]);
        System.out.println("u = " + u);
        System.out.println("new color = " + graph[i][j]);
*/

        g.setColor(color[graph[i][j]]);
        g.fillRect(squareWidth * i, squareWidth * j, squareWidth, squareWidth);
    }
}

