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

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 package samer.mds;
2
3 import samer.core.*;
4 import samer.core.types.*;
5 import samer.core.util.*;
6 import samer.maths.*;
7 import samer.tools.*;
8 import java.util.*;
9 import java.awt.*;
10
11 public class MatrixPointViewer2 extends samer.core.util.swing.VCanvas implements Task, Observer, Agent
12 {
13 Matrix P; // point positions
14 Vec x; // point activities
15 int N;
16 int k1, k2;
17 Color[] clut;
18 VMap xmap, ymap, cmap;
19 VInteger markerSize;
20 int[] ix, iy;
21
22 public MatrixPointViewer2(Matrix P, Vec x) {
23 this.P=P;
24 this.x=x;
25 N=P.getRowDimension();
26 k1=0; k2=1;
27
28 // ColorRamp cramp=new ColorRamp(256);
29 // cramp.set(0,Color.black);
30 // cramp.gradientTo(255,Color.white);
31 clut=new Color[256];
32 for (int i=0; i<256; i++) clut[i]=new Color(255,255,255,i);
33
34 ix = new int[N];
35 iy = new int[N];
36
37 markerSize=new VInteger("markerSize",4);
38 cmap=new VMap(new LinearMap(256),new Node("cmap"));
39 xmap=new VMap(new LinearMap(256),new Node("xmap"));
40 ymap=new VMap(new LinearMap(256),new Node("ymap"));
41 exposeCommands(this);
42
43 P.viewable().addObserver(this);
44 xmap.addObserver(this);
45 ymap.addObserver(this);
46 }
47
48 public void sized() {
49 xmap.getMap().setIntRange(width);
50 ymap.getMap().setIntRange(height);
51 update(null,null);
52 }
53
54 public void attach() { P.viewable().addObserver(this); }
55 public void detach() {
56 P.viewable().deleteObserver(this);
57 xmap.dispose();
58 ymap.dispose();
59 cmap.dispose();
60 markerSize.dispose();
61 }
62
63 public void rotate() {
64 k2++;
65 if (k2>=P.getColumnDimension()) {
66 k2=0; k1++;
67 if (k1>=P.getColumnDimension()) k1=0;
68 }
69 if (k2==k1) rotate();
70 }
71
72 public void starting() {}
73 public void stopping() {}
74 public void run() { repaint(); }
75 public void dispose() { detach(); }
76
77 public void update(Observable o, Object a)
78 {
79 double [][] p=P.getArray();
80 IMap xm=xmap.getMap();
81 IMap ym=ymap.getMap();
82
83 for (int i=0; i<N; i++) {
84 double r[]=p[i];
85 ix[i]=xm.toInt(r[k1]);
86 iy[i]=ym.toInt(r[k2]);
87 }
88 repaint();
89 }
90
91 public void paint(Graphics g) {
92 double [] a=x.array();
93 IMap cm=cmap.getMap();
94 int b=markerSize.value;
95
96 clear(g);
97 for (int i=0; i<N; i++) {
98 g.setColor(clut[cm.clipInt(a[i])]);
99 g.fillRect(ix[i],iy[i],b,b);
100 }
101 }
102
103 public void getCommands(Registry r) {
104 r.add("rotate").add("x-map").add("y-map").add("c-map");
105 }
106 public void execute(String cmd, Environment env)
107 {
108 if (cmd.equals("rotate")) { rotate(); update(null,null); }
109 else if (cmd.equals("x-map")) Shell.expose(xmap.getViewer(),"x-map");
110 else if (cmd.equals("y-map")) Shell.expose(ymap.getViewer(),"y-map");
111 else if (cmd.equals("c-map")) Shell.expose(cmap.getViewer(),"c-map");
112 }
113 }