8. วัตถุสิ่งของ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 8-22  คลาส LineGraph สำหรับแสดงวินโดว์พร้อมกราฟเส้น
import jlab.graphics.DWindow;
public class LineGraph {
  private DWindow window;
  private double ymin, ymax;
  public LineGraph(int w, int h) {
    window = new DWindow(w, h);
  }
  public void plot(int color, TimeSeries ts) {
    if (ymin == ymax) {
      ymin = ts.min(); ymax = ts.max();
    }
    int n = ts.size();
    double width = window.getWidth();
    double height = window.getHeight();
    for (int i = 0; i < n-1; i++) {
      double sx0 = toScreen(i,   0, n-1, width);
      double sx1 = toScreen(i+1, 0, n-1, width);
      double sy0 = toScreen(ts.get(i),   ymax, ymin, height);
      double sy1 = toScreen(ts.get(i+1), ymax, ymin, height);
      window.drawLine(color, sx0, sy0, sx1, sy1);
    }
  }
  private static double toScreen(double v, double min,  
                                 double max, double len) {
    return (v - min) * (len-1) / (max - min);
  }
}
©2009 S.Prasitjutrakul