annotate src/samer/tools/Trace.java @ 3:15b93db27c04

Get StreamSource to compile, update args for demo
author samer
date Fri, 05 Apr 2019 17:00:18 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.tools;
samer@0 11 import samer.core.*;
samer@0 12 import samer.core.util.*;
samer@0 13 import samer.core.util.heavy.*;
samer@0 14 import java.awt.*;
samer@0 15 import java.awt.event.*;
samer@0 16 import java.awt.image.*;
samer@0 17 import java.util.*;
samer@0 18
samer@0 19 /**
samer@0 20 Displays values of a single real variable as a
samer@0 21 trace: value vs time. The trace can be set to scroll
samer@0 22 or wrap when it reaches the right-hand edge.
samer@0 23
samer@0 24 */
samer@0 25
samer@0 26 public class Trace extends VCanvas implements Observer, Agent, Task
samer@0 27 {
samer@0 28 protected int x1, x2, dx, j0=-1;
samer@0 29 protected IMap map;
samer@0 30
samer@0 31 boolean scroll=false;
samer@0 32 boolean axisFlag;
samer@0 33 Color axisColor;
samer@0 34 VMap vmap;
samer@0 35
samer@0 36 public Trace()
samer@0 37 {
samer@0 38 dx = Shell.getInt("scrollStep",4);
samer@0 39 scroll = Shell.getBoolean("scroll", false);
samer@0 40 axisColor=Shell.getColor("axesColor",Color.gray);
samer@0 41 axisFlag=Shell.getBoolean("axesFlag",false);
samer@0 42
samer@0 43 x1=0; x2=0;
samer@0 44 vmap = new VMap(new LinearMap(-1,1));
samer@0 45 vmap.addObserver(this);
samer@0 46 exposeCommands(this);
samer@0 47 exposeCommands(vmap);
samer@0 48 map = vmap.getMap();
samer@0 49 }
samer@0 50
samer@0 51 public IMap getMap() { return vmap.getMap(); }
samer@0 52
samer@0 53 public void setScroll(boolean f) { scroll=f; }
samer@0 54 public void setStep(int s) { dx=s; }
samer@0 55 public void setAxisColor(Color c) { axisColor=c; }
samer@0 56 public void setAxis(boolean f) {
samer@0 57 j0= -1; axisFlag=f; if (axisFlag) computeAxisPos();
samer@0 58 }
samer@0 59
samer@0 60 public void drawAxis(Graphics g) {
samer@0 61 g.setColor(axisColor);
samer@0 62 g.drawLine(0,j0,width-1,j0);
samer@0 63 }
samer@0 64
samer@0 65 public void paint(Graphics g) { if (j0>=0) drawAxis(g); }
samer@0 66 public void clear(Graphics g) { super.clear(g); paint(g); }
samer@0 67
samer@0 68 public void dispose() { vmap.dispose(); }
samer@0 69 public void starting() {}
samer@0 70 public void stopping() {}
samer@0 71 /**
samer@0 72 moves the trace along one time step, by preparing
samer@0 73 space for the next bit of plotting.
samer@0 74 */
samer@0 75 public void run()
samer@0 76 {
samer@0 77 x1=x2; x2+=dx;
samer@0 78 // graphics.setColor(getBackground());
samer@0 79
samer@0 80 if (x2>=width) {
samer@0 81 if (scroll) { // scroll just enough
samer@0 82 int st=1+x2-width;
samer@0 83 graphics.copyArea(st,0,width-st,height,-st,0);
samer@0 84 x1-=st; x2-=st;
samer@0 85 } else { // fly back
samer@0 86 // blank out dead space at right
samer@0 87 // and also first column of pixels at left
samer@0 88 graphics.clearRect(x1+1,0,width-x1-1,height);
samer@0 89 graphics.clearRect(0,0,1,height);
samer@0 90 x1=0; x2=dx;
samer@0 91 }
samer@0 92 }
samer@0 93
samer@0 94 // clear rectangle and draw axis
samer@0 95 graphics.clearRect(x1+1,0,dx,height);
samer@0 96 if (j0>=0) {
samer@0 97 graphics.setColor(axisColor);
samer@0 98 graphics.fillRect(x1,j0,dx+1,1);
samer@0 99 }
samer@0 100 }
samer@0 101
samer@0 102
samer@0 103 public void update(Observable o, Object arg)
samer@0 104 {
samer@0 105 if (o==vmap) {
samer@0 106 if (arg==VMap.NEW_MAP) map=vmap.getMap();
samer@0 107 if (axisFlag) computeAxisPos();
samer@0 108 clear(getGraphics());
samer@0 109 }
samer@0 110 }
samer@0 111
samer@0 112 public Dimension getPreferredSize() { return new Dimension(256,64); }
samer@0 113 protected void sized() {
samer@0 114 map.setIntRange(height);
samer@0 115 if (axisFlag) computeAxisPos();
samer@0 116 }
samer@0 117
samer@0 118 public void getCommands(Registry r) {
samer@0 119 r.add("scroll",scroll).add("axis",axisFlag).add("step");
samer@0 120 }
samer@0 121 public void execute(String cmd, Environment env) throws Exception {
samer@0 122 if (cmd.equals("scroll")) { scroll = X._bool(env.datum(),!scroll); }
samer@0 123 else if (cmd.equals("axis")) { setAxis(X._bool(env.datum(),!axisFlag)); }
samer@0 124 else if (cmd.equals("step")) { dx = X._int(env.datum(),dx); }
samer@0 125 }
samer@0 126
samer@0 127 private void computeAxisPos() {
samer@0 128 j0 = height-map.toInt(0.0);
samer@0 129 if (map.wasClipped()) j0=-1;
samer@0 130 }
samer@0 131 }