2 * Copyright (c) 2009-2010 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 import com.jme3.system.AppSettings;
36 import com.jme3.system.JmeCanvasContext;
37 import com.jme3.system.JmeSystem;
38 import java.applet.Applet;
39 import java.awt.Canvas;
40 import java.awt.Graphics;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.net.MalformedURLException;
45 import java.util.HashMap;
46 import java.util.logging.Level;
47 import java.util.logging.Logger;
48 import javax.swing.JOptionPane;
49 import javax.swing.SwingUtilities;
52 * @author Kirill Vainer
54 public class AppletHarness extends Applet {
56 public static final HashMap<Application, Applet> appToApplet
57 = new HashMap<Application, Applet>();
59 private JmeCanvasContext context;
60 private Canvas canvas;
61 private Application app;
63 private String appClass;
64 private URL appCfg = null;
65 private URL assetCfg = null;
67 public static Applet getApplet(Application app){
68 return appToApplet.get(app);
71 private void createCanvas(){
72 AppSettings settings = new AppSettings(true);
76 InputStream in = null;
78 in = appCfg.openStream();
81 } catch (IOException ex){
82 // Called before application has been created ....
83 // Display error message through AWT
84 JOptionPane.showMessageDialog(this, "An error has occured while "
85 + "loading applet configuration"
88 JOptionPane.ERROR_MESSAGE);
94 } catch (IOException ex) {
99 if (assetCfg != null){
100 settings.putString("AssetConfigURL", assetCfg.toString());
103 settings.setWidth(getWidth());
104 settings.setHeight(getHeight());
106 JmeSystem.setLowPermissions(true);
109 Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
110 app = clazz.newInstance();
111 }catch (ClassNotFoundException ex){
112 ex.printStackTrace();
113 }catch (InstantiationException ex){
114 ex.printStackTrace();
115 }catch (IllegalAccessException ex){
116 ex.printStackTrace();
119 appToApplet.put(app, this);
120 app.setSettings(settings);
123 context = (JmeCanvasContext) app.getContext();
124 canvas = context.getCanvas();
125 canvas.setSize(getWidth(), getHeight());
132 public final void update(Graphics g) {
133 canvas.setSize(getWidth(), getHeight());
138 appClass = getParameter("AppClass");
139 if (appClass == null)
140 throw new RuntimeException("The required parameter AppClass isn't specified!");
143 appCfg = new URL(getParameter("AppSettingsURL"));
144 } catch (MalformedURLException ex) {
145 System.out.println(ex.getMessage());
150 assetCfg = new URL(getParameter("AssetConfigURL"));
151 } catch (MalformedURLException ex){
152 System.out.println(ex.getMessage());
153 assetCfg = getClass().getResource("/com/jme3/asset/Desktop.cfg");
157 System.out.println("applet:init");
162 context.setAutoFlushFrames(true);
163 System.out.println("applet:start");
168 context.setAutoFlushFrames(false);
169 System.out.println("applet:stop");
173 public void destroy(){
174 System.out.println("applet:destroyStart");
175 SwingUtilities.invokeLater(new Runnable(){
178 System.out.println("applet:destroyRemoved");
182 System.out.println("applet:destroyDone");
184 appToApplet.remove(app);