Hi Marc,

        I did what you suggested with changing the values of Java3D objects(see
ActionPerformed method), but I get this new error, which tells me I have
not set my capability bits correctly,  Could you please look at the
attached file and give me any suggestions.

        THe error is :
                inside actionPerformed
                inside 1.
        inside capability null/njavax.media.j3d.CapabilityNotSetException:
Appearance: n
        o capability to set material
        at javax.media.j3d.Appearance.setMaterial(Appearance.java:210)
        at ExSwitch.actionPerformed(ExSwitch.java:459)
        at java.awt.MenuItem.processActionEvent(MenuItem.java:500)
        at java.awt.MenuItem.processEvent(MenuItem.java:464)
        at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:269)
        at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:259)
        at java.awt.EventQueue.dispatchEvent(Compiled Code)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:68)
        after setting app[1]

        THANKS
        Vineet Gupta






        At 10:52 AM 2/4/1999 +0100, Marc Alexa wrote:
>Hi!
>
>Sometimes it seems to me that it is better to change values of
>Java3D objects rather than exchanging the objects. What worked
>for me is to change the Material but let the Apparance object be
>the same. Be sure to set the right capcbilities of your Appearance
>object.
>
>Marc
>
>> X-Sender: [EMAIL PROTECTED]
>> Date: Wed, 03 Feb 1999 16:26:04 -0500
>> To: [EMAIL PROTECTED]
>> From: Vineet Gupta <[EMAIL PROTECTED]>
>> Subject: Re: [java3d] Changing appearance interactively
>> Mime-Version: 1.0
>> 
>> To Chad, Tachio,
>> 
>> 
>>      THANKS for the response, but setting the capability to read/write still
>> gives the same error.  Any other suggestions.
>> 
>> 
>>      Vineet Gupta
>> 
>> 
>> At 02:57 PM 2/3/1999 -0500, Tachio Terauchi wrote:
>> >Maybe you didn't set capability.
>> >
>> >-Tachio
>> >
>> >On Tue, 2 Feb 1999, Vineet Gupta wrote:
>> >
>> >> Hi,
>> >> 
>> >>   I have been trying to change the appearance interactively of the small
>> >> world .  I start out with the unlit wireframe, change to the
following to
>> >> show how the scene changes:
>> >> 
>> >>   unlit wireframe (1)
>> >>   unlit solid (2)
>> >>   unlit points (3) 
>> >>   lit solid (4)
>> >>   transparent(5)
>> >>   Texture (6)
>> >>   ...
>> >> 
>> >>   I can change the scene if I precompute the six appearance above for 
>using
>> >> six world scenes, but this is inefficient.  I have tried to make changes
>> >> interactive for one world scene, but I get this error:
>> >> 
>> >> 
>> >>                   Exception occurred during event dispatching.
>> >>                   at change.actionPerformed(change.java:397)
>> >>                   ...
>> >> 
>> >>   I have menu options which can be clicked with above scene changes.  On
>> >> actionperformed method,
>> >> I have shape.setAppearance(null) and then assign the new appearance by
>> >> shape.setAppearance( changeappearance ).  
>> >> I know it has got to be something simple I am not doing.
>> >> 
>> >>   Please help!    
>> >> 
>> >> 
>> >>   THANKS
>> >>   Vineet Gupta
>> >> 
>> >> =====================================================================
>> >> To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
>> >> Java 3D Home Page: http://java.sun.com/products/java-media/3D/
>> >> 
>> >
>> >=====================================================================
>> >To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
>> >Java 3D Home Page: http://java.sun.com/products/java-media/3D/
>> >
>> >
>> =====================================================================
>> To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
>> Java 3D Home Page: http://java.sun.com/products/java-media/3D/
>
>---------------------------------------------------------------------
> Marc Alexa                             Darmstadt U. of Technology
> [EMAIL PROTECTED]  Dept. of CS, GRIS
> http://www.igd.fhg.de/~alexa           3D Graphics Computing
> Phone: ++49.6151.155.674               Rundeturmstr. 6
> Fax:   ++49.6151.155.669               D-64283 Darmstadt, Germany
>---------------------------------------------------------------------
>
>
>


