001: /*
002: * $RCSfile: ReadRaster.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.3 $
041: * $Date: 2007/02/09 17:21:51 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.read_raster;
046:
047: import java.applet.Applet;
048: import java.awt.BorderLayout;
049: import java.awt.event.*;
050: import java.awt.image.DataBufferInt;
051: import java.awt.image.BufferedImage;
052: import java.awt.GraphicsConfiguration;
053: import com.sun.j3d.utils.applet.MainFrame;
054: import com.sun.j3d.utils.geometry.ColorCube;
055: import com.sun.j3d.utils.universe.*;
056: import javax.media.j3d.*;
057: import javax.vecmath.*;
058: import java.util.Enumeration;
059:
060: public class ReadRaster extends Applet {
061:
062: private SimpleUniverse u = null;
063:
064: public BranchGroup createSceneGraph(BufferedImage bImage,
065: Raster readRaster) {
066:
067: // Create the root of the branch graph
068: BranchGroup objRoot = new BranchGroup();
069:
070: // Create a Raster shape. Add it to the root of the subgraph
071:
072: ImageComponent2D drawImageComponent = new ImageComponent2D(
073: ImageComponent.FORMAT_RGB, bImage, true, true);
074:
075: Raster drawRaster = new Raster(new Point3f(0.0f, 0.0f, 0.0f),
076: Raster.RASTER_COLOR, 0, 0, bImage.getWidth(), bImage
077: .getHeight(), drawImageComponent, null);
078: Shape3D shape = new Shape3D(drawRaster);
079: drawRaster.setCapability(Raster.ALLOW_IMAGE_WRITE);
080: objRoot.addChild(shape);
081:
082: // Ceate the transform greup node and initialize it to the
083: // identity. Enable the TRANSFORM_WRITE capability so that
084: // our behavior code can modify it at runtime. Add it to the
085: // root of the subgraph.
086: TransformGroup objTrans = new TransformGroup();
087: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
088:
089: TransformGroup cubeScale = new TransformGroup();
090: Transform3D t3d = new Transform3D();
091: t3d.setTranslation(new Vector3d(-0.5, 0.5, 0.0));
092: cubeScale.setTransform(t3d);
093:
094: cubeScale.addChild(objTrans);
095: objRoot.addChild(cubeScale);
096:
097: // Create a simple shape leaf node, add it to the scene graph.
098: objTrans.addChild(new ColorCube(0.3));
099:
100: // Create a new Behavior object that will perform the desired
101: // operation on the specified transform object and add it into
102: // the scene graph.
103: Transform3D yAxis = new Transform3D();
104: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
105: 0, 4000, 0, 0, 0, 0, 0);
106: myRotationInterpolator rotator = new myRotationInterpolator(
107: drawRaster, readRaster, rotationAlpha, objTrans, yAxis,
108: 0.0f, (float) Math.PI * 2.0f);
109: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
110: 0.0, 0.0), 100.0);
111: rotator.setSchedulingBounds(bounds);
112: objTrans.addChild(rotator);
113:
114: // Have Java 3D perform optimizations on this scene graph.
115: objRoot.compile();
116:
117: return objRoot;
118: }
119:
120: public ReadRaster() {
121: }
122:
123: public void init() {
124:
125: int width = 128;
126: int height = 128;
127:
128: ImageComponent2D readImageComponent = new ImageComponent2D(
129: ImageComponent.FORMAT_RGB, width, height, false, true);
130:
131: Raster readRaster = new Raster(new Point3f(0.0f, 0.0f, 0.0f),
132: Raster.RASTER_COLOR, 0, 0, width, height,
133: readImageComponent, null);
134:
135: setLayout(new BorderLayout());
136: GraphicsConfiguration config = SimpleUniverse
137: .getPreferredConfiguration();
138:
139: Canvas3D c = new myCanvas3D(config, readRaster);
140: add("Center", c);
141:
142: // Create a simple scene and attach it to the virtual universe
143: BufferedImage bImage = new BufferedImage(width, height,
144: BufferedImage.TYPE_INT_RGB);
145:
146: BranchGroup scene = createSceneGraph(bImage, readRaster);
147: u = new SimpleUniverse(c);
148:
149: // This will move the ViewPlatform back a bit so the
150: // objects in the scene can be viewed.
151: u.getViewingPlatform().setNominalViewingTransform();
152:
153: u.addBranchGraph(scene);
154: }
155:
156: public void destroy() {
157: u.cleanup();
158: }
159:
160: //
161: // The following allows ReadRaster to be run as an application
162: // as well as an applet
163: //
164: public static void main(String[] args) {
165: new MainFrame(new ReadRaster(), 256, 256);
166: }
167: }
168:
169: class myCanvas3D extends Canvas3D {
170:
171: Raster readRaster;
172: GraphicsContext3D gc;
173:
174: public myCanvas3D(GraphicsConfiguration graphicsConfiguration,
175: Raster readRaster) {
176:
177: super (graphicsConfiguration);
178: this .readRaster = readRaster;
179: gc = getGraphicsContext3D();
180: }
181:
182: public void postSwap() {
183: super .postSwap();
184: synchronized (readRaster) {
185: gc.readRaster(readRaster);
186: }
187: }
188: }
189:
190: class myRotationInterpolator extends RotationInterpolator {
191: Point3f wPos = new Point3f(0.025f, -0.025f, 0.0f);
192: Raster drawRaster;
193: Raster readRaster;
194: BufferedImage bImage;
195: ImageComponent2D newImageComponent;
196:
197: public myRotationInterpolator(Raster drawRaster, Raster readRaster,
198: Alpha alpha, TransformGroup target,
199: Transform3D axisOfRotation, float minimumAngle,
200: float maximumAngle) {
201:
202: super (alpha, target, axisOfRotation, minimumAngle, maximumAngle);
203: this .drawRaster = drawRaster;
204: this .readRaster = readRaster;
205: }
206:
207: public void processStimulus(Enumeration criteria) {
208:
209: synchronized (readRaster) {
210: bImage = readRaster.getImage().getImage();
211: }
212: newImageComponent = new ImageComponent2D(
213: ImageComponent.FORMAT_RGB, bImage, true, true);
214: drawRaster.setImage(newImageComponent);
215: super.processStimulus(criteria);
216: }
217: }
|