You can add clickable buttons in a 3D view.
For example, a button can follow an object to activate/deactivate a property or expand/collapse a slice ...
You can use the class cgJOGL3DButtonNode to add a button in the view. The node can be added anywhere in the scene graph like any other node. However, to keep a clean scene graph hierarchy, it is recommended to add the buttons in a dedicated container directly under the view root node.
The button is created with 3 kinds of information :
An icon (a java BufferedImage or Icon)
A location
A size (in pixels)
Note that the constructor takes an X,Y position and assumes that the position is defined as an absolute location in pixels.
However, it is possible to attach a button to a 3D location in the model space by using the setLocation(Point3d) and setLocationInModelSpace(boolean) method.
Alternatively, you can use a toggle button node : cgJOGL3DToggleButtonNode
It is possible to use a higher level node which will act as a toolbar : cgJOGLToolbarNode
This node defines a method to directly add an action (a java Action) : public cgJOGL3DButtonNode add(Action a) or public cgJOGL3DButtonNode add(Action a, boolean toggle) (in case you need to add an action associated to a toggle button).
These methods will create a new button node (or toggle button node) in the toolbar node hierarchy and associate the given action to the newly created button. The created button is returned in case you need to perform some operation on it.
The following code shows how to create a toolbar node which will show the same buttons as a standard Java toolbar.
Sample code
protected JToolBar createToolbar(cgJOGL3DPlotView view) { //create a Java toolbar JToolBar toolbar = new JToolBar(); //create a 3D toolbar node cgJOGLToolbarNode toolbarNode = useToolbarNode() ? new cgJOGLToolbarNode() : null; //add actions on the 2 toolbar ZoomAction zoomIn = new ZoomAction(2, view); toolbar.add(zoomIn); if (toolbarNode != null) { toolbarNode.add(zoomIn); } ZoomAction zoomOut = new ZoomAction(0.5, view); toolbar.add(zoomOut); if (toolbarNode != null) { toolbarNode.add(zoomOut); } ZoomToFullAction fit = new ZoomToFullAction(view); toolbar.add(fit); if (toolbarNode != null) { toolbarNode.add(fit); } if (_axisNode != null) { //add a toggle action toolbar.addSeparator(); ShowHideGridAction gridAction = new ShowHideGridAction(_axisNode); JToggleButton button = new JToggleButton(); button.setSelected(_axisNode.isVisible()); button.setAction(gridAction); button.setHideActionText(true); toolbar.add(button); if (toolbarNode != null){ cgJOGL3DToggleButtonNode buttonNode = (cgJOGL3DToggleButtonNode) toolbarNode.add(gridAction, true); buttonNode.setButtonSelected(_axisNode.isVisible()); } } ExportToImageAction export = new ExportToImageAction(_view); toolbar.add(export); if (toolbarNode != null) { toolbarNode.add(export); }
//Add the toolbar node in the scenegraph
if (toolbarNode != null && _view.getSceneGraph() instanceof cgJOGLCompositeNode) { ((cgJOGLCompositeNode) _view.getSceneGraph()).addNode(toolbarNode); } return toolbar; }
The location of the toolbar can be specified in pixels by using the setLocation(int x, int y) method.
The toolbar can be expanded or collapsed by using respectively the expand() and collapse() method.
Adding button nodes (or toolbar nodes) in the scene graph is not enough. You need to add a dedicated controller which will handle button "clicks."
This is simply done by adding a cgJOGL3DButtonNodeController to the view.
Sample code
cgJOGL3DButtonNodeController buttonClicker = new cgJOGL3DButtonNodeController(view); view.addMouseListener(buttonClicker); view.addMouseMotionListener(buttonClicker);