9. สร้างใหม่จากเก่า

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.

 
รหัสที่ 9-10  คลาส DWindow3D ขยายความสามารถจากคลาส DWindow
import jlab.graphics.*;

public class DWindow3D extends DWindow {
  private double depth;
  private double xc, yc;
   
  public DWindow3D(double w, double h, double d) {
    super(w, h);
    depth = d;
    xc = w/2; yc = h/2;
  }
  public double getDepth() {
    return depth;
  }
  public void drawLine(double x0, double y0, double z0,  
                       double x1, double y1, double z1) {
    super.drawLine(toSX(x0, z0), toSY(y0, z0),  
                   toSX(x1, z1), toSY(y1, z1));
  }
  public void drawRect(double x0, double y0, double z0,
                       double x1, double y1, double z1) {
    drawLine(x0, y0, z0, x0, y1, z0);
    drawLine(x0, y0, z0, x1, y0, z1);
    drawLine(x1, y1, z1, x1, y0, z1);
    drawLine(x1, y1, z1, x0, y1, z0);
  }
  public void fillEllipse(int c, double x, double y, double z,
                          double a, double b) {
    double sx = toSX(x, z);
    double sy = toSY(y, z);
    super.fillEllipse(c, sx, sy, scale(a, z), scale(b, z));
  }
  public void drawFrame(double step) {
    double w = getWidth(), h = getHeight();
    for (int z = 0; z <= depth; z += step)
      drawRect(0, 0, z, w, h, z);
    drawRect(0, 0, 0, 0, h, depth);
    drawRect(w, 0, 0, w, h, depth);
  }
  private double toSX(double x, double z) {
    return xc + scale(x - xc, z);
  }
  private double toSY(double y, double z) {
    return yc + scale(y - yc, z);
  }
  private double scale(double v, double z) {
    return v * 200 / (200 + z);
  }
©2009 S.Prasitjutrakul