summaryrefslogtreecommitdiffstats
path: root/src/GeometryByReference/ImageComponentByReferenceTest.java
blob: 24f20f88308bd39b7252e237869f88233b5a3770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * $RCSfile$
 *
 * Copyright (c) 2005 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$
 * $Date$
 * $State$
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.ColorCube;
import java.awt.image.*;
import java.awt.color.ColorSpace;
import com.sun.j3d.utils.geometry.*;

public class ImageComponentByReferenceTest extends JApplet implements ActionListener {

    Shape3D s1,s2;
    TextureLoader t0, t1, t2;
    int count = 0;

    Appearance app = new Appearance();
    BranchGroup objRoot = new BranchGroup();
    TransformGroup objTrans = new TransformGroup();
    BufferedImage bImage1;
    TiledImage checkBoard;
    boolean yUp = false;
    boolean byRef = true;
    JComboBox rasterType, texType;
    ImageComponent2D[] image = new ImageComponent2D[8];
    Appearance dummyApp = new Appearance();
    Texture2D texOne, texCheckBoard;
    javax.media.j3d.Raster raster;
    Box textureCube;
    Shape3D boxShape;
    int w1 = 64, h1 = 32, checkw = 16 , checkh = 16;

    private java.net.URL texImage = null;

    private SimpleUniverse u = null;
    
    public BranchGroup createSceneGraph() {
        objRoot = new BranchGroup();
 
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);

        objRoot.addChild(objTrans);
 
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

	app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);


        textureCube = new Box(0.4f, 0.4f, 0.4f,
                                  Box.GENERATE_TEXTURE_COORDS|
				  Box.GENERATE_NORMALS, app);
	boxShape = textureCube.getShape(Box.FRONT);
	boxShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
	objTrans.addChild(textureCube);

	checkBoard = new TiledImage();
	TextureLoader texLoader = new TextureLoader( texImage, this);
	ImageComponent2D oneImage = texLoader.getImage();
	bImage1 = oneImage.getImage();

	int index = 0;
	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      false,
				      true);

	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      true,
				      true);


	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      false,
				      false);
	
	
	image[index++] =  new ImageComponent2D(oneImage.getFormat(),
				      (RenderedImage)bImage1,
				      true,
				      false);

	createRaster(objRoot);

	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      false,
				      true);

	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      true,
				      true);


	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      false,
				      false);
	
	
	image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
				      checkBoard,
				      true,
				      false);



	texOne = new Texture2D(Texture.BASE_LEVEL,
				      Texture.RGBA,
				      image[2].getWidth(), image[2].getHeight());

	texOne.setCapability(Texture.ALLOW_IMAGE_WRITE);
	texOne.setImage(0, image[2]);
	
        app.setTexture(texOne);

	texCheckBoard = new Texture2D(Texture.BASE_LEVEL,
				      Texture.RGBA,
				      image[4].getWidth(), image[4].getHeight());
	
	texCheckBoard.setCapability(Texture.ALLOW_IMAGE_WRITE);
        objRoot.compile();
	return objRoot;
    }

    public void actionPerformed(ActionEvent e ) {
	Object target = e.getSource();

	if (target == rasterType) {
	    if (rasterType.getSelectedIndex() < 4) {
		raster.setSize(w1, h1);
	    }
	    else {
		raster.setSize(checkw, checkh);
	    }
	    raster.setImage(image[rasterType.getSelectedIndex()]);
	}
	else if (target == texType) {
	    boxShape.setAppearance(dummyApp);
	    if (texType.getSelectedIndex() < 4) {
		texOne.setImage(0, image[texType.getSelectedIndex()]);
		app.setTexture(texOne);
	    }
	    else {
		texCheckBoard.setImage(0, image[texType.getSelectedIndex()]);
		app.setTexture(texCheckBoard);
	    }
		
	    boxShape.setAppearance(app);
	}


    }

    JPanel createImagePanel() {
	JPanel panel = new JPanel();
	String texVals[] = { "One_Yup_ByCopy",
			     "One_Yup_ByReference",
			     "One_Ydown_ByCopy",
			     "One_Ydown_ByReference",
			     "Checkered_Yup_ByCopy",
			     "Checkered_Yup_ByReference",
			     "Checkered_Ydown_ByCopy",
			     "Checkered_Ydown_ByReference"};
	
	rasterType = new JComboBox(texVals);
	rasterType.setLightWeightPopupEnabled(false);
	rasterType.addActionListener(this);
	rasterType.setSelectedIndex(2);
	panel.add(new JLabel("Raster Image")); 	
	panel.add(rasterType);

	texType = new JComboBox(texVals);
	texType.setLightWeightPopupEnabled(false);
	texType.addActionListener(this);
	texType.setSelectedIndex(2);
	panel.add(new JLabel("Texture Image")); 	
	panel.add(texType);
	return panel;

    }



    public ImageComponentByReferenceTest() 
    {
    }

    public ImageComponentByReferenceTest(java.net.URL url) {
        texImage = url;
    }

    public void init() {
        if (texImage == null) {
	    // the path to the image for an applet
	    try {
	        texImage = new java.net.URL(getCodeBase().toString() +
					    "../images/one.jpg");
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}

        Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
	BranchGroup scene = createSceneGraph();u = new SimpleUniverse(c);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);
	Container contentPane = getContentPane();
	JPanel p = new JPanel();
	BoxLayout boxlayout = new BoxLayout(p, 
					    BoxLayout.Y_AXIS);
	p.setLayout(boxlayout);
        contentPane.add("Center", c);
  
	contentPane.add("South", p);
	
	p.add(createImagePanel());

    }

    public void destroy() {
	u.cleanup();
    }
 
    public static void main(String[] args) {
        java.net.URL url = null;
        // the path to the image file for an application
	try {
	    url = new java.net.URL("file:../images/one.jpg");
	}
	catch (java.net.MalformedURLException ex) {
	    System.out.println(ex.getMessage());
	    System.exit(1);
	}

        new MainFrame(new ImageComponentByReferenceTest(url), 800, 700);
    }

   void createRaster( BranchGroup scene) {



	// Create raster geometries and shapes
	Vector3f trans = new Vector3f( );
	Transform3D tr = new Transform3D( );
	TransformGroup tg;

	// Left
	raster = new javax.media.j3d.Raster( );
	raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE);
	raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE);
	raster.setPosition( new Point3f( -0.9f, 0.75f, 0.0f ) );
	raster.setType( javax.media.j3d.Raster.RASTER_COLOR );
	raster.setOffset( 0, 0 );

	raster.setSize( image[2].getWidth(), image[2].getHeight() );
	raster.setImage( image[2] );
	Shape3D sh = new Shape3D( raster, new Appearance( ) );
	scene.addChild( sh );



    }
}