6 Replies Latest reply: Aug 12, 2010 10:11 AM by 843799 RSS

    how to change object background color on  java run time

    843799
      Hi,

      I create object loading program. my problem is run time i change object background color using color picker. i select any one color of color picker than submit. The selecting color not assign object background.

      pls help me? How to run time change object background color?

      here follwing code

      import com.sun.j3d.loaders.objectfile.ObjectFile;
      import com.sun.j3d.loaders.ParsingErrorException;
      import com.sun.j3d.loaders.IncorrectFormatException;
      import com.sun.j3d.loaders.Scene;
      import java.applet.Applet;
      import java.awt.*;
      import java.awt.event.*;
      import com.sun.j3d.utils.applet.MainFrame;
      import com.sun.j3d.utils.universe.*;
      import javax.media.j3d.*;
      import javax.vecmath.*;
      import java.io.*;
      import com.sun.j3d.utils.behaviors.vp.*;
      import java.net.URL;
      import java.net.MalformedURLException;
      import java.awt.Graphics ;
      import javax.swing.*;


      public class ObjLoad1 extends Applet implements ActionListener
      {
      private boolean spin = false;
      private boolean noTriangulate = false;
      private boolean noStripify = false;
      private double creaseAngle = 60.0;
      private URL filename = null;
      private SimpleUniverse u;
      private BoundingSphere bounds;
      private Panel cardPanel;
      private Button Tit,sub;
      private CardLayout ourLayout;
      private BorderLayout bl;
      Background bgNode;
      BranchGroup objRoot;
      List thelist;
      Label l1;
           
      public BranchGroup createSceneGraph()
           {
      BranchGroup objRoot = new BranchGroup();
      TransformGroup objScale = new TransformGroup();
      Transform3D t3d = new Transform3D();
      t3d.setScale(0.7);
      objScale.setTransform(t3d);
      objRoot.addChild(objScale);

      TransformGroup objTrans = new TransformGroup();
      objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
      objScale.addChild(objTrans);

      int flags = ObjectFile.RESIZE;
      if (!noTriangulate) flags |= ObjectFile.TRIANGULATE;
      if (!noStripify) flags |= ObjectFile.STRIPIFY;
      ObjectFile f = new ObjectFile(flags,(float)(creaseAngle * Math.PI / 180.0));
      Scene s = null;

           try {
                s = f.load(filename);
           }
           catch (FileNotFoundException e) {
           System.err.println(e);
           System.exit(1);
           }
           catch (ParsingErrorException e) {
           System.err.println(e);
           System.exit(1);
           }
           catch (IncorrectFormatException e) {
           System.err.println(e);
           System.exit(1);
           }
           
           objTrans.addChild(s.getSceneGroup());
           bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

      if (spin) {
           Transform3D yAxis = new Transform3D();
           Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,0,0,4000,0,0,0,0,0);
           RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,objTrans,yAxis,0.0f,(float) Math.PI*2.0f);
           rotator.setSchedulingBounds(bounds);
           objTrans.addChild(rotator);
           }
      //Background color setting
      Color3f bgColor = new Color3f(100,200,230);
      bgNode = new Background(bgColor);
      bgNode.setApplicationBounds(bounds);
      objRoot.addChild(bgNode);
      return objRoot;
           }


      private void usage()
      {
      System.out.println("Usage: java ObjLoad1 [-s] [-n] [-t] [-c degrees] <.obj file>");
      System.out.println("-s Spin (no user interaction)");
      System.out.println("-n No triangulation");
      System.out.println("-t No stripification");
      System.out.println("-c Set crease angle for normal generation (default is 60 without");
      System.out.println("smoothing group info, otherwise 180 within smoothing groups)");
      System.exit(0);
      } // End of usage

      public void init() {
           
      if (filename == null) {
      try {
      URL path = getCodeBase();
      filename = new URL(path.toString() + "./galleon.obj");
      }
      catch (MalformedURLException e) {
           System.err.println(e);
           System.exit(1);
      }
      }


           //setLayout(new BorderLayout());
           
           //setLayout(new GridLayout(5,0));
           //setLayout(new CardLayout());
           //setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

      GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
      Canvas3D c = new Canvas3D(config);
      add(c);




      BranchGroup scene = createSceneGraph();
      u = new SimpleUniverse(c);
           
      ViewingPlatform viewingPlatform = u.getViewingPlatform();
      PlatformGeometry pg = new PlatformGeometry();

      Color3f ambientColor = new Color3f(45,27,15);
      AmbientLight ambientLightNode = new AmbientLight(ambientColor);
      ambientLightNode.setInfluencingBounds(bounds);
      pg.addChild(ambientLightNode);
      Color3f light1Color = new Color3f(111,222,222);
      Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
      Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
      Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);

      DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
      light1.setInfluencingBounds(bounds);
      pg.addChild(light1);

      DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
      light2.setInfluencingBounds(bounds);
      pg.addChild(light2);

      viewingPlatform.setPlatformGeometry(pg);

      viewingPlatform.setNominalViewingTransform();

      if (!spin) {
      OrbitBehavior orbit = new OrbitBehavior(c,OrbitBehavior.REVERSE_ALL);
      BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
      orbit.setSchedulingBounds(bounds);
      viewingPlatform.setViewPlatformBehavior(orbit);     
      }
      u.addBranchGraph(scene);
           }


           public ObjLoad1(String[] args) {
                if (args.length != 0) {
                     for (int i = 0 ; i < args.length ; i++) {
                          if (args.startsWith("-")) {
                               if (args[i].equals("-s")) {
                                    spin = true;
                               } else if (args[i].equals("-n")) {
                                    noTriangulate = true;
                               } else if (args[i].equals("-t")) {
                                    noStripify = true;
                               } else if (args[i].equals("-c")) {
                                    if (i < args.length - 1) {
                                         creaseAngle = (new Double(args[++i])).doubleValue();
                                    } else usage();
                               } else {
                                    usage();
                               }
                          } else {
                               try {
                                    if ((args[i].indexOf("file:") == 0) ||
                                              (args[i].indexOf("http") == 0)) {
                                         filename = new URL(args[i]);
                                    }
                                    else if (args[i].charAt(0) != '/') {
                                         filename = new URL("file:./" + args[i]);
                                    }
                                    else {
                                         filename = new URL("file:" + args[i]);
                                    }
                               }
                               catch (MalformedURLException e) {
                                    System.err.println(e);
                                    System.exit(1);
                               }
                          }
                     }
                }
           }

      public void actionPerformed(ActionEvent e)
           {
           
           if (e.getSource() == Tit)
           {
      //Color Picker tool

                Color c1 = JColorChooser.showDialog(((Component)e.getSource()).getParent(),"Zaxis Color Picker", Color.blue);
                cardPanel.setBackground(c1);
                objRoot.removeChild(bgNode);
                int a = c1.getRed();
                int b = c1.getBlue();
                int c = c1.getBlue();
                System.out.println(a);
                System.out.println(b);
                System.out.println(c);
                Color3f ccc = new Color3f(a,b,c);
                bgNode.setApplicationBounds(bounds);
           
           objRoot.addChild(bgNode);
           }
           else
           {
                System.out.println("mathi");
           }
           }

      public ObjLoad1()
      {
      Tit = new Button("BG Color");
      sub = new Button("Object Color");

      cardPanel = new Panel();
      cardPanel.add(Tit);
      cardPanel.add(sub);
      //cardPanel.add(l1);
      //cardPanel.add(thelist);
      sub.addActionListener(this);
      Tit.addActionListener(this);
      // thelist.addActionListener(this);
      //setLayout for applet to be BorderLayout
      this.setLayout(new BorderLayout());
      //button Panel goes South, card panels go Center
      this.add(cardPanel, BorderLayout.SOUTH);
      //this.add(cardPanel, BorderLayout.CENTER);     
      this.setVisible(true);
      }

      public void destroy() {
           }
      public static void main(String[] args) {
           new MainFrame(new ObjLoad1(args),400, 400);
           }
      }
        • 1. Re: how to change object background color on  java run time
          796125
          To begin with, it's not necessary to remove the previous background and add a new one. Instead, set the background's
          ALLOW_COLOR_WRITE
          capability, and simply call the
          setColor(Color3f color)
          method.


          The only other thing I can think of: Is there any background node specified in the object file that you are loading? When bounding regions of background nodes intersect, the closest one is chosen. If they are both in the same position, I don't think Java3D guarantees which one will be shown. See the specification guide for more details

          http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dguide/LeafNodes.html#47362
          • 2. Re: how to change object background color on  java run time
            843799
            hi,

            i am using setColor(Color3f color) method

            like

            if (e.getSource() == Tit)
                 {
                      Color c1 = JColorChooser.showDialog(((Component)e.getSource()).getParent(),"Zaxis Color Picker", Color.blue);
                      bgColor = new Color3f(c1);
                      System.out.println(bgColor.get());
                 bgNode.setColor(bgColor);
                 bgNode.setApplicationBounds(bounds);
                 objRoot.addChild(bgNode);
                 
                 }

            but error will be displayed
            like
            javax.media.j3d.CapabilityNotSetException: Background: no capability to set color
                 at javax.media.j3d.Background.setColor(Background.java:307)
                 at ObjLoad1.actionPerformed(ObjLoad1.java:230)
                 at java.awt.Button.processActionEvent(Unknown Source)
                 at java.awt.Button.processEvent(Unknown Source)
                 at java.awt.Component.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.EventQueue.dispatchEvent(Unknown Source)
                 at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.run(Unknown Source)


            pls help me
            • 3. Re: how to change object background color on  java run time
              796125
              hi,

              i am using setColor(Color3f color) method

              but error will be displayed
              like
              javax.media.j3d.CapabilityNotSetException:
              Background: no capability to set color

              pls help me
              certainly. Basically, cannot modify an active scene graph unless you told Java3D that you would be modifying that aspect of it, before it became live.

              In order to be able to modify the background color, you first have to set the appropriate capabilities. For example:
              Background bg = new Background(new Color3f(Color.blue));
              bg.setCapability(Background.ALLOW_COLOR_WRITE);
              bg.setCapability(Background.ALLOW_COLOR_READ);
              someBranchGroup.add(bg);
              then, later, you shoud be able to do ths:
              bg.setColor(new Color3f(Color.red));
              - Adam
              • 4. Re: how to change object background color on  java run time
                843799
                Tank u adam!

                successfully color changes from run time

                regards,
                M.Mathivanan
                • 5. Re: how to change object background color on  java run time
                  843799
                  am make , openofice for xp
                  but error will be displayed
                  it ask to install java run time
                  where i can take it
                  pls help me


                  from
                  uton
                  • 6. Re: how to change object background color on  java run time
                    843799
                    HI!
                    I would like to Java source code make a , OpenOffice.org calc Format/page.../Page this Page Orientation Portrait Or Landscape. This is possible?
                    Thank you!