![]() ![]()
|
7
Programming User Interaction
Default User Interaction
Listeners
Mapping and Picking
dragZValue Method
gridValue Method
All elements mentioned in this chapter refer to both the Java 2 API and the
Java 3D API, unless specifically noted.
This chapter describes the user-interaction features of JClass Chart 3D - how a user can interact with the chart and how an application can control interaction.
7.1 Default User Interaction
By default, an end-user can rotate, translate, scale, and zoom into the plot cube. The rotate action is implemented by manipulating the three rotation angles of the
JCView3dclass. Scaling, translating, and zooming are achieved by appropriately setting the scale, horizontal shift, and vertical shift properties of the JCViewport class.Figure 24 shows the user interactions enabled by JClass Chart 3D's default translations.
Figure 24 : JClass Chart 3D's default user interactions (MB1 Mouse Button 1).
Note that the blank preview cube gets drawn only if the
previewMethodproperty of theJCViewportclass is set toPREVIEW_CUBEand you are using the Java 2 version of JClass Chart 3D.
7.1.1 JCViewport class
The
JCViewportclass stores information concerning the viewport through which the user views the plot cube. The default viewport is determined by JClass Chart 3D. The user can modify this default by scaling it and translating it within theJCChart3dArea.JCViewportcomprises these properties:
- horizontalShift - indicates horizontal shift as a multiple of the original viewport size.
- verticalShift - indicates vertical shift as a multiple of the original viewport size.
- normalized - indicates whether the viewport is normalized (that is, is the scale equal to 1 and the shift equal to 0).
- previewMethod - controls what is drawn as the user rotates scales, or translates the cube interactively; can either be PREVIEW_CUBE (displays a wireframe cube [default]) or PREVIEW_FULL (displays the entire surface).
- scale - zoom factor. A value less than 1 means a viewport smaller than the default. A value greater than 1 means a viewport greater than the default. The scale value must be between MIN_VIEW_SCALE and MAX_VIEW_SCALE.
Rotation
The rotate actions manipulate the
xRotation,yRotation, andzRotationproperties of theJCView3dclass in various ways. The standard rotation (no selected axis) implements a track ball type rotation. This causes the chart to rotate in the direction of the mouse. You can also rotate about one of the three standard axes (X, Y, or Z). There is also one more type of rotation, called eye rotation, that allows you to rotate about a line from the center of the cube to the user's eye position.
7.1.2 JCActionTable Class
The
JCActionTableclass is responsible for keeping track of the mappings from a user input event to a JClass Chart 3D action. For example, an association can be made to map from the Shift+left mouse button set of events to theTranslateaction.JClass Chart 3D has a rich set of predefined associations that handle scaling, translation, rotation, and many other functions. To enable the standard set of actions, add the following line of code to your program:
chart3d.getActionTable().addAllDefaultActions();
JCActionTablestores a set of mappings between anActionInitiatorand the name of a class that implements theJCActioninterface. AnActionInitiatorcaptures the essence of a user input event via its subclasses -KeyActionInitiatorandMouseActionInitiator. AMouseActionInitiatordefines the mouse button and modifiers that the chart will recognize when the user presses a mouse button. For example, JClass Chart 3D'sRotatemethod is invoked when the user presses the left mouse button, while holding down the Ctrl key.You can define a
MouseActionInitiator mai = new MouseActionInitiator(MouseEvent.MouseActionInitiatorfor this combination:
BUTTON1_MASK, InputEvent.CTRL_MASK);You can associate the
Rotatemethod with the initiator by specifying its pathname as a String. For users of the Java 2 version of JClass Chart 3D, the pathname iscom.klg.jclass.chart3d.j2d.actions.RotateAction. For users of the Java 3D version of JClass Chart 3D, the pathname iscom.klg.jclass.chart3d. j3d.actions.RotateAction.Both of the above
RotateActionclasses implement theJCActioninterface, with the most important methods being thestart(),animate(), andend()methods. These correspond to the user's mouse button down, drag, and release events.When JClass Chart 3D detects that the user has pressed the left mouse button, it will dynamically instantiate the specified class, and use it to handle the user's mouse events. To minimize memory usage, this class will be created only once and then be reused for subsequent interactions.
Adding and removing individual actions
JClass Chart 3D's
JCActionTable at = chart3d.getActionTable();JCActionTabledefines all of the commonly used interactions, so individual actions can be added or removed, and custom actions can be added. For example, to add the aboveRotatemethod, simply use:
at.addAction(JCActionTable.DEFAULT_MOUSE_ROTATE_ANY_ACTION, at.getDefaultRotateActionClass());A
KeyActionInitiator key = new KeyActionInitiator(KeyEvent.VK_R,KeyActionInitiatordefines the keyCode and modifiers that JClass Chart 3D will recognize when the user presses a key. For example, if you want to detect when the user presses the Shift-r key combination, create aKeyActionInitiatorwith the following code:
InputEvent.SHIFT_MASK);To make this key combination invoke the
JCActionTable at = chart3d.getActionTable();Resetaction, use:
at.addAction(key, at.getDefaultResetActionClass());Custom actions
Custom actions can be added by writing a class that implements the
JCActionTable at = chart3d.getActionTable();JCActioninterface, and associating it with anActionInitiatorby using theaddAction()method and specifying its name as a String. For example, if you wanted to associate the above key with your own action handler that existed incom.yourcompany.yourproduct.Reset.class, you would specify:
at.addAction(key, "com.yourcompany.yourproduct.Reset");If you later wanted to remove this specific action, use:
chart3d.getActionTable().removeAction(key);To remove all actions, including default and custom actions, use:
chart3d.getActionTable().removeAllActions();The full set of default actions and mouse and key combinations are described in the
JCActionTableclass description in the API Reference Javadocs, which are included when you purchase JClass Chart 3D.For an overview of JClass actions, please review the following table. Note that except for the Edit action, these are the actions that get set on the chart when the
addAllDefaultActionsmethod is called.
Action Name
Default Binding
Description
Mouse Button(MB)
Key
* Not available for the Java 3D API version of JClass Chart 3D
7.1.3 JCChart3dEvent class
The
JCChart3dEventclass is used to encapsulate an action (for example, rotate, zoom, or scale) on a JClass Chart 3D. It holds the chart that was acted upon. TheJCChart3dEventclass gets sent to all Chart3D listeners through thechangeChart()method (see below).
7.2 Listeners
JClass Chart 3D uses the standard AWT/Swing event/listener mechanism to tell an application that certain events have happened on the chart. A listener is called when these events have completed.
There are three listeners in JClass Chart 3D, each of which is discussed below:
Data Listener
The
Chart3dDataListenerinterface is a template for event listener interfaces for chart data events. Its method,chart3dDataChange, is called whenever the JClass Chart 3D data has changed; interested listeners should implement this method and register the object with a data source that implements theChart3dDataManagerinterface.Chart3d Listener
The
JCChart3dListenerinterface is the event listener interface for JClass Chart 3D events. It has two methods. ThechangeChartmethod is called whenever the chart has been changed through a user action. ThesendEvent()method of theJCChart3dclass tells interested listeners that the chart has changed once the action is completed. Interested listeners should implement this method and register the object with JClass Chart 3D. ThepaintChartmethod is called whenever the paint method of theJCChart3dobject is called.Pick Listener
The
JCPick3dListenerinterface is the event listener interface for chart pick events. It has a single method calledpick, which is called whenever a JClass Chart 3D has been picked. Interested listeners should implement this method and register the object with JClass Chart 3D.
7.3 Mapping and Picking
Mapping
The
map()method of theChart3dDataViewobject takes (x, y) pixel coordinates and maps them to a point in data space. For grid data, this is a point interpolated from the nearest grid values. For point data, it is the nearest data point. If the pixel point is not within the chart, a point with its x, y, and z values set toDouble.MAX_VALUEis returned. The user can also call theChart3dDataView'scoordToDataCoord()method which is a wrapper around themap()method.Unmapping
Unmapping is the opposite of mapping. It maps from data space coordinates to pixel coordinates. It basically applies the current data transformation that JClass Chart 3D uses to transform the data point to pixels.
Chart3dDataView'sdataCoordToCoordmethod is a wrapper around itsunmap()method.JCData3dIndex class
The
JCData3dIndexclass contains a unique index to an object in JClass Chart 3D consisting of either an (x,y) grid data value, a (series, point) point data value, a label, or a contour range depending on whether the index is of typeJCData3dGridIndex,JCData3dPointIndex,JCData3dLabelIndex, orJCData3dContourIndex, respectively. This class is used by the JClass Chart 3D'spick()andunpick()methods and contains information related to these operations.Picking
The
pick()method of JClass Chart 3D takes an (x, y) pixel position and selects the internal component in which it is contained. This could be the header, the footer, the chart3dArea, or the legend. The object property of the returnedJCData3dIndexindicates which component has been selected. If the pixel position is not contained in any of these components,nullis returned. Note that the pixel position is assumed to be relative to the JClass Chart 3D component, not the internal components.If the
chart3dAreais selected,pickreturns the index of the point closest to the specified pixel position via the returnedJCData3dIndexobject. This object also contains the distance from the pixel position to the selected point. If the pixel position misses the chart, the index returned will have each of its indices set to -1.If a label is selected,
pickreturns aJCData3dLabelIndexwhich contains the internal value label of the selected label, as well as the index of that label within the internal value labels array. (See Label Selection and Clustering, in Chapter 2 for more information.)Note: Label selection and coloring is currently not available in the Java 3D API version of JClass Chart 3D.
If the legend is selected,
pickreturns aJCData3dContourIndexif contour level element appears in the legend (grid data only). For point data, aJCData3dPointIndexobject with the selected series is returned.
Chart3dDataView'scoordToDataIndexmethod is a convenient wrapper for picking on the chart3dArea.Unpicking
The
unpickmethod is the opposite ofpick. It determines the pixel position for a givenJCData3dGridIndexorJCData3dPointIndexobject (the type of the index needs to match the type of the currentelevationData).Chart3dDataView'sdataIndexToCoordmethod is a convenient wrapper for theunpickmethod.
7.4 dragZValue Method
The
dragZValuemethod in theChart3dDataViewclass finds a new Z value for a given point index based on a given pixel position. Given a start point A (for grid data, the index refers to a point on the grid; for point data, it refers to one of the points in the list of series) and a point P on the screen, project the line AP (in 3D-space) onto the line through A parallel to the Z axis to find the Z value that corresponds to P on the projected line.The
dragZValuemethod returns the new computed z-value. This method takes several parameters:
data- the data for which this operation is to take place (an instance of eitherChart3dGridDataorChart3dPointData).index- the data index of the point; for grid data, this must be an instance ofJCData3dGridIndex, which corresponds to an X- and Y-grid position specification, while for point data, this must be an instance ofJCData3dPointIndexwhich corresponds to the series and point number of the point.- both the X- and Y-value of the screen position (in relation to the chart component, not the
chart3dAreacomponent).This procedure can be used to support the interactive modification of a grid or point value. For example, when the user clicks somewhere on the chart, it calls the
pick()method of theJCChart3dobject to determine the index of the closest point. Then as the mouse is dragged, new pixel values are passed along with the chosen index todragZValue(), which returns the new Z value for the index. The new Z value can be set on the data source and the chart updated. The edit action contains a built-in implementation of this mechanism.
7.5 gridValue Method
The
gridValuemethod in theChart3dDataViewclass returns an estimate of the surface value at (x, y) calculated using bilinear interpolation. The method finds the four closest grid points, interpolates, and returns the estimated Z value. The parameters are:
![]() ![]()
|