package odend; import java.awt.*; import java.awt.event.*; /** * plots a polyline given by x and y arrays<br> * plot is scaled to fit exactly into thwe window */ public class SimplePlot extends Frame { /** coordinates (t[i], s[i]) of the points of a polyline */ protected double[] t, s; /** 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 */ public SimplePlot(String title, int width, int height, double[] t, double[] s) { super(title); setSize(width, height); this.t = t; this.s = s; // get minimal and maximal values tmin = t[0]; tmax = t[0]; smin = s[0]; smax = s[0]; for (int i = 1; i < t.length; 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); } /** * 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 < t.length; 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; } } }