001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wpvs/j3d/BackgroundFactory.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.ogcwebservices.wpvs.j3d;
038    
039    import java.awt.Color;
040    import java.awt.Dimension;
041    import java.awt.Graphics;
042    import java.awt.image.BufferedImage;
043    import java.io.IOException;
044    import java.net.URL;
045    
046    import javax.imageio.ImageIO;
047    import javax.media.j3d.Background;
048    import javax.media.j3d.BoundingSphere;
049    import javax.media.j3d.ImageComponent2D;
050    import javax.vecmath.Color3f;
051    import javax.vecmath.Point3d;
052    
053    import com.sun.j3d.utils.image.TextureLoader;
054    
055    /**
056     * Factory for creating different types of Java3D objects to be used as background objects.
057     *
058     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
059     * @author last edited by: $Author: mschneider $
060     *
061     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
062     *
063     * @since 2.0
064     */
065    public class BackgroundFactory {
066    
067            private BackgroundFactory() {
068                    // nothing to do
069            }
070    
071            /**
072             * Creates Java 3D <code>background</code> object, drawn at infinity. This method is used
073             * for "box" views and/or when the users wants a fixed background image. Not the in the latter
074             * case for optimal results, the image should be of same dimensions as the <code>GetView</code>
075             * image
076             * @param vp the ViewPoint defining the observer's position
077             * @param color the background color (seen when image is null or doesn't cover the whole view)
078             * @param imgURL a URL pointing to an image
079             * @param reqImgDimension
080             * @return A Java3D Background Node
081             * @throws IOException
082             */
083            public static Background createSimpleBackgroundGroup( ViewPoint vp, Color color, URL imgURL, Dimension reqImgDimension ) throws IOException{
084    
085                    Point3d ftPrint = vp.getFootprint()[0];
086    
087                    Background bg = new Background( new Color3f( color ) );
088            Point3d origin = new Point3d( ftPrint.x, ftPrint.y, ftPrint.z );
089            BoundingSphere bounds = new BoundingSphere(origin, ftPrint.x );
090    
091            bg.setApplicationBounds( bounds );
092    
093            if( imgURL != null ) {
094                BufferedImage buffImg = ImageIO.read( imgURL );
095    
096                // scale image to fill the whole bakground
097                BufferedImage tmpImg = new BufferedImage( reqImgDimension.width, reqImgDimension.height, buffImg.getType() );
098                Graphics g = tmpImg.getGraphics();
099                g.drawImage( buffImg, 0, 0, tmpImg.getWidth() - 1 ,tmpImg.getHeight() - 1, null );
100                g.dispose();
101    
102                ImageComponent2D img = new TextureLoader( tmpImg ).getImage();
103                bg.setImage( img );
104            }
105    
106            return bg;
107            }
108    
109    
110    
111    }
112