import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.geometry.*;
import java.applet.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;

public class Appear     extends Example
{
        //--------------------------------------------------------------
        //  SCENE CONTENT
        //--------------------------------------------------------------

        //
        //  Nodes (updated via menu)
        //
        private Switch switch2 = null;
        private int currentSwitch = 0;
        Shape3D shape;

        // for setting appearance       
        Transform3D t3d;
        TransformGroup tg0;
        Appearance[] app;
        Group scene;
        landgrid lgd;
        double[] height1;

        ColoringAttributes ca;
        Material material;
        //
        //  Build scene
        //
        public Group buildScene( )
        {
                // Turn on the example headlight
                setHeadlightEnable( true );

                // Default to walk navigation
                setNavigationType( Walk );

                // Build the scene group
                // scene = new Group( );
                // create the root of the branch graph
                scene = new Group();

                if ( debug ) System.err.println( "  switch shapes..." );

                // Add a light
                BoundingSphere worldBounds = new BoundingSphere(
                        new Point3d( 0.0, 0.0, 0.0 ),  // Center
                        1000.0 );                      // Extent

                DirectionalLight light = new DirectionalLight( );
                light.setEnable( true );
                light.setColor( new Color3f( 1.0f, 1.0f, 1.0f ) );
                light.setDirection( new Vector3f( 0.5f, -1.0f, -0.5f ) );
                light.setInfluencingBounds( worldBounds );
                scene.addChild( light );

                // Get the elevation data from the elevation file
                System.out.println("Swoe for elevation");
                lgd = new landgrid();
                lgd.Read_Swoe();
                int numberofelements;
                numberofelements= lgd.ground.pix.length;
                System.out.println("number of elevation data read is "+ 
 numberofelements);
                
                height1 = lgd.returnheight();

                app = new Appearance[13];
                
                // set the initial setup to 0
                for (int I=0; I < 13; I++)
                {
                        app[I] = createAppearance(I);
                }
                Transform3D t3d = new Transform3D();
                t3d.setTranslation ( new Vector3f( -2.0f, 1.5f, 0.0f ) );
                TransformGroup tg0 = new TransformGroup ( t3d );        
                ElevationGrid shape = new ElevationGrid(lgd.latin,lgd.lonin, height1);
                shape.setAppearance( app[0] );
        
                scene.addChild ( shape );
                
                System.out.println("exiting Build scene from ExSwitch");
                return scene;
        }

        
        private Appearance createAppearance(int idx) {
        Appearance app = new Appearance();

        // Globally used colors
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

        switch (idx) {

        // Unlit wire frame
        case 0:
            {
                
                // Set up the coloring properties
                Color3f objColor = new Color3f(0.5f, 0.0f, 0.2f);
                ca = new ColoringAttributes(); // ColoringAttributes
                // ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
                // ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
                ca.setColor(objColor);
                app.setColoringAttributes(ca);

                // Set up the polygon attributes
                PolygonAttributes pa = new PolygonAttributes();
                pa.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
                pa.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
                pa.setPolygonMode(pa.POLYGON_LINE);
                pa.setCullFace(pa.CULL_BACK);
                // pa.setPolygonOffset(0.0);
                app.setPolygonAttributes(pa);

                break;
            }
        // Texture mapped, lit solid
        case 1:
            {
                // Set up the texture map
                TextureLoader tex = new TextureLoader("apimage.jpg", this);
                // tex.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_READ);
                // tex.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);
                app.setTexture(tex.getTexture());

                // Set up the material properties
                Material material = new Material(white, black,white, black, 1.0f);
                material.setCapability(Appearance.ALLOW_MATERIAL_READ);
                material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                // app.setMaterial(material);
                break;
            }


        
        // Unlit solid
        case 2:
            {
                // Set up the coloring properties
                Color3f objColor = new Color3f(.3f, 0.3f, 0.3f); //.4,.2,.4
                ColoringAttributes ca = new ColoringAttributes();
                ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
                ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
                ca.setColor(objColor);
                ca.setShadeModel( ColoringAttributes.SHADE_GOURAUD );
                app.setColoringAttributes(ca);
                break;
            }


        
        // Unlit points
        case 3:
            {
                // Set up the coloring properties
                Color3f objColor = new Color3f(0.2f, 0.2f, 1.0f);
                ColoringAttributes ca = new ColoringAttributes();
                ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
                ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
                ca.setColor(objColor);
                app.setColoringAttributes(ca);

                // Set up the polygon attributes
                PolygonAttributes pa = new PolygonAttributes();
                pa.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
                pa.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
                pa.setPolygonMode(pa.POLYGON_POINT);
                pa.setCullFace(pa.CULL_NONE);
                app.setPolygonAttributes(pa);

                // Set up point attributes
                PointAttributes pta = new PointAttributes();
                pta.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_READ);
                pta.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
                pta.setPointSize(5.0f);
                pta.setPointAntialiasingEnable( false );
                app.setPointAttributes(pta);
                break;
            }

        // Lit solid
        case 4:
            {
                // Set up the material properties
                Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
                Material material = new Material(objColor, black, objColor, 
                                                white, 80.0f);
                material.setCapability(Appearance.ALLOW_MATERIAL_READ);
                material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                app.setMaterial(material);
                break;
            }

        // Transparent, lit solid
        case 5:
            {
                // Set up the transparency properties
                TransparencyAttributes ta = new TransparencyAttributes();
                ta.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
                ta.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
                ta.setTransparencyMode(ta.BLENDED);
                ta.setTransparency(0.6f);
                app.setTransparencyAttributes(ta);

                // Set up the polygon attributes
                PolygonAttributes pa = new PolygonAttributes();
                pa.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
                pa.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
                pa.setCullFace(pa.CULL_NONE);
                app.setPolygonAttributes(pa);

                // Set up the material properties
                Color3f objColor = new Color3f(0.7f, 0.8f, 1.0f);
                Material material = new Material(objColor, black, objColor, 
                                                white, 80.0f);
                material.setCapability(Appearance.ALLOW_MATERIAL_READ);
                material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                app.setMaterial(material);
                break;
            }

        // Lit solid, no specular
        case 6:
            {
                // Set up the material properties
                Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
                Material material = new Material(objColor, black, objColor, 
                                                white, 80.0f);
                material.setCapability(Appearance.ALLOW_MATERIAL_READ);
                material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                app.setMaterial(material);
                break;
            }
        // Lit solid, specular only
        case 7:
            {
                // Set up the material properties
                Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
                Material material = new Material(objColor, black, objColor, 
                                                white, 80.0f);
                material.setCapability(Appearance.ALLOW_MATERIAL_READ);
                material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                app.setMaterial(material);
                break;
            }
        // Another lit solid with a different color
        case 8:
            {
                // Set up the material properties
                Color3f objColor = new Color3f(0.8f, 0.8f, 0.0f);
                Material material = new Material(objColor, black, objColor, 
                                                white, 80.0f);
                material.setCapability(Appearance.ALLOW_MATERIAL_READ);
                material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                app.setMaterial(material);
                break;
            }
        // Material
        case 9:
                {
                        Material mat = new Material( );
                        mat.setCapability(Appearance.ALLOW_MATERIAL_READ);
                        mat.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                        mat.setAmbientColor(  0.3f, 0.3f, 0.3f );
                        mat.setDiffuseColor(  1.0f, 0.0f, 0.0f );
                        mat.setEmissiveColor( 0.0f, 0.0f, 0.0f );
                        mat.setSpecularColor( 1.0f, 1.0f, 1.0f );
                        mat.setShininess( 80.0f );
                        app.setMaterial( mat );
                        break;
                }
        // Transparency
        case 10:
        {
                TransparencyAttributes ta = new TransparencyAttributes( );
                ta.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
                ta.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
                ta.setTransparency( 0.5f );
                ta.setTransparencyMode( TransparencyAttributes.BLENDED );
                app.setTransparencyAttributes( ta );
                break;
        }
        // Line 
        case 11:
                {
                LineAttributes lta = new LineAttributes( );
                lta.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ);
                lta.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);
                lta.setLineWidth( 10.0f );
                lta.setLineAntialiasingEnable( false );
                lta.setLinePattern( LineAttributes.PATTERN_SOLID );
                app.setLineAttributes( lta );
                break;
        }
        // Rendering
        case 12:
                {
                RenderingAttributes ra = new RenderingAttributes( );
                ra.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_READ);
                ra.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_WRITE);
                ra.setDepthBufferEnable( true );
                ra.setAlphaTestFunction( RenderingAttributes.ALWAYS );
                app.setRenderingAttributes( ra );
                break;
        }
        
        default:
            {
                ColoringAttributes ca = new ColoringAttributes();
                ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
                ca.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
                ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
                app.setColoringAttributes(ca);
            }
        }
        return app;
    }
        //--------------------------------------------------------------
        //  USER INTERFACE
        //--------------------------------------------------------------

        //
        //  Main
        //
        public static void main( String[] args )
        {
                
                ExSwitch ex = new ExSwitch( );
                // new MainFrame(ex, 256, 256);
                ex.initialize( args );
                ex.buildUniverse( );
                ex.showFrame( );
                
        }
        // for applet
        public void init ( )
        {
                String [] args = new String[2];

                this.initialize( args );
                this.buildUniverse ( );
                this.showFrame( );
        }
                

        //
        //  Initialize the GUI (application and applet)
        //
        public void initialize( String[] args )
        {
                // Initialize the window, menubar, etc.
                super.initialize( args );
                exampleFrame.setTitle( "Java 3D Switch Example" );

                // Add a menu to select among switch options
                Menu mt = new Menu( "Appearance" );

                MenuItem mi = new MenuItem("0. Unlit Wireframe"); 
                mi.addActionListener(this);
                mt.add(mi);
        
                mi = new MenuItem("1. Texture mappped, lit solid");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("2. Unlit Solid");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("3. Unlit points");
                mi.addActionListener(this);
                mt.add(mi);
        
                mi = new MenuItem("4. Lit Solid");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("5. Transparent, lit solid");
                mi.addActionListener(this);
                mt.add(mi);
        
                mi = new MenuItem("6. Lit solid, no specular");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("7. Lit solid, specular only");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("8. Material");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("9. Transparency");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("10. Line");
                mi.addActionListener(this);
                mt.add(mi);

                mi = new MenuItem("11. Rendering");
                mi.addActionListener(this);
                mt.add(mi);

                exampleMenuBar.add(mt);
        
        }
        

        public void actionPerformed(ActionEvent evt)
        {
                
                System.out.println("inside actionPerformed");
                String arg = evt.getActionCommand();
                if (arg.equals("1. Texture mappped, lit solid"))
                {
                        System.out.println("inside 1.");
                        try {
                        // System.out.println("app[0] = "+app[0]);
                        // System.out.println("app[1] = "+app[1]);                     
 
// shape.setAppearance( app[1] ); gives 
/*      Exception occurred during event dispatching
        java.lang.NullPointerException
                at ExSwitch.actionPerformed (...)
*/
                        app[1].setMaterial(material);

/*      Gives the following error:

inside actionPerformed
inside 1.
inside capability null/njavax.media.j3d.CapabilityNotSetException: Appearance: n
o capability to set material
        at javax.media.j3d.Appearance.setMaterial(Appearance.java:210)
        at ExSwitch.actionPerformed(ExSwitch.java:459)
        at java.awt.MenuItem.processActionEvent(MenuItem.java:500)
        at java.awt.MenuItem.processEvent(MenuItem.java:464)
        at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:269)
        at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:259)
        at java.awt.EventQueue.dispatchEvent(Compiled Code)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:68)
after setting app[1]
 
*/


                        }
                        catch (NullPointerException e)
                        {
                                System.out.print("inside null pointer");
                                e.printStackTrace();
                        }
                        catch (CapabilityNotSetException e)
                        {
                                System.out.print("inside capability null");
                                e.printStackTrace();
                        }


                        System.out.println("after setting app[1]");
                        return;
                }

                else if (arg.equals("2. Unlit Solid"))
                {
                        System.out.println("inside 2.");
                        shape.setAppearance(app[2]);
                        System.out.println("after shape.setAppearance");
                        return; 
                }

                else if (arg.equals("Exit"))
                {
                        System.exit(0);
                        return;
                }
        } // end of method 
} // end of class

Reply via email to