/** * GraphDiagram.java * simjava graph */ package eduni.simdiag; import java.awt.*; import java.util.*; import java.io.*; import java.net.*; import java.applet.Applet; import java.awt.event.*; import java.lang.*; class DPoint { public double x,y; public DPoint(double xx, double yy) {x=xx;y=yy;} } class Linedata { String name; Vector points = new Vector();// vector of points public Linedata(String n) { name = n; } public void add(double x, double y) { points.addElement(new DPoint(x,y)); } public DPoint getDPoint(int i) { return (DPoint)points.elementAt(i); } public String getName() { return name; } Color valtocol(int v) { if (v==0) return Color.blue; if (v==1) return Color.red; if (v==2) return Color.darkGray; if (v==3) return Color.gray; if (v==4) return Color.green; if (v==5) return Color.lightGray; if (v==6) return Color.magenta; if (v==7) return Color.orange; if (v==8) return Color.pink; if (v==9) return Color.cyan; return valtocol(v%10); } public void draw(Graphics g, int i, GraphPanel p) { g.setColor(valtocol(i)); // Join points. for (int j=0; j xmax) xmax = x; if (y > ymax) ymax = y; if (x < xmin) xmin = x; if (y < ymin) ymin = y; } public void display() { repaint(); } double log10(double d) { return Math.log(d)/Math.log(10); } void drawNotches(Graphics g, int x1, int y1, int x2, int y2, double v1, double v2) { boolean isXaxis = (y1==y2); double tsize = v2 - v1; // e.g. 17.2 double tgaplog = log10( (double) tsize ); // e.g. 1.2ish int tgaprounded = (int)tgaplog; // e.g. 1 double tgapfinal = Math.pow( 10.0, (double) tgaprounded ); // e.g.10.0 // Change to 0 5 10 etc? if (tsize/tgapfinal < 3.0) { tgapfinal /= 2.0; } // Get first point double div = 0.5 + (v1 / tgapfinal); int idiv = (int) div; double firstt1 = idiv * tgapfinal; // Draw major notches if (isXaxis) { int notchHeight = yoff/10; for (double t=firstt1; t<=v2; t+=tgapfinal ) { int xp = xscale(t); String s = Double.toString(t); g.setColor(Color.blue); g.drawLine(xp,y1,xp,notchHeight+y1); int w = g.getFontMetrics().stringWidth(s); int h = g.getFontMetrics().getHeight(); g.drawString(s,xp-w/2,y1+notchHeight+h); g.setColor(Color.gray.brighter()); g.drawLine(xp,y1,xp,0); } } else { int notchHeight = xoff/10; for (double t=firstt1; t<=v2; t+=tgapfinal ) { int yp = yscale(t); String s = Double.toString(t); g.setColor(Color.blue); g.drawLine(x1,yp,x1-notchHeight,yp); int sw = g.getFontMetrics().stringWidth(s); int sh = g.getFontMetrics().getHeight(); g.drawString(s,x1-sw-notchHeight,yp+sh/2); g.setColor(Color.gray.brighter()); g.drawLine(x1,yp,getSize().width,yp ); } } } void drawAxes(Graphics g ) { int w = getSize().width; int h = getSize().height; g.setColor(Color.blue); g.drawLine(xoff, h-yoff, w, h-yoff); g.drawLine(xoff, h-yoff, xoff, 0); g.drawString(xax,w/2,h-yoff/3); g.drawString(yax,xoff/3,h/2); // Draw notches drawNotches(g,xoff, h-yoff, w, h-yoff,xmin,xmax); drawNotches(g,xoff, h-yoff, xoff, 0 ,ymin,ymax); g.setColor(Color.blue.brighter()); g.drawLine(xoff, h-yoff+1, w, h-yoff+1); g.drawLine(xoff+1, h-yoff, xoff+1, 0); } Image offscreen; Dimension offscreensize=new Dimension(); Graphics offg; /** Plot the graph */ public void paint(Graphics g) { Dimension d = getSize(); if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) { offscreen = createImage(d.width, d.height); offscreensize = d; offg = offscreen.getGraphics(); } int w = getSize().width; int h = getSize().height; System.out.println("Painting "+w+" "+h); offg.setColor(Color.lightGray.darker()); offg.fillRect(0,0,w,h); offg.setColor(Color.lightGray); offg.fillRect(xoff,0,w,h-yoff); drawAxes(offg); for (int i=0; i