The Actual Ray Tracer

    Thread raytracer;

    public void start() {
        if (raytracer == null) {
            raytracer = new Thread(this);
            raytracer.start();
        } else {
            raytracer.resume();
        }
    }

    public void stop() {
        if (raytracer != null) {
            raytracer.suspend();
        }
    }
    
    public void run() {
        Graphics g = getGraphics();
        long time = System.currentTimeMillis();
        for (int j = 0; j < height; j++) {
            showStatus("Scanline "+j);
            for (int i = 0; i < width; i++) {
                renderPixel(i, j);
            }
            g.drawImage(screen, 0, 0, this);
        }
        g.drawImage(screen, 0, 0, this);
        time = System.currentTimeMillis() - time;
        showStatus("Rendered in "
           + (time/60000) + ":"
           + ((time%60000)*0.001));
        finished = true;
    }

private void renderPixel(int i, int j) {
    Vector3D dir = new Vector3D(
                 i*Du.x + j*Dv.x + Vp.x,
                 i*Du.y + j*Dv.y + Vp.y,
                 i*Du.z + j*Dv.z + Vp.z);
    Ray ray = new Ray(eye, dir);
    if (ray.trace(objectList)) {
        gc.setColor(ray.Shade(lightList,
                              objectList,
                              background));
    } else {
        gc.setColor(background);
    }
    // oh well, it works
    gc.drawLine(i, j, i, j); 
}
Lecture 19   Slide 30   6.837 Fall '00