Mercurial > hg > jslab
comparison src/samer/maths/VectorPlotter.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 /* | |
2 * VectorPlotter.java | |
3 * | |
4 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
5 * All rights reserved. | |
6 * | |
7 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
8 * without even the implied warranty of MERCHANTABILITY or | |
9 * FITNESS FOR A PARTICULAR PURPOSE. | |
10 */ | |
11 | |
12 package samer.maths; | |
13 | |
14 import java.util.*; | |
15 import java.awt.*; | |
16 import java.awt.event.*; | |
17 import samer.core.*; | |
18 import samer.core.util.*; | |
19 import samer.core.Agent.*; | |
20 import samer.tools.*; | |
21 | |
22 /** | |
23 A Viewer that draws a variety of graphs from data in a vector | |
24 */ | |
25 | |
26 public class VectorPlotter extends Plotter implements Agent, Observer | |
27 { | |
28 private Vec vector; | |
29 private boolean autoscale=false; | |
30 private int type; | |
31 private Pen pen; | |
32 private VMap vmap=new VMap(ymap); | |
33 | |
34 Observable obs; | |
35 Node node=null; | |
36 | |
37 public final static int LINE=0; | |
38 public final static int STEP=1; | |
39 public final static int FILL=2; | |
40 public final static int FILL3D=3; | |
41 public final static int CLOSED=4; | |
42 public final static int STEPS=5; | |
43 | |
44 public VectorPlotter(Observable o, Vec s) { this(o); setVec(s); } | |
45 protected VectorPlotter(Observable o) | |
46 { | |
47 obs=o; | |
48 node=Shell.env().node(); | |
49 | |
50 type = Shell.getInt("type",LINE); | |
51 setAxes( XAXIS); // no Y axis please | |
52 // pen = getPen(); | |
53 | |
54 vmap.addObserver(this); | |
55 ymap = vmap.getMap(); | |
56 | |
57 // attach commands from this agent to this Viewer | |
58 exposeCommands( this); | |
59 exposeCommands( vmap); | |
60 } | |
61 | |
62 public Dimension getPreferredSize() { | |
63 int cx=Shell.getInt("cell.width",Shell.getInt("cell.size",2)); | |
64 return new Dimension(cx*vector.size(),96); | |
65 } | |
66 protected void realized() { super.realized(); attach(); } | |
67 | |
68 // .............. Viewer bits ............................. | |
69 | |
70 public void update(Observable o, Object s) | |
71 { | |
72 if (s==this) return; // &&& | |
73 if (s==Viewable.DISPOSING) { | |
74 Shell.releaseViewer(this); | |
75 } else if (obs==o) { | |
76 if (autoscale) scale(); | |
77 repaint(); | |
78 } else if (o==vmap) { | |
79 if (s==VMap.NEW_MAP) ymap = vmap.getMap(); | |
80 if (autoscale) scale(); repaint(); | |
81 } | |
82 } | |
83 | |
84 public void attach() { obs.addObserver(this); } | |
85 public void detach() | |
86 { | |
87 obs.deleteObserver(this); | |
88 super.detach(); | |
89 } | |
90 | |
91 // .............. Agent bits .............................. | |
92 | |
93 public void getCommands(Registry r) | |
94 { | |
95 r.add("line",type==LINE) | |
96 .add("box",type==CLOSED) | |
97 .add("fill",type==FILL) | |
98 .add("fill3d",type==FILL3D) | |
99 .add("steps",type==STEPS); | |
100 r.add("scale").add("autoscale",autoscale); | |
101 r.group(); r.add("publish"); | |
102 } | |
103 | |
104 public void execute(String c, Environment env) throws Exception | |
105 { | |
106 if (c.equals("line")) { type=LINE; repaint(); } | |
107 else if (c.equals("box")) { type=CLOSED; repaint(); } | |
108 else if (c.equals("fill")) { type=FILL; repaint(); } | |
109 else if (c.equals("fill3d")) { type=FILL3D; repaint(); } | |
110 else if (c.equals("steps")) { type=STEPS; repaint(); } | |
111 else if (c.equals("scale")) { scale(); repaint(); } | |
112 else if (c.equals("autoscale")) { | |
113 autoscale = X._bool(env.datum(),!autoscale); | |
114 if (autoscale) { scale(); repaint(); } | |
115 } else if (c.equals("publish")) | |
116 Shell.put(X.string(env.datum(),"plotter"),this); | |
117 } | |
118 | |
119 // ................ functionality ........................... | |
120 | |
121 public void setVec( Vec v) | |
122 { | |
123 vector = v; | |
124 xmap.setDomain(0,vector.size()); | |
125 if (autoscale) scale(); | |
126 } | |
127 | |
128 public void scale() | |
129 { | |
130 double max, min, x; | |
131 | |
132 Vec.Iterator i=vector.iterator(); | |
133 min=max=i.next(); | |
134 while (i.more()) { | |
135 x=i.next(); | |
136 if (x>max) max=x; | |
137 if (x<min) min=x; | |
138 } | |
139 | |
140 if (ymap instanceof LinearMap) { | |
141 // leave some space | |
142 double delta=max-min; | |
143 min -= 0.1*delta; | |
144 max += 0.1*delta; | |
145 } else if (ymap instanceof LogMap) { | |
146 // somewhat arbitrarily... | |
147 if (min==0) min=max/10000; | |
148 } | |
149 | |
150 vmap.setDomain(min,max); | |
151 vmap.changed(this); | |
152 } | |
153 | |
154 public void update(Graphics g) { | |
155 g.clearRect(0,0,width,height); | |
156 paint(g); | |
157 } | |
158 | |
159 public void paint( Graphics g) | |
160 { | |
161 Vec.Iterator i=vector.iterator(); | |
162 | |
163 super.paint(g); // draw axes or whatever | |
164 pen=getPen(g); | |
165 // pen.assert(); | |
166 | |
167 switch (type) { | |
168 | |
169 case LINE: | |
170 | |
171 pen.moveto(0,i.next()); | |
172 for (int j=1; i.more(); j++) { | |
173 pen.lineto(j,i.next()); | |
174 } | |
175 break; | |
176 | |
177 case FILL3D: | |
178 | |
179 for (int j=0; i.more(); j++) { | |
180 pen.moveto(j,0); | |
181 pen.abs(j+1,i.next()).rectify(); | |
182 pen.fill3DRect(true); | |
183 } | |
184 break; | |
185 | |
186 case FILL: | |
187 | |
188 for (int j=0; i.more(); j++) { | |
189 pen.moveto(j,0); | |
190 pen.abs(j+1,i.next()).rectify(); | |
191 pen.fillRect(); | |
192 } | |
193 break; | |
194 | |
195 case STEPS: | |
196 | |
197 pen.moveto(0,i.next()); | |
198 for (int j=1; i.more(); j++) { | |
199 pen.rel(1,0).lineto(); | |
200 pen.abs(j,i.next()).lineto(); | |
201 } | |
202 pen.rel(1,0).lineto(); | |
203 break; | |
204 | |
205 case CLOSED: | |
206 | |
207 for (int j=0; i.more(); j++) { | |
208 pen.moveto(j,0); | |
209 pen.abs(j+1,i.next()).rectify(); | |
210 pen.rect(); | |
211 } | |
212 break; | |
213 | |
214 } | |
215 } | |
216 } |