001    /**
002       ##############################################################################
003       ##                                                                          ##
004       ## Appearance3DChooser                                                      ##
005       ##                                                                          ##
006       ## Copyright (C) 2009  Frederic Roudaut  <frederic.roudaut@free.fr>         ##
007       ##                                                                          ##
008       ##                                                                          ##
009       ## This program is free software: you can redistribute it and/or modify     ##
010       ## it under the terms of the GNU General Public License as published by     ##
011       ## the Free Software Foundation, either version 3 of the License, or        ##
012       ## (at your option) any later version.                                      ##
013       ##                                                                          ##
014       ## This program is distributed in the hope that it will be useful,          ##
015       ## but WITHOUT ANY WARRANTY; without even the implied warranty of           ##
016       ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            ##
017       ## GNU General Public License for more details.                             ##
018       ##                                                                          ##
019       ## You should have received a copy of the GNU General Public License        ##
020       ## along with this program.  If not, see <http://www.gnu.org/licenses/>.    ##
021       ##                                                                          ##
022       ##                                                                          ##
023       ##############################################################################
024     **/
025    
026    package com.appearance3Dchooser;
027    
028    import java.beans.PropertyChangeEvent;
029    import java.beans.PropertyChangeListener;
030    
031    import java.io.*;
032    import javax.swing.JApplet;
033    import javax.swing.JOptionPane;
034    
035    
036    /**
037     * Applet Example for Appearence3DChooser.
038     * 
039     */
040    public class Appearance3DChooserApplet extends JApplet 
041    {
042    
043            private static final long serialVersionUID = 1L;
044    
045            /*
046          public static void main(final String[] args) 
047          {
048          final Appearance3DChooserApplet demo = new Appearance3DChooserApplet();
049          demo.setVisible(true);
050          }
051             */
052    
053            /**
054             * Called when the Applet is first loaded.
055             * 
056             */
057            public void init ()
058            {
059    
060                    // if loadDLL is set to true, then it means that the dynamic library has to be loaded 
061                    // into the client JRE Path. In this case the associated jar has to be signed first. 
062                    // Used to Automaticaly copy the DLL in the java path. Nevertheless they have to be in 
063                    // in a root dll directory
064                    if (getParameter("loadDLL") != null && getParameter("loadDLL").compareTo("true") == 0)
065                    {
066                            // load Windows DLL for Java3D 
067                            if(System.getProperty("os.name").indexOf("Win") != -1)
068                            {
069                                    copyResourceFromJarToBinDir("j3dcore-d3d.dll");
070                                    copyResourceFromJarToBinDir("j3dcore-ogl.dll");
071                                    copyResourceFromJarToBinDir("j3dcore-ogl-cg.dll");
072                                    copyResourceFromJarToBinDir("j3dcore-ogl-chk.dll");
073                            }               
074                            else // Consider it is Linux                
075                            {
076                                    copyResourceFromJarToBinDir("libj3dcore-ogl.so");
077                                    copyResourceFromJarToBinDir("libj3dcore-ogl-cg.so");                    
078                            }               
079                    }
080    
081                    // if askRenderer is set to true, then it means that a popup will we run prior
082                    // in order to set the Renderer
083                    if (getParameter("askRenderer") != null && getParameter("askRenderer").compareTo("true") == 0)
084                    {
085    
086                            if (System.getProperty("os.name").indexOf("Win") != -1) 
087                            {                       
088                                    String[] options = {"OpenGL","Direct3D"}; 
089                                    String rend = "DEFAULT";
090                                    if(System.getProperty("j3d.rend")!=null)
091                                            rend = System.getProperty("j3d.rend");
092                                    {
093                                            if(rend.compareTo("ogl") == 0)
094                                                    rend = options[0];
095                                            else if (rend.compareTo("d3d") == 0)
096                                                    rend = options[1];
097                                    }
098    
099                                    int i = JOptionPane.showOptionDialog(null,
100                                                    Info.ressources.getObject("3D_Motor_choice_1").toString() + " : " + rend + "\n" +
101                                                    Info.ressources.getObject("3D_Motor_choice_2").toString(),                              
102                                                    Info.ressources.getObject("3D_Motor_choice_3").toString(),
103                                                    JOptionPane.OK_CANCEL_OPTION,
104                                                    JOptionPane.QUESTION_MESSAGE,
105                                                    null,
106                                                    options,
107                                                    options[0]); // OpenGL by default
108                                    if (i == 0) { // OpenGL
109                                            System.setProperty("j3d.rend", "ogl");
110                                    } else 
111                                    { // Direct3D
112                                            System.setProperty("j3d.rend","d3d");                           
113                                    }
114                            }
115                    }
116    
117    
118                    final Appearance3DChooser m = new Appearance3DChooser("test", true, true, true, true);
119    
120                    m.toBack();
121                    m.setVisible(false);
122                    setContentPane(m.getRootPane());        
123    
124                    m.runListeners();
125                    m.addPropertyChangeListener(new PropertyChangeListener() {
126                            public void propertyChange(PropertyChangeEvent evt) 
127                            {
128                                    if (evt.getPropertyName().compareTo(Appearance3DChooser.SPECULAR) == 0)
129                                    {
130                                            System.out.println("SPECULAR : (" + m.getSpecularColor()[0]+ "," 
131                                                            + m.getSpecularColor()[1]+ "," 
132                                                            + m.getSpecularColor()[2]+")");
133                                    }
134                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.AMBIENT) == 0)
135                                    {
136                                            System.out.println("AMBIENT : (" + m.getAmbientColor()[0]+ "," 
137                                                            + m.getAmbientColor()[1] + "," 
138                                                            + m.getAmbientColor()[2]+")");              
139                                    }
140                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.EMISSIVE) == 0)
141                                    {
142                                            System.out.println("EMISSIVE : (" + m.getEmissiveColor()[0]+ "," 
143                                                            + m.getEmissiveColor()[1] + "," 
144                                                            + m.getEmissiveColor()[2]+")");
145                                    }
146                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.DIFFUSE) == 0)
147                                    {
148                                            System.out.println("DIFFUSE : (" + m.getDiffuseColor()[0] + "," 
149                                                            + m.getDiffuseColor()[1] + "," 
150                                                            + m.getDiffuseColor()[2]+")");
151                                    }
152                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.COLORTARGET) == 0)
153                                    {
154                                            System.out.println("COLORTARGET : (" + m.getColorTarget() +")");
155                                    }
156                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.SHININESS) == 0)
157                                    {
158                                            System.out.println("SHININESS : (" + m.getShininess() + ")");
159                                    }
160                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.LIGHTING) == 0)
161                                    {
162                                            System.out.println("LIGHTING : (" + m.getLighting() +")");
163                                    }
164                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TRANSPARENCY) == 0)
165                                    {
166                                            System.out.println("TRANSPARENCY : (" + m.getTransparency() +")");
167                                    }
168                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TRANSPARENCY_MODE) == 0)
169                                    {
170                                            System.out.println("TRANSPARENCY_MODE : (" + m.getTransparencyMode() +")");
171                                    }
172                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TRANSPARENCY_SRC_BLEND_FUNCTION) == 0)
173                                    {
174                                            System.out.println("TRANSPARENCY_SRC_BLEND_FUNCTION : (" + m.getTransparencySrcBlendFunction() +")");
175                                    }
176                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TRANSPARENCY_DST_BLEND_FUNCTION) == 0)
177                                    {
178                                            System.out.println("TRANSPARENCY_DST_BLEND_FUNCTION : (" + m.getTransparencyDstBlendFunction() +")");
179                                    }
180                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_USE) == 0)
181                                    {
182                                            System.out.println("TEXTURE_USE : (" + m.isTextureSet() +")");
183                                    }
184                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TEXTURE_FILE) == 0)
185                                    {
186                                            System.out.println("TEXTURE_FILE : (" + m.getTextureFile() +")");
187                                    }
188                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TEXTURE_TRANSFORM) == 0)
189                                    {
190                                            System.out.println("TEXTURE_TRANSFORM : \n" + m.getTextureTransform() );
191                                            System.out.println("Scale : x=" + m.getTextureTransformScaleX() + ", y=" +m.getTextureTransformScaleY());
192                                            System.out.println("Translation : x=" + m.getTextureTransformTranslatX() + ", y=" +m.getTextureTransformTranslatY());
193                                            System.out.println("Rotation : z=" + m.getTextureTransformRotZ());                  
194                                    }
195                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TEXTURE_PERSPECTIVE_CORRECTION_MODE) == 0)
196                                    {
197                                            System.out.println("TEXTURE_PERSPECTIVE_CORRECTION_MODE : (" + m.getTexturePerspectiveCorrectionMode() +")");
198                                    }
199                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TEXTURE_MODE) == 0)
200                                    {
201                                            System.out.println("TEXTURE_MODE : (" + m.getTextureMode() +")");
202                                    }
203                                    else if (evt.getPropertyName().compareTo(Appearance3DChooser.TEXTURE_BLEND) == 0)
204                                    {
205                                            System.out.println("TEXTURE_BLEND : (" + 
206                                                            m.getTextureBlendColor()[0] + "," +
207                                                            m.getTextureBlendColor()[1] + "," +
208                                                            m.getTextureBlendColor()[2] + "," +
209                                                            m.getTextureBlendColor()[3] 
210                                                                                     + ")");
211                                    }
212                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_RGB_MODE) == 0)
213                                    {
214                                            System.out.println("TEXTURE_COMBINE_RGB_MODE : (" + m.getTextureCombineRGBMode() +")");
215                                    }
216                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_RGB_SOURCE) == 0)
217                                    {
218                                            System.out.println("TEXTURE_COMBINE_RGB_SOURCE : (" + 
219                                                            m.getTextureCombineRGBSource()[0] + "," +
220                                                            m.getTextureCombineRGBSource()[1] + "," +
221                                                            m.getTextureCombineRGBSource()[2] 
222                                                                                           + ")");
223                                    }
224                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_RGB_FUNCTION) == 0)
225                                    {
226                                            System.out.println("TEXTURE_COMBINE_RGB_FUNCTION : (" + 
227                                                            m.getTextureCombineRGBFunction()[0] + "," +
228                                                            m.getTextureCombineRGBFunction()[1] + "," +
229                                                            m.getTextureCombineRGBFunction()[2] 
230                                                                                             + ")");
231                                    }
232                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_RGB_SCALE) == 0)
233                                    {
234                                            System.out.println("TEXTURE_COMBINE_RGB_SCALE : (" + m.getTextureCombineRGBScale() +")");
235                                    }
236                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_ALPHA_MODE) == 0)
237                                    {
238                                            System.out.println("TEXTURE_COMBINE_ALPHA_MODE : (" + m.getTextureCombineAlphaMode() +")");
239                                    }
240                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_ALPHA_SOURCE) == 0)
241                                    {
242                                            System.out.println("TEXTURE_COMBINE_ALPHA_SOURCE : (" + 
243                                                            m.getTextureCombineAlphaSource()[0] + "," +
244                                                            m.getTextureCombineAlphaSource()[1] + "," +
245                                                            m.getTextureCombineAlphaSource()[2]
246                                                                                             + ")");
247                                    }                   
248                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_ALPHA_FUNCTION) == 0)
249                                    {
250                                            System.out.println("TEXTURE_COMBINE_ALPHA_FUNCTION : (" + 
251                                                            m.getTextureCombineAlphaFunction()[0] + "," +
252                                                            m.getTextureCombineAlphaFunction()[1] + "," +
253                                                            m.getTextureCombineAlphaFunction()[2]
254                                                                                               + ")");
255                                    }
256                                    else if (evt.getPropertyName().compareTo(Appearance3DChooserPanel.TEXTURE_COMBINE_ALPHA_SCALE) == 0)
257                                    {
258                                            System.out.println("TEXTURE_COMBINE_ALPHA_SCALE : (" + m.getTextureCombineAlphaScale() +")");
259                                    }                   
260    
261                            }});    
262    
263    
264                    // Set Values and reinit the defaults one since it leads to a 
265                    // problem because of the mouse focus from JFrame       
266                    m.setEmissiveColor(0f,0f,0f,false);
267                    m.setSpecularColor(0f,0f,0f,false);
268                    m.setAmbientColor(0f,0f,0f,false);
269                    m.setDiffuseColor(0f,0f,0f,false);
270                    m.setTextureBlendColor(0f,0f,0f,0f,false);
271    
272                    m.setInitParameters();
273    
274    
275            }
276    
277    
278            /**
279             * Used to automaticaly download a DLL on the client Host.
280             * @param tDll the DLL to download. It Must be in a Root dll directory.
281             * 
282             */
283            protected void copyResourceFromJarToBinDir(String tDll)  // Name of the DLL
284            {
285                    try 
286                    {
287                            //InputStream dllInputStream = getClass().getResourceAsStream("/dll/"+tDll);
288                            InputStream dllInputStream = new FileInputStream("dll\\"+tDll);
289    
290                            String javaHome = System.getProperty("java.home");
291                            String fileSeparator = "" + System.getProperty("file.separator").charAt(0);
292                            File outFile = null;
293    
294                            BufferedInputStream  binStream  = new BufferedInputStream(dllInputStream);
295                            outFile = new File(javaHome+fileSeparator+"bin"+fileSeparator+tDll);
296    
297                            BufferedOutputStream boutStream = new BufferedOutputStream(new FileOutputStream(outFile));
298                            int b = -1;
299                            while((b = binStream.read()) != -1) 
300                            { 
301                                    boutStream.write(b);
302                            }
303                            binStream.close();
304                            binStream.close();
305                            boutStream.flush();
306                            boutStream.close();
307    
308                            System.out.println("Copy " + tDll + " into " + outFile.getPath()); 
309                    } 
310    
311                    catch(Exception e) 
312                    {
313                            System.err.println("Error : cannot download " + tDll); 
314                            e.printStackTrace();
315                    }
316            }
317    
318    }