Source Code Cross Referenced for Shaft.java in  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3d » examples » gears » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » java 3d » org.jdesktop.j3d.examples.gears 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $RCSfile: Shaft.java,v $
003:         *
004:         * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * Redistribution and use in source and binary forms, with or without
007:         * modification, are permitted provided that the following conditions
008:         * are met:
009:         *
010:         * - Redistribution of source code must retain the above copyright
011:         *   notice, this list of conditions and the following disclaimer.
012:         *
013:         * - Redistribution in binary form must reproduce the above copyright
014:         *   notice, this list of conditions and the following disclaimer in
015:         *   the documentation and/or other materials provided with the
016:         *   distribution.
017:         *
018:         * Neither the name of Sun Microsystems, Inc. or the names of
019:         * contributors may be used to endorse or promote products derived
020:         * from this software without specific prior written permission.
021:         *
022:         * This software is provided "AS IS," without a warranty of any
023:         * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024:         * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025:         * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026:         * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027:         * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028:         * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029:         * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030:         * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031:         * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032:         * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033:         * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034:         * POSSIBILITY OF SUCH DAMAGES.
035:         *
036:         * You acknowledge that this software is not designed, licensed or
037:         * intended for use in the design, construction, operation or
038:         * maintenance of any nuclear facility.
039:         *
040:         * $Revision: 1.2 $
041:         * $Date: 2007/02/09 17:21:38 $
042:         * $State: Exp $
043:         */
044:
045:        package org.jdesktop.j3d.examples.gears;
046:
047:        import java.lang.Math.*;
048:        import javax.media.j3d.*;
049:        import javax.vecmath.*;
050:
051:        public class Shaft extends javax.media.j3d.TransformGroup {
052:
053:            /**
054:             * Construct a Shaft;
055:             * @return a new shaft that with the specified radius centered about
056:             * the origin an laying in the XY plane and of a specified length
057:             * extending in the Z dimension
058:             * @param radius radius of shaft
059:             * @param length shaft length shaft extends from -length/2 to length/2 in
060:             * the Z dimension
061:             * @param segmentCount number of segments for the shaft face
062:             * @param look the Appearance to associate with this shaft
063:             */
064:            public Shaft(float radius, float length, int segmentCount,
065:                    Appearance look) {
066:                // The direction of the ray from the shaft's center
067:                float xDirection, yDirection;
068:                float xShaft, yShaft;
069:
070:                // The z coordinates for the shaft's faces (never change)
071:                float frontZ = -0.5f * length;
072:                float rearZ = 0.5f * length;
073:
074:                int shaftFaceVertexCount; // #(vertices) per shaft face
075:                int shaftFaceTotalVertexCount; // total #(vertices) in all teeth
076:                int shaftFaceStripCount[] = new int[1]; // per shaft vertex count
077:                int shaftVertexCount; // #(vertices) for shaft
078:                int shaftStripCount[] = new int[1]; // #(vertices) in strip/strip
079:
080:                // Front and rear facing normals for the shaft's faces
081:                Vector3f frontNormal = new Vector3f(0.0f, 0.0f, -1.0f);
082:                Vector3f rearNormal = new Vector3f(0.0f, 0.0f, 1.0f);
083:                // Outward facing normal
084:                Vector3f outNormal = new Vector3f(1.0f, 0.0f, 0.0f);
085:
086:                // Temporary variables for storing coordinates and vectors 
087:                Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
088:                Shape3D newShape;
089:
090:                // The angle subtended by a single segment
091:                double segmentAngle = 2.0 * Math.PI / segmentCount;
092:                double tempAngle;
093:
094:                // Allow this object to spin. etc.
095:                this .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
096:
097:                /* for the forward facing fan:
098:                 *               ___3___
099:                 *             -    |    -
100:                 *          /       |       \
101:                 *       4/\        |        /\2
102:                 *     /     \      |      /     \
103:                 *   /         \    |    /         \
104:                 *  :            \  |  /            :
105:                 * |--------------- *----------------|
106:                 * 5                0                1
107:                 *
108:                 * for backward facing fan exchange 1 with 5; 2 with 4, etc.
109:                 */
110:
111:                // Construct the shaft's front and rear face
112:                shaftFaceVertexCount = segmentCount + 2;
113:                shaftFaceStripCount[0] = shaftFaceVertexCount;
114:
115:                TriangleFanArray frontShaftFace = new TriangleFanArray(
116:                        shaftFaceVertexCount, GeometryArray.COORDINATES
117:                                | GeometryArray.NORMALS, shaftFaceStripCount);
118:
119:                TriangleFanArray rearShaftFace = new TriangleFanArray(
120:                        shaftFaceVertexCount, GeometryArray.COORDINATES
121:                                | GeometryArray.NORMALS, shaftFaceStripCount);
122:
123:                coordinate.set(0.0f, 0.0f, frontZ);
124:                frontShaftFace.setCoordinate(0, coordinate);
125:                frontShaftFace.setNormal(0, frontNormal);
126:
127:                coordinate.set(0.0f, 0.0f, rearZ);
128:                rearShaftFace.setCoordinate(0, coordinate);
129:                rearShaftFace.setNormal(0, rearNormal);
130:
131:                for (int index = 1; index < segmentCount + 2; index++) {
132:
133:                    tempAngle = segmentAngle * -(double) index;
134:                    coordinate.set(radius * (float) Math.cos(tempAngle), radius
135:                            * (float) Math.sin(tempAngle), frontZ);
136:                    frontShaftFace.setCoordinate(index, coordinate);
137:                    frontShaftFace.setNormal(index, frontNormal);
138:
139:                    tempAngle = -tempAngle;
140:                    coordinate.set(radius * (float) Math.cos(tempAngle), radius
141:                            * (float) Math.sin(tempAngle), rearZ);
142:                    rearShaftFace.setCoordinate(index, coordinate);
143:                    rearShaftFace.setNormal(index, rearNormal);
144:                }
145:                newShape = new Shape3D(frontShaftFace, look);
146:                this .addChild(newShape);
147:                newShape = new Shape3D(rearShaftFace, look);
148:                this .addChild(newShape);
149:
150:                // Construct shaft's outer skin (the cylinder body)
151:                shaftVertexCount = 2 * segmentCount + 2;
152:                shaftStripCount[0] = shaftVertexCount;
153:
154:                TriangleStripArray shaft = new TriangleStripArray(
155:                        shaftVertexCount, GeometryArray.COORDINATES
156:                                | GeometryArray.NORMALS, shaftStripCount);
157:
158:                outNormal.set(1.0f, 0.0f, 0.0f);
159:
160:                coordinate.set(radius, 0.0f, rearZ);
161:                shaft.setCoordinate(0, coordinate);
162:                shaft.setNormal(0, outNormal);
163:
164:                coordinate.set(radius, 0.0f, frontZ);
165:                shaft.setCoordinate(1, coordinate);
166:                shaft.setNormal(1, outNormal);
167:
168:                for (int count = 0; count < segmentCount; count++) {
169:                    int index = 2 + count * 2;
170:
171:                    tempAngle = segmentAngle * (double) (count + 1);
172:                    xDirection = (float) Math.cos(tempAngle);
173:                    yDirection = (float) Math.sin(tempAngle);
174:                    xShaft = radius * xDirection;
175:                    yShaft = radius * yDirection;
176:                    outNormal.set(xDirection, yDirection, 0.0f);
177:
178:                    coordinate.set(xShaft, yShaft, rearZ);
179:                    shaft.setCoordinate(index, coordinate);
180:                    shaft.setNormal(index, outNormal);
181:
182:                    coordinate.set(xShaft, yShaft, frontZ);
183:                    shaft.setCoordinate(index + 1, coordinate);
184:                    shaft.setNormal(index + 1, outNormal);
185:                }
186:                newShape = new Shape3D(shaft, look);
187:                this.addChild(newShape);
188:            }
189:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.