Areas
  • J3D FAQ
  • Tutorials
  • Utilities
  • Books
  • News
  • Other Sites
  •  Chapters
  • TOC
  • Getting Started
  • Lighting
  • Textures
  • Behaviours
  • User Feedback
  • Navigation
  • Audio
  • Input Devices
  • No Monitors
  •  Sections
  • Chapter TOC
  • Geometry Types
  • Backgrounder
  • The Primitives
  • Rasters
  • 3D Text
  • Compressed
  •   

    Example Code Outline

    © Justin Couch 1999

    For this chapter, we are going to make some minor changes to the code shown in Chapter 1. We start with the same GeometryExample class as before. This time, we are not going to immediately create some geometry by default but set it up so that we can swap items of raw geometry in and out on demand. This requires two changes to be made.

    Geometry Representation

    GeometryExample must first be capable of having multiple pieces of geometry set. To do this we set up the capability bits to allow us to write geometry information to the live Shape3D instance. As the geometry creation is no longer needed, we can remove the internal createGeometry() method completely. The constructor now becomes:
      public ExampleGeometry()
      {
        setCapability(ALLOW_GEOMETRY_WRITE);
    
        constructAppearance();
      }
    
    Next we need to provide some way of creating the geometry and passing it in. Several alternatives present themselves:
    • Externally create instances and pass them in. The caller instantiates the individual items in response to user input and passes them in.
    • Create all the objects internally within the geometry example and use some form of big switch statement to select the one to be added.
    • Have the goemetry class dynamically load them from outside the application. The caller would need to pass in some indicator about what to load.
    The final alternative makes for the most flexible code, but requires a lot of extra work for such a simple application. Also, as you will see later, the constructors of almost all these objects require arguments and instantiating classes with arguments takes a lot of work using reflection. The second alternative requires a lot of code to be packed into one class, but can be made relatively extensible. The first version places a lot of load on the external classes to make sure that they correctly instantiate the geometry but leaves geomtry implementation relatively free..

    We could pretty much pick any version of these, but we'll go with the middle one. This leaves all the trickery to the geometry class so that we can swap it out for another implementation when we come to later chapters and their examples.

    User Interface

    The second modifcation to our example applet requires us to have some way of selecting the geometry to display. Because we want to have the geometry a bit dynamic (we're adding examples as we go, not everything at once) the best way to select it is with either a menu or a panel of radio buttons. For simplicity, the menu wins because we don't need to alter the screen layout.

    To build the menu, we need a list of elements to choose from. So, to our test frame, add a new Menu for geometry. The contents of the menu will be strings that we've fetched from the GeometryExample class, through some new methods.

      private void createGeometryMenu(Menu menu)
      {
        // guaranteed this is always non-null
        String[] items = geometry.getAvailableItems();
    
        for(int i = 0; i < items.length; i++)
        {
          MenuItem menu_item = new IntAction(items[i], i);
          menu_item.addActionListener(this);
          menu.add(menu_item);
        }
      }
    
    We've created a little generic placeholder class called IntAction so that we can track the ID from the geometry class. This useful later when we need to signal it to open the particular geometry item. Like so:
      public void actionPerformed(ActionEvent evt)
      {
        Object src = evt.getSource();
    
        if(src == close_menu)
          System.exit(0);
        else if(src instanceof IntAction)
        {
          IntAction item = (IntAction)src;
          geomety.selectItem(item.getValue());
        }
      }
    

      

    [ TOC ] [ Home ] [ FAQ ] [ Books ] [ Tutorials ] [ Utilities ] [ Contact Us ]