import java.util.Vector; import java.util.Observer; import java.util.Observable; import java.awt.*; public class Grapher extends Canvas implements Observer { Vector graphs=new Vector(0,1); protected GraphicsWrapper gw=null; protected float xInc, yInc; protected boolean showAxes; protected boolean showLabels; protected boolean scroll; protected float xMin; protected long minRedrawTime; protected long lastRedrawTime=0; public Grapher(int rw, int rh, float minX, float maxX, float minY, float maxY) { setAbsoluteMetrics(rw,rh,minX,maxX,minY,maxY); setMinRedrawTime(500); noAxes(); } public void setMinRedrawTime(long millis) { minRedrawTime=millis; } protected boolean canRedraw() { long time=System.currentTimeMillis(); if (time-lastRedrawTime>minRedrawTime) { lastRedrawTime=time; return true; } return false; } public Grapher(int rw, int rh, float xWidth, float minY, float maxY) { setScrollMetrics(rw,rh,xWidth,minY,maxY); } public Grapher(float minX, float maxX, float minY, float maxY) { this(0,0,minX,maxX,minY,maxY); } public Grapher(float xWidth, float minY, float maxY) { this(0,0,xWidth,minY,maxY); } public void addGraph(Graph g) { graphs.addElement(g); g.addObserver(this); } public void removeGraph(Graph g) { if (graphs.removeElement(g)) g.deleteObserver(this); } public void removeGraphs() { Graph g; for (int i=0; i max) max=x; } } return max; } protected void drawAxes(Graphics g) { gw.setColor(Color.black); gw.setGraphics(g); GraphWindow win=gw.getGraphWindow(); float minx=win.getMinX(); float maxx=win.getMaxX(); float miny=win.getMinY(); float maxy=win.getMaxY(); if (0>=minx&&0<=maxx) gw.drawLine(new GraphPoint(0,miny), new GraphPoint(0,maxy)); if (0>=miny&&0<=maxy) gw.drawLine(new GraphPoint(minx,0), new GraphPoint(maxx,0)); float pty = 0 > miny ? 0 : miny; float ptx = 0 > minx ? 0 : minx; float inix=((int)(minx/xInc))*xInc; float iniy=((int)(miny/yInc))*yInc; GraphPoint a; int x, y; for (float cx=inix; cx<=maxx; cx+=xInc) { a=gw.getGraphWindow().translate(new GraphPoint(cx,pty)); x = (int)a.getX(); y = (int)a.getY(); g.drawLine(x,y,x,y-5); if (showLabels) if (cx==0) g.drawString("O",x+5,y-5); else g.drawString(String.valueOf(cx),x,y-5); } for (float cy=iniy; cy<=maxy; cy+=yInc) { a=gw.getGraphWindow().translate(new GraphPoint(ptx,cy)); x = (int)a.getX(); y = (int)a.getY(); g.drawLine(x,y,x+5,y); if (showLabels) if (cy!=0) g.drawString(String.valueOf(cy),x+5,y); else if (ptx!=0) g.drawString(String.valueOf(cy),x+5,y); } } protected void clear(Graphics g) { g.setColor(Color.white); GraphWindow win=gw.getGraphWindow(); g.fillRect(0,0,win.getRealWidth(),win.getRealHeight()); } protected void render(Graph graph, Graphics graphics) { gw.setGraphics(graphics); GraphWindow win=gw.getGraphWindow(); Vector points=graph.getPointsBetween(win.getMinX(),win.getMaxX()); int style=graph.getStyle(); //System.out.println(graph.getName()+" : "+graph.getColor()); graphics.setColor(graph.getColor()); GraphPoint ptA; for (int i=0; i