view src/samer/mds/MatrixPointViewer2.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
package samer.mds;

import samer.core.*;
import samer.core.types.*;
import samer.core.util.*;
import samer.maths.*;
import samer.tools.*;
import java.util.*;
import java.awt.*;

public class MatrixPointViewer2 extends samer.core.util.swing.VCanvas implements Task, Observer, Agent
{
	Matrix			P;			// point positions
	Vec			x;			// point activities
	int				N;
	int				k1, k2;
	Color[]		clut;
	VMap			xmap, ymap, cmap;
	VInteger		markerSize;
	int[]			ix, iy;
	
	public MatrixPointViewer2(Matrix P, Vec x) {
		this.P=P;
		this.x=x;
		N=P.getRowDimension();
		k1=0; k2=1;

//		ColorRamp cramp=new ColorRamp(256);
//		cramp.set(0,Color.black);
//		cramp.gradientTo(255,Color.white);
		clut=new Color[256];
		for (int i=0; i<256; i++) clut[i]=new Color(255,255,255,i);

		ix = new int[N];
		iy = new int[N];
		
		markerSize=new VInteger("markerSize",4);
		cmap=new VMap(new LinearMap(256),new Node("cmap"));
		xmap=new VMap(new LinearMap(256),new Node("xmap"));
		ymap=new VMap(new LinearMap(256),new Node("ymap"));
		exposeCommands(this);

		P.viewable().addObserver(this);
		xmap.addObserver(this);
		ymap.addObserver(this);
	}

	public void sized() {
		xmap.getMap().setIntRange(width);
		ymap.getMap().setIntRange(height);
		update(null,null);
	}
	
	public void attach() { P.viewable().addObserver(this); }
	public void detach() {
		P.viewable().deleteObserver(this);
		xmap.dispose();
		ymap.dispose();
		cmap.dispose();
		markerSize.dispose();
	}	

	public void rotate() {
		k2++;
		if (k2>=P.getColumnDimension()) {
			k2=0;	k1++;
			if (k1>=P.getColumnDimension()) k1=0;
		}
		if (k2==k1) rotate();
	}

	public void starting() {}
	public void stopping() {}
	public void run() { repaint(); }
	public void dispose() { detach(); }
	
	public void update(Observable o, Object a)
	{
		double [][] p=P.getArray();
		IMap		xm=xmap.getMap();
		IMap		ym=ymap.getMap();
		
		for (int i=0; i<N; i++) {
			double r[]=p[i];
			ix[i]=xm.toInt(r[k1]);
			iy[i]=ym.toInt(r[k2]);
		}
		repaint();
	}

	public void paint(Graphics g) {
		double [] a=x.array();
		IMap		cm=cmap.getMap();
		int			b=markerSize.value;

		clear(g);
		for (int i=0; i<N; i++) {
			g.setColor(clut[cm.clipInt(a[i])]);
			g.fillRect(ix[i],iy[i],b,b);
		}
	}

	public void getCommands(Registry r) {
		r.add("rotate").add("x-map").add("y-map").add("c-map");
	}
	public void execute(String cmd, Environment env)
	{
		if (cmd.equals("rotate")) { rotate(); update(null,null); }
		else if (cmd.equals("x-map")) Shell.expose(xmap.getViewer(),"x-map");
		else if (cmd.equals("y-map")) Shell.expose(ymap.getViewer(),"y-map");
		else if (cmd.equals("c-map")) Shell.expose(cmap.getViewer(),"c-map");
	}
}