SimplePlot


package odeadapt;

import java.awt.*;
import java.awt.event.*;

/**
 *  plots a polyline given by x and y arrays
 */
public class SimplePlot extends Frame {
  // coordinates (t[i], s[i]) of the points of a polyline
  protected double[] t, s;
  
  // number of points to draw
  int maxPoints;
  
  // bounds of the coordinte arrays
  protected double tmin, tmax, smin, smax;
  
  class WindowCloser extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }

  /**
   * SimplePlot constructor<br>
   * creates window with given title and dimensions<br>
   * passes handles to the polyline arrays<br>
   * draws only max first points 
   */
  public SimplePlot(String title, int width, int height, 
		    double[] t, double[] s, int max) {
    super(title);
    setSize(width, height);
    this.t = t;
    this.s = s;
    this.maxPoints = max;
    
    // get minimal and maximal values
    tmin = t[0];
    tmax = t[0];
    smin = s[0];
    smax = s[0];
    for (int i = 1; i < maxPoints; i++) {
      tmin = Math.min(tmin, t[i]);
      tmax = Math.max(tmax, t[i]);
      smin = Math.min(smin, s[i]);
      smax = Math.max(smax, s[i]);
    }
    
    // register exit function
    addWindowListener(new WindowCloser());
    
    // and display
    setBackground(Color.white);
    setVisible(true);
  }

  /**
   * SimplePlot constructor<br>
   * creates window with given title and dimensions<br>
   * passes handles to the polyline arrays
   */
  public SimplePlot(String title, int width, int height, double[] t, double[] s) {
    this(title, width, height, t, s, t.length);
  }

  /**
   * draw the polyline at each update
   */
  public void paint(Graphics g) {
    int ya, yb;
    int xa, xb;
    
    // bestimme Größe der Zeichenfläche
    Dimension d = getSize();
    Insets border = getInsets();
    int x0 = border.left;
    int x1 = border.right;
    int y0 = border.top;
    int y1 = border.bottom;
    int dx = d.width - x0 - x1;
    int dy = d.height - y0 - y1;
    
    // Skalierungsfaktoren von (t,s) zu (x,y)
    double sx = (tmax - tmin) / dx;
    double sy = (smax - smin) / dy;
    
    // Anfangswert
    xa = (int) Math.round(t[0] / sx + x0);
    ya = (int) Math.round((smax - s[0]) / sy + y0);
    for (int i = 1; i < maxPoints; i++) {
      xb = (int) Math.round(t[i] / sx + x0);
      yb = (int) Math.round((smax - s[i]) / sy + y0);
      g.drawLine(xa, ya, xb, yb);
      xa = xb;
      ya = yb;
    }
  }
}

previous    contents     next

Peter Junglas 20.12.1999