![]() | ![]() | ![]() |
|
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
Geometry RepresentationGeometryExample 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:
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 InterfaceThe 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 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()); } } |
|