📄 orbitbehavior.java
字号:
/* * $RCSfile: OrbitBehavior.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.5 $ * $Date: 2007/02/09 17:20:15 $ * $State: Exp $ */package com.sun.j3d.utils.behaviors.vp;import java.awt.event.ComponentEvent;import java.awt.event.MouseEvent;import java.awt.event.KeyEvent;import java.awt.AWTEvent;import java.awt.Component;import java.awt.Cursor;import javax.swing.SwingUtilities;import javax.media.j3d.WakeupOnAWTEvent;import javax.media.j3d.WakeupOnElapsedFrames;import javax.media.j3d.TransformGroup;import javax.media.j3d.Transform3D;import javax.media.j3d.View;import javax.media.j3d.Canvas3D;import javax.vecmath.Vector3d;import javax.vecmath.Point3d;import javax.vecmath.Matrix3d;import com.sun.j3d.utils.universe.ViewingPlatform;import com.sun.j3d.internal.J3dUtilsI18N;/** * Moves the View around a point of interest when the mouse is dragged with * a mouse button pressed. Includes rotation, zoom, and translation * actions. Zooming can also be obtained by using mouse wheel. * <p> * This behavior must be added to the ViewingPlatform * using the <code>ViewingPlatform.setViewPlatformBehavior</code> method. * <p> * The rotate action rotates the ViewPlatform around the point of interest * when the mouse is moved with the main mouse button pressed. The * rotation is in the direction of the mouse movement, with a default * rotation of 0.01 radians for each pixel of mouse movement. * <p> * The zoom action moves the ViewPlatform closer to or further from the * point of interest when the mouse is moved with the middle mouse button * pressed (or Alt-main mouse button on systems without a middle mouse button). * The default zoom action is to translate the ViewPlatform 0.01 units for each * pixel of mouse movement. Moving the mouse up moves the ViewPlatform closer, * moving the mouse down moves the ViewPlatform further away. * <p> * By default, the zoom action allows the ViewPlatform to move through * the center of rotation to orbit at a negative radius. * The <code>STOP_ZOOM</code> constructor flag will stop the ViewPlatform at * a minimum radius from the center. The default minimum radius is 0.0 * and can be set using the <code>setMinRadius</code> method. * <p> * The <code>PROPORTIONAL_ZOOM</code> constructor flag changes the zoom action * to move the ViewPlatform proportional to its distance from the center * of rotation. For this mode, the default action is to move the ViewPlatform * by 1% of its distance from the center of rotation for each pixel of * mouse movement. * <p> * The translate action translates the ViewPlatform when the mouse is moved * with the right mouse button pressed (Shift-main mouse button on systems * without a right mouse button). The translation is in the direction of the * mouse movement, with a default translation of 0.01 units for each pixel * of mouse movement. * <p> * The sensitivity of the actions can be scaled using the * <code>set</code><i>Action</i><code>Factor()</code> methods which scale * the default movement by the factor. The rotate and translate actions * have separate factors for x and y. * <p> * The actions can be reversed using the <code>REVERSE_</code><i>ACTION</i> * constructor flags. The default action moves the ViewPlatform around the * objects in the scene. The <code>REVERSE_</code><i>ACTION</i> flags can * make the objects in the scene appear to be moving in the direction * of the mouse movement. * <p> * The actions can be disabled by either using the * <code>DISABLE_</code><i>ACTION</i> constructor flags or the * <code>set</code><i>Action</i><code>Enable</code> methods. * <p> * The default center of rotation is (0, 0, 0) and can be set using the * <code>setRotationCenter()</code> method. * * @since Java 3D 1.2.1 */public class OrbitBehavior extends ViewPlatformAWTBehavior { private Transform3D velocityTransform = new Transform3D(); private Transform3D longditudeTransform = new Transform3D(); private Transform3D rollTransform = new Transform3D(); private Transform3D latitudeTransform = new Transform3D(); private Transform3D rotateTransform = new Transform3D(); // needed for integrateTransforms but don't want to new every time private Transform3D temp1 = new Transform3D(); private Transform3D temp2 = new Transform3D(); private Transform3D translation = new Transform3D(); private Vector3d transVector = new Vector3d(); private Vector3d distanceVector = new Vector3d(); private Vector3d centerVector = new Vector3d(); private Vector3d invertCenterVector = new Vector3d(); private double longditude = 0.0; private double latitude = 0.0; private double rollAngle = 0.0; private double startDistanceFromCenter = 20.0; private double distanceFromCenter = 20.0; private final double MAX_MOUSE_ANGLE = Math.toRadians( 3 ); private final double ZOOM_FACTOR = 1.0; private Point3d rotationCenter = new Point3d(); private Matrix3d rotMatrix = new Matrix3d(); private Transform3D currentXfm = new Transform3D(); private int mouseX = 0; private int mouseY = 0; private double rotXFactor = 1.0; private double rotYFactor = 1.0; private double transXFactor = 1.0; private double transYFactor = 1.0; private double zoomFactor = 1.0; private double xtrans = 0.0; private double ytrans = 0.0; private double ztrans = 0.0; private boolean zoomEnabled = true; private boolean rotateEnabled = true; private boolean translateEnabled = true; private boolean reverseRotate = false; private boolean reverseTrans = false; private boolean reverseZoom = false; private boolean stopZoom = false; private boolean proportionalZoom = false; private double minRadius = 0.0; private int leftButton = ROTATE; private int rightButton = TRANSLATE; private int middleButton = ZOOM; // the factor to be applied to wheel zooming so that it does not // look much different with mouse movement zooming. // This is a totally subjective factor. private float wheelZoomFactor = 50.0f; /** * Constructor flag to reverse the rotate behavior */ public static final int REVERSE_ROTATE = 0x010; /** * Constructor flag to reverse the translate behavior */ public static final int REVERSE_TRANSLATE = 0x020; /** * Constructor flag to reverse the zoom behavior */ public static final int REVERSE_ZOOM = 0x040; /** * Constructor flag to reverse all the behaviors */ public static final int REVERSE_ALL = (REVERSE_ROTATE | REVERSE_TRANSLATE | REVERSE_ZOOM); /** * Constructor flag that indicates zoom should stop when it reaches * the minimum orbit radius set by setMinRadius(). The minimus * radius default is 0.0. */ public static final int STOP_ZOOM = 0x100; /** * Constructor flag to disable rotate */ public static final int DISABLE_ROTATE = 0x200; /** * Constructor flag to disable translate */ public static final int DISABLE_TRANSLATE = 0x400; /** * Constructor flag to disable zoom */ public static final int DISABLE_ZOOM = 0x800; /** * Constructor flag to use proportional zoom, which determines * how much you zoom based on view's distance from the center of * rotation. The percentage of distance that the viewer zooms * is determined by the zoom factor. */ public static final int PROPORTIONAL_ZOOM = 0x1000; /** * Used to set the fuction for a mouse button to Rotate */ private static final int ROTATE = 0; /** * Used to set the function for a mouse button to Translate */ private static final int TRANSLATE = 1; /** * Used to set the function for a mouse button to Zoom */ private static final int ZOOM = 2; private static final double NOMINAL_ZOOM_FACTOR = .01; private static final double NOMINAL_PZOOM_FACTOR = 1.0; private static final double NOMINAL_ROT_FACTOR = .01; private static final double NOMINAL_TRANS_FACTOR = .01; private double rotXMul = NOMINAL_ROT_FACTOR * rotXFactor; private double rotYMul = NOMINAL_ROT_FACTOR * rotYFactor; private double transXMul = NOMINAL_TRANS_FACTOR * transXFactor; private double transYMul = NOMINAL_TRANS_FACTOR * transYFactor; private double zoomMul = NOMINAL_ZOOM_FACTOR * zoomFactor; /** * Parameterless constructor for this behavior. This is intended for use * by ConfiguredUniverse, which requires such a constructor for * configurable behaviors. The Canvas3D used to listen for mouse and * mouse motion events is obtained from the superclass * setViewingPlatform() method. * @since Java 3D 1.3 */ public OrbitBehavior() { super(MOUSE_LISTENER | MOUSE_MOTION_LISTENER | MOUSE_WHEEL_LISTENER); } /** * Creates a new OrbitBehavior * * @param c The Canvas3D to add the behavior to */ public OrbitBehavior(Canvas3D c) { this(c, 0 ); } /** * Creates a new OrbitBehavior * * @param c The Canvas3D to add the behavior to * @param flags The option flags */ public OrbitBehavior(Canvas3D c, int flags) { super(c, MOUSE_LISTENER | MOUSE_MOTION_LISTENER | MOUSE_WHEEL_LISTENER | flags ); if ((flags & DISABLE_ROTATE) != 0) rotateEnabled = false; if ((flags & DISABLE_ZOOM) != 0) zoomEnabled = false; if ((flags & DISABLE_TRANSLATE) != 0) translateEnabled = false; if ((flags & REVERSE_TRANSLATE) != 0) reverseTrans = true; if ((flags & REVERSE_ROTATE) != 0) reverseRotate = true; if ((flags & REVERSE_ZOOM) != 0) reverseZoom = true; if ((flags & STOP_ZOOM) != 0) stopZoom = true; if ((flags & PROPORTIONAL_ZOOM) !=0) { proportionalZoom = true; zoomMul = NOMINAL_PZOOM_FACTOR * zoomFactor; } } protected synchronized void processAWTEvents( final AWTEvent[] events ) { motion = false; for(int i=0; i<events.length; i++) if (events[i] instanceof MouseEvent) processMouseEvent( (MouseEvent)events[i] ); } protected void processMouseEvent( final MouseEvent evt ) { if (evt.getID()==MouseEvent.MOUSE_PRESSED) { mouseX = evt.getX(); mouseY = evt.getY(); motion=true; } else if (evt.getID()==MouseEvent.MOUSE_DRAGGED) { int xchange = evt.getX() - mouseX; int ychange = evt.getY() - mouseY; // rotate if (rotate(evt)) { if (reverseRotate) { longditude -= xchange * rotXMul; latitude -= ychange * rotYMul; } else { longditude += xchange * rotXMul; latitude += ychange * rotYMul; } } // translate else if (translate(evt)) { if (reverseTrans) { xtrans -= xchange * transXMul; ytrans += ychange * transYMul; } else { xtrans += xchange * transXMul; ytrans -= ychange * transYMul; } } // zoom else if (zoom(evt)) { doZoomOperations( ychange ); } mouseX = evt.getX(); mouseY = evt.getY(); motion = true; } else if (evt.getID()==MouseEvent.MOUSE_RELEASED ) { } else if (evt.getID()==MouseEvent.MOUSE_WHEEL ) { if (zoom(evt)) { // if zooming is done through mouse wheel, // the amount of increments the wheel changed, // multiplied with wheelZoomFactor is used, // so that zooming speed looks natural compared to mouse movement zoom. if ( evt instanceof java.awt.event.MouseWheelEvent){ // I/O differenciation is made between // java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL or // java.awt.event.MouseWheelEvent.WHEEL_BLOCK_SCROLL so
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -