samer@0: /* samer@0: * VectorPlotter.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths; samer@0: samer@0: import java.util.*; samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: import samer.core.*; samer@0: import samer.core.util.*; samer@0: import samer.core.Agent.*; samer@0: import samer.tools.*; samer@0: samer@0: /** samer@0: A Viewer that draws a variety of graphs from data in a vector samer@0: */ samer@0: samer@0: public class VectorPlotter extends Plotter implements Agent, Observer samer@0: { samer@0: private Vec vector; samer@0: private boolean autoscale=false; samer@0: private int type; samer@0: private Pen pen; samer@0: private VMap vmap=new VMap(ymap); samer@0: samer@0: Observable obs; samer@0: Node node=null; samer@0: samer@0: public final static int LINE=0; samer@0: public final static int STEP=1; samer@0: public final static int FILL=2; samer@0: public final static int FILL3D=3; samer@0: public final static int CLOSED=4; samer@0: public final static int STEPS=5; samer@0: samer@0: public VectorPlotter(Observable o, Vec s) { this(o); setVec(s); } samer@0: protected VectorPlotter(Observable o) samer@0: { samer@0: obs=o; samer@0: node=Shell.env().node(); samer@0: samer@0: type = Shell.getInt("type",LINE); samer@0: setAxes( XAXIS); // no Y axis please samer@0: // pen = getPen(); samer@0: samer@0: vmap.addObserver(this); samer@0: ymap = vmap.getMap(); samer@0: samer@0: // attach commands from this agent to this Viewer samer@0: exposeCommands( this); samer@0: exposeCommands( vmap); samer@0: } samer@0: samer@0: public Dimension getPreferredSize() { samer@0: int cx=Shell.getInt("cell.width",Shell.getInt("cell.size",2)); samer@0: return new Dimension(cx*vector.size(),96); samer@0: } samer@0: protected void realized() { super.realized(); attach(); } samer@0: samer@0: // .............. Viewer bits ............................. samer@0: samer@0: public void update(Observable o, Object s) samer@0: { samer@0: if (s==this) return; // &&& samer@0: if (s==Viewable.DISPOSING) { samer@0: Shell.releaseViewer(this); samer@0: } else if (obs==o) { samer@0: if (autoscale) scale(); samer@0: repaint(); samer@0: } else if (o==vmap) { samer@0: if (s==VMap.NEW_MAP) ymap = vmap.getMap(); samer@0: if (autoscale) scale(); repaint(); samer@0: } samer@0: } samer@0: samer@0: public void attach() { obs.addObserver(this); } samer@0: public void detach() samer@0: { samer@0: obs.deleteObserver(this); samer@0: super.detach(); samer@0: } samer@0: samer@0: // .............. Agent bits .............................. samer@0: samer@0: public void getCommands(Registry r) samer@0: { samer@0: r.add("line",type==LINE) samer@0: .add("box",type==CLOSED) samer@0: .add("fill",type==FILL) samer@0: .add("fill3d",type==FILL3D) samer@0: .add("steps",type==STEPS); samer@0: r.add("scale").add("autoscale",autoscale); samer@0: r.group(); r.add("publish"); samer@0: } samer@0: samer@0: public void execute(String c, Environment env) throws Exception samer@0: { samer@0: if (c.equals("line")) { type=LINE; repaint(); } samer@0: else if (c.equals("box")) { type=CLOSED; repaint(); } samer@0: else if (c.equals("fill")) { type=FILL; repaint(); } samer@0: else if (c.equals("fill3d")) { type=FILL3D; repaint(); } samer@0: else if (c.equals("steps")) { type=STEPS; repaint(); } samer@0: else if (c.equals("scale")) { scale(); repaint(); } samer@0: else if (c.equals("autoscale")) { samer@0: autoscale = X._bool(env.datum(),!autoscale); samer@0: if (autoscale) { scale(); repaint(); } samer@0: } else if (c.equals("publish")) samer@0: Shell.put(X.string(env.datum(),"plotter"),this); samer@0: } samer@0: samer@0: // ................ functionality ........................... samer@0: samer@0: public void setVec( Vec v) samer@0: { samer@0: vector = v; samer@0: xmap.setDomain(0,vector.size()); samer@0: if (autoscale) scale(); samer@0: } samer@0: samer@0: public void scale() samer@0: { samer@0: double max, min, x; samer@0: samer@0: Vec.Iterator i=vector.iterator(); samer@0: min=max=i.next(); samer@0: while (i.more()) { samer@0: x=i.next(); samer@0: if (x>max) max=x; samer@0: if (x