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-32  คลาส Ball3D เขียนเป็นคลาสนามธรรม
import jlab.graphics.*;

public abstract class Ball3D extends BallG {
  protected double z, dz;
  protected Ball3D(double r, double x, double y, double z) {
    super(r, x, y);
    this.z = z;
    dz = -5 + (10 * Math.random());
  }
  public void move(DWindow3D w3) {
    super.move(w3);
    z += dz;
    if (z <= 0 || z >= w3.getDepth()) dz = -dz;
    z = Math.min(Math.max(z, 0), w3.getDepth());
  }
  protected void drawShadow(DWindow3D w3) {
    double s = y / w3.getHeight();
    w3.fillEllipse(DWindow.BLACK, x, w3.getHeight(), z,
                   2*r*s, r/2*s);
  }
  public abstract void draw(DWindow3D w3);
}
©2009 S.Prasitjutrakul