import java.applet.Applet;
import java.awt.*;
import go.*;

class GoSphereDemo extends GoInterface
{
    GoSphere  sphere;
    int       sphereType;
    GoMatrix  currentModelview = new GoMatrix();
    int       xStart;
    int       yStart;

    final static int SMOOTH            = 0;
    final static int FLAT              = 1;
    final static int WIRE              = 2;
    final static int SMOOTH_WITH_WIRE  = 3;

    GoSphereDemo(int sphereType)
    {   
        this.sphereType = sphereType;

        //
        // Create a sphere to illustrate this demo.
        //

        sphere = new GoSphere(1.0, 16, 16);

        //
        // Turn on LIGHT_0
        //

        go.light(Go.LIGHT_0, true);
        go.light(Go.LIGHT_0, Go.DIRECTIONAL, 1.0, 1.0, 1.0);
    }

    //
    // Interface render method.
    //
    public void render()
    {
        //
        // Clear.
        //

        go.clear(Go.IMAGE);

        //
        // Draw.
        //
            
        switch(sphereType) {
            
        case SMOOTH:
            go.color(0, 0, 1);       // blue solid
            sphere.renderSolid(go);
            break;
            
        case FLAT:
            go.enable(Go.FLAT_SHADE);
            go.color(0, 0, 1);       // blue solid
            sphere.renderSolid(go);
            go.disable(Go.FLAT_SHADE);
            break;
            
        case WIRE:
            go.color(1, 1, 0);       // yellow wire
            sphere.renderWire(go);
            break;
            
        case SMOOTH_WITH_WIRE:
            go.color(0, 0, 1);       // blue solid
            sphere.renderSolid(go);
            go.color(1, 1, 0);       // yellow wire
            sphere.renderWire(go);
            break;
        }
            
        //
        // Display.
        //

        swap();
    }

    //
    // Interface resized method.
    //
    public void resized(int width, int height)
    {
        //
        // Setup projection matrix.
        //

        go.push(Go.MATRIX_MODE);
        go.matrixMode(Go.PROJECTION);
        go.identity();
        if(width > height) {
            go.ortho(-1.0 * width / height, 1.0 * width / height,
                     -1.0, 1.0,
                     -1.0, 1.0);
        }
        else {
            go.ortho(-1.0, 1.0,
                     -1.0 * height / width, 1.0 * height / width,
                     -1.0, 1.0);
        }
        go.pop(Go.MATRIX_MODE);
    }

    //
    // Interface mousePressed method.
    //
    public void mousePressed(int x, int y)
    {
        //
        // Save values that will be used to track the
        // mouse in the mouseDragged() method.
        //
                        
        xStart = x;
        yStart = y;
                         
        go.getModelview(currentModelview);
    }

    //
    // Interface mouseDragged method.
    //
    public void mouseDragged(int x, int y)
    {
        int xEnd = x;
        int yEnd = y;
        
        //
        // Setup modelview matrix based on mouse position.
        //
                        
        go.identity();
        double xTheta = (yEnd - yStart) / 2;
        double yTheta = (xEnd - xStart) / 2;
        go.rotate(xTheta, 1, 0, 0);
        go.rotate(yTheta, 0, 1, 0);
                        
        //
        // Post multiply current modelview matrix.
        //
                         
        go.multiply(currentModelview);
                         
        //
        // Redisplay.
        //
                        
        rerender();
    }
}

public class sphere extends Applet
{
    public void init()
    {
        setLayout(new GridLayout(1, 1));
        
        String sphereType = getParameter("sphereType");
        
        if(sphereType.equals("smooth")) {
            add(new GoSphereDemo(GoSphereDemo.SMOOTH));
        }
        else if(sphereType.equals("flat")) {
            add(new GoSphereDemo(GoSphereDemo.FLAT));
        }
        else if(sphereType.equals("wire")) {
            add(new GoSphereDemo(GoSphereDemo.WIRE));
        }
        else if(sphereType.equals("smoothWithWire")) {
            add(new GoSphereDemo(GoSphereDemo.SMOOTH_WITH_WIRE));
        }
    }
}