001: /*
002: * $RCSfile: ImageComponent2DURL.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/03/29 21:13:42 $
042: * $State: Exp $
043: */
044: package org.jdesktop.j3d.utils.scenegraph;
045:
046: import com.sun.j3d.utils.scenegraph.io.SceneGraphStateProvider;
047: import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.SceneGraphObjectState;
048: import java.awt.image.BufferedImage;
049: import java.io.IOException;
050: import java.net.URL;
051: import java.util.ArrayList;
052: import javax.imageio.ImageIO;
053: import javax.media.j3d.ImageComponent2D;
054: import org.jdesktop.j3d.utils.scenegraph.io.ImageComponent2DURLState;
055:
056: /**
057: *
058: * A subclass of ImageComponent2D that includes a base URL and image name. There
059: * is also an associated state class for SceneGraphIO so when this node is saved
060: * only the baseURL and imageName are written, not the image itself. A simple
061: * AssetManager is provided which will load the images from the combined URL and
062: * imageName. Users can replace the asset manager with to provide more advanced
063: * handling if required.
064: *
065: * <p>
066: * The imageName may contain both the filename and a directory hierarchy
067: * ie
068: * <p>
069: * baseURL http://server.com/textures
070: * imageName material/stone/flagstone.jpg
071: *
072: * @author paulby
073: */
074: public class ImageComponent2DURL extends
075: javax.media.j3d.ImageComponent2D implements
076: SceneGraphStateProvider {
077:
078: private java.net.URL baseURL = null;
079: private String imageName = null;
080:
081: private static AssetManager assetManager = new SimpleAssetManager();
082:
083: /**
084: * Special constructor that creates a new ImageComponent2DURL from the provided
085: * ImageComponent2D. This constructor will not call the AssetManager to load the
086: * image, instead taking the image from the provide ImageComponent2D.
087: */
088: public ImageComponent2DURL(ImageComponent2D ic,
089: java.net.URL baseURL, String imageName) {
090: super (ic.getFormat(), ic.getImage(), ic.isByReference(), ic
091: .isYUp());
092: this .baseURL = baseURL;
093: this .imageName = imageName;
094: set(ic.getImage());
095: }
096:
097: /**
098: * Constructs a 2D image component object using the specified
099: * format, width, height, byReference flag, and yUp flag.
100: * Default values are used for all other parameters.
101: *
102: * @param format the image component format, one of: FORMAT_RGB,
103: * FORMAT_RGBA, etc.
104: * @param width the number of columns of pixels in this image component
105: * object
106: * @param height the number of rows of pixels in this image component
107: * object
108: * @param byReference a flag that indicates whether the data is copied
109: * into this image component object or is accessed by reference.
110: * @param yUp a flag that indicates the y-orientation of this image
111: * component. If yUp is set to true, the origin of the image is
112: * the lower left; otherwise, the origin of the image is the upper
113: * left.
114: * @param baseURL The base URL for the image location
115: * @param imageName The path and name of the image
116: * @exception IllegalArgumentException if format is invalid, or if
117: * width or height are not positive.
118: *
119: */
120: public ImageComponent2DURL(int format, int width, int height,
121: boolean byReference, boolean yUp, java.net.URL baseURL,
122: String imageName) {
123: this (format, width, height, byReference, yUp, baseURL,
124: imageName, false);
125: }
126:
127: /**
128: * Constructs a 2D image component object using the specified
129: * format, width, height, byReference flag, and yUp flag.
130: * Default values are used for all other parameters.
131: * If dontUseAssetManager is true then the constructor will not use
132: * the asset manager to load the image.
133: *
134: * @param format the image component format, one of: FORMAT_RGB,
135: * FORMAT_RGBA, etc.
136: * @param width the number of columns of pixels in this image component
137: * object
138: * @param height the number of rows of pixels in this image component
139: * object
140: * @param byReference a flag that indicates whether the data is copied
141: * into this image component object or is accessed by reference.
142: * @param yUp a flag that indicates the y-orientation of this image
143: * component. If yUp is set to true, the origin of the image is
144: * the lower left; otherwise, the origin of the image is the upper
145: * left.
146: * @param baseURL The base URL for the image location
147: * @param imageName The path and name of the image
148: * @exception IllegalArgumentException if format is invalid, or if
149: * width or height are not positive.
150: *
151: */
152: public ImageComponent2DURL(int format, int width, int height,
153: boolean byReference, boolean yUp, java.net.URL baseURL,
154: String imageName, boolean dontUseAssetManager) {
155: super (format, width, height, byReference, yUp);
156: this .baseURL = baseURL;
157: this .imageName = imageName;
158: if (!dontUseAssetManager)
159: notifyAssetManager();
160: }
161:
162: /**
163: * Call the asset manager to load the image
164: */
165: private void notifyAssetManager() {
166: assetManager.loadImage(this );
167: }
168:
169: /**
170: * Set the asset manager to handle all ImageComponent2DURL objects.
171: */
172: public static void setAssetManager(AssetManager manager) {
173: if (manager == null)
174: throw new IllegalArgumentException(
175: "AssetManager can not be null");
176: assetManager = manager;
177: }
178:
179: /**
180: * Get the current asset manager.
181: */
182: public static AssetManager getAssetManager() {
183: return assetManager;
184: }
185:
186: /**
187: * Set the Base URL for this image component
188: *
189: * @param url The URL for the image component
190: */
191: public void setBaseURL(java.net.URL url) {
192: this .baseURL = url;
193: }
194:
195: /**
196: * Get the Base URL for this image component
197: *
198: * @return TheURL for this image component
199: */
200: public java.net.URL getBaseURL() {
201: return baseURL;
202: }
203:
204: /**
205: * Set the name of the image
206: *
207: * The baseURL prepended to the name will give the full
208: * URL of the image
209: */
210: public void setImageName(String imageName) {
211: this .imageName = imageName;
212: }
213:
214: /**
215: * Get the name of the image
216: *
217: * The baseURL prepended to the name will give the full
218: * URL of the image
219: */
220: public String getImageName() {
221: return new String(imageName);
222: }
223:
224: /**
225: * Return the State Class used by SceneGraphIO
226: */
227: public Class<? extends SceneGraphObjectState> getStateClass() {
228: return ImageComponent2DURLState.class;
229: }
230:
231: /**
232: * AssetManager interface. The AssetManager is responsible for loading
233: * each image.
234: */
235: public interface AssetManager {
236: public void loadImage(ImageComponent2DURL image);
237:
238: /**
239: * Create an image component of the correct size/type for the specified
240: * image
241: */
242: public ImageComponent2DURL createImageComponent2DURL(
243: java.net.URL baseURL, String filename);
244: }
245:
246: /**
247: * A simple implementation of AssetManager that loads the image
248: * from the combined baseURL and imageName.
249: */
250: static class SimpleAssetManager implements AssetManager {
251: public void loadImage(ImageComponent2DURL ic) {
252: String url = ic.getBaseURL().toExternalForm();
253:
254: try {
255: URL fullURL = new URL(url + "/" + ic.getImageName());
256: BufferedImage bufImg = ImageIO.read(fullURL);
257: ic.set(bufImg);
258: } catch (IOException ex) {
259: ex.printStackTrace();
260: }
261:
262: }
263:
264: public ImageComponent2DURL createImageComponent2DURL(
265: URL baseURL, String filename) {
266: throw new RuntimeException("Not Implemented");
267: }
268: }
269:
270: }
|