/*
 *	@(#)ImageComponentByReferenceTest.java 1.8 00/03/31 14:31:00
 *
 * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.ColorCube;
import java.awt.image.*;
import java.awt.color.ColorSpace;
import com.sun.j3d.utils.geometry.*;

public class ImageComponentByReferenceTest extends JApplet implements ActionListener {

    Shape3D s1,s2;
    TextureLoader t0, t1, t2;
    int count = 0;

    Appearance app = new Appearance();
    BranchGroup objRoot = new BranchGroup();
    TransformGroup objTrans = new TransformGroup();
    BufferedImage bImage1;
    TiledImage checkBoard;
    boolean yUp = false;
    boolean byRef = true;
    JComboBox rasterType, texType;
    ImageComponent2D[] image = new ImageComponent2D[8];
    Appearance dummyApp = new Appearance();
    Texture2D texOne, texCheckBoard;
    javax.media.j3d.Raster raster;
    Box textureCube;
    Shape3D boxShape;
    int w1 = 64, h1 = 32, checkw = 16 , checkh = 16;

    private java.net.URL texImage = null;
    
    public BranchGroup createSceneGraph() {
        objRoot = new BranchGroup();
 
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);

        objRoot.addChild(objTrans);
 
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

	app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);


        textureCube = new Box(0.4f, 0.4f, 0.4f,
                                  Box.GENERATE_TEXTURE_COORDS|
				  Box.GENERATE_NORMALS, app);
	boxShape = textureCube.getShape(Box.FRONT);
	boxShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
	objTrans.addChild(textureCube);

	checkBoard = new TiledImage();
	TextureLoader texLoader = new TextureLoader( texImage, this);
	ImageComponent2D oneImage = texLoader.getImage();
	bImage1 = oneImage.getImage();

	int index = 0;
	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      false,
				      true);

	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      true,
				      true);


	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      false,
				      false);
	
	
	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      true,
				      false);

	createRaster(objRoot);

	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      false,
				      true);

	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      true,
				      true);


	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      false,
				      false);
	
	
	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      true,
				      false);



	texOne = new Texture2D(Texture.BASE_LEVEL,
				      Texture.RGBA,
				      image[2].getWidth(), image[2].getHeight());

	texOne.setCapability(Texture.ALLOW_IMAGE_WRITE);
	texOne.setImage(0, image[2]);
	
        app.setTexture(texOne);

	texCheckBoard = new Texture2D(Texture.BASE_LEVEL,
				      Texture.RGBA,
				      image[4].getWidth(), image[4].getHeight());
	
	texCheckBoard.setCapability(Texture.ALLOW_IMAGE_WRITE);
        objRoot.compile();
	return objRoot;
    }

    public void actionPerformed(ActionEvent e ) {
	Object target = e.getSource();

	if (target == rasterType) {
	    if (rasterType.getSelectedIndex() < 4) {
		raster.setSize(w1, h1);
	    }
	    else {
		raster.setSize(checkw, checkh);
	    }
	    raster.setImage(image[rasterType.getSelectedIndex()]);
	}
	else if (target == texType) {
	    boxShape.setAppearance(dummyApp);
	    if (texType.getSelectedIndex() < 4) {
		texOne.setImage(0, image[texType.getSelectedIndex()]);
		app.setTexture(texOne);
	    }
	    else {
		texCheckBoard.setImage(0, image[texType.getSelectedIndex()]);
		app.setTexture(texCheckBoard);
	    }
		
	    boxShape.setAppearance(app);
	}


    }

    JPanel createImagePanel() {
	JPanel panel = new JPanel();
	String texVals[] = { "One_Yup_ByCopy",
			     "One_Yup_ByReference",
			     "One_Ydown_ByCopy",
			     "One_Ydown_ByReference",
			     "Checkered_Yup_ByCopy",
			     "Checkered_Yup_ByReference",
			     "Checkered_Ydown_ByCopy",
			     "Checkered_Ydown_ByReference"};
	
	rasterType = new JComboBox(texVals);
	rasterType.setLightWeightPopupEnabled(false);
	rasterType.addActionListener(this);
	rasterType.setSelectedIndex(2);
	panel.add(new JLabel("Raster Image")); 	
	panel.add(rasterType);

	texType = new JComboBox(texVals);
	texType.setLightWeightPopupEnabled(false);
	texType.addActionListener(this);
	texType.setSelectedIndex(2);
	panel.add(new JLabel("Texture Image")); 	
	panel.add(texType);
	return panel;

    }



    public ImageComponentByReferenceTest() 
    {
    }

    public ImageComponentByReferenceTest(java.net.URL url) {
        texImage = url;
    }

    public void init() {
        if (texImage == null) {
	    // the path to the image for an applet
	    try {
	        texImage = new java.net.URL(getCodeBase().toString() +
					    "../images/one.jpg");
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}

        Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
	BranchGroup scene = createSceneGraph();
        SimpleUniverse u = new SimpleUniverse(c);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);
	Container contentPane = getContentPane();
	JPanel p = new JPanel();
	BoxLayout boxlayout = new BoxLayout(p, 
					    BoxLayout.Y_AXIS);
	p.setLayout(boxlayout);
        contentPane.add("Center", c);
  
	contentPane.add("South", p);
	
	p.add(createImagePanel());

    }
 
    public static void main(String[] args) {
        java.net.URL url = null;
        // the path to the image file for an application
	try {
	    url = new java.net.URL("file:../images/one.jpg");
	}
	catch (java.net.MalformedURLException ex) {
	    System.out.println(ex.getMessage());
	    System.exit(1);
	}

        new MainFrame(new ImageComponentByReferenceTest(url), 800, 700);
    }

   void createRaster( BranchGroup scene) {



	// Create raster geometries and shapes
	Vector3f trans = new Vector3f( );
	Transform3D tr = new Transform3D( );
	TransformGroup tg;

	// Left
	raster = new javax.media.j3d.Raster( );
	raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE);
	raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE);
	raster.setPosition( new Point3f( -0.9f, 0.75f, 0.0f ) );
	raster.setType( javax.media.j3d.Raster.RASTER_COLOR );
	raster.setOffset( 0, 0 );

	raster.setSize( image[2].getWidth(), image[2].getHeight() );
	raster.setImage( image[2] );
	Shape3D sh = new Shape3D( raster, new Appearance( ) );
	scene.addChild( sh );



    }
}




