Mercurial > hg > jslab
view src/samer/tools/Trace.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
line wrap: on
line source
/* * Copyright (c) 2000, Samer Abdallah, King's College London. * All rights reserved. * * This software is provided AS iS and WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. */ package samer.tools; import samer.core.*; import samer.core.util.*; import samer.core.util.heavy.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.*; /** Displays values of a single real variable as a trace: value vs time. The trace can be set to scroll or wrap when it reaches the right-hand edge. */ public class Trace extends VCanvas implements Observer, Agent, Task { protected int x1, x2, dx, j0=-1; protected IMap map; boolean scroll=false; boolean axisFlag; Color axisColor; VMap vmap; public Trace() { dx = Shell.getInt("scrollStep",4); scroll = Shell.getBoolean("scroll", false); axisColor=Shell.getColor("axesColor",Color.gray); axisFlag=Shell.getBoolean("axesFlag",false); x1=0; x2=0; vmap = new VMap(new LinearMap(-1,1)); vmap.addObserver(this); exposeCommands(this); exposeCommands(vmap); map = vmap.getMap(); } public IMap getMap() { return vmap.getMap(); } public void setScroll(boolean f) { scroll=f; } public void setStep(int s) { dx=s; } public void setAxisColor(Color c) { axisColor=c; } public void setAxis(boolean f) { j0= -1; axisFlag=f; if (axisFlag) computeAxisPos(); } public void drawAxis(Graphics g) { g.setColor(axisColor); g.drawLine(0,j0,width-1,j0); } public void paint(Graphics g) { if (j0>=0) drawAxis(g); } public void clear(Graphics g) { super.clear(g); paint(g); } public void dispose() { vmap.dispose(); } public void starting() {} public void stopping() {} /** moves the trace along one time step, by preparing space for the next bit of plotting. */ public void run() { x1=x2; x2+=dx; // graphics.setColor(getBackground()); if (x2>=width) { if (scroll) { // scroll just enough int st=1+x2-width; graphics.copyArea(st,0,width-st,height,-st,0); x1-=st; x2-=st; } else { // fly back // blank out dead space at right // and also first column of pixels at left graphics.clearRect(x1+1,0,width-x1-1,height); graphics.clearRect(0,0,1,height); x1=0; x2=dx; } } // clear rectangle and draw axis graphics.clearRect(x1+1,0,dx,height); if (j0>=0) { graphics.setColor(axisColor); graphics.fillRect(x1,j0,dx+1,1); } } public void update(Observable o, Object arg) { if (o==vmap) { if (arg==VMap.NEW_MAP) map=vmap.getMap(); if (axisFlag) computeAxisPos(); clear(getGraphics()); } } public Dimension getPreferredSize() { return new Dimension(256,64); } protected void sized() { map.setIntRange(height); if (axisFlag) computeAxisPos(); } public void getCommands(Registry r) { r.add("scroll",scroll).add("axis",axisFlag).add("step"); } public void execute(String cmd, Environment env) throws Exception { if (cmd.equals("scroll")) { scroll = X._bool(env.datum(),!scroll); } else if (cmd.equals("axis")) { setAxis(X._bool(env.datum(),!axisFlag)); } else if (cmd.equals("step")) { dx = X._int(env.datum(),dx); } } private void computeAxisPos() { j0 = height-map.toInt(0.0); if (map.wasClipped()) j0=-1; } }