• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32 
33 package jme3test.awt;
34 
35 import com.jme3.app.Application;
36 import com.jme3.system.AppSettings;
37 import com.jme3.system.JmeCanvasContext;
38 import com.jme3.system.JmeSystem;
39 import java.applet.Applet;
40 import java.awt.Canvas;
41 import java.awt.Graphics;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import javax.swing.SwingUtilities;
47 
48 /**
49  *
50  * @author Kirill
51  */
52 public class AppHarness extends Applet {
53 
54     private JmeCanvasContext context;
55     private Canvas canvas;
56     private Application app;
57 
58     private String appClass;
59     private URL appCfg = null;
60 
createCanvas()61     private void createCanvas(){
62         AppSettings settings = new AppSettings(true);
63 
64         // load app cfg
65         if (appCfg != null){
66             try {
67                 InputStream in = appCfg.openStream();
68                 settings.load(in);
69                 in.close();
70             } catch (IOException ex){
71                 ex.printStackTrace();
72             }
73         }
74 
75         settings.setWidth(getWidth());
76         settings.setHeight(getHeight());
77         settings.setAudioRenderer(null);
78 
79         JmeSystem.setLowPermissions(true);
80 
81         try{
82             Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
83             app = clazz.newInstance();
84         }catch (ClassNotFoundException ex){
85             ex.printStackTrace();
86         }catch (InstantiationException ex){
87             ex.printStackTrace();
88         }catch (IllegalAccessException ex){
89             ex.printStackTrace();
90         }
91 
92         app.setSettings(settings);
93         app.createCanvas();
94 
95         context = (JmeCanvasContext) app.getContext();
96         canvas = context.getCanvas();
97         canvas.setSize(getWidth(), getHeight());
98 
99         add(canvas);
100         app.startCanvas();
101     }
102 
103     @Override
update(Graphics g)104     public final void update(Graphics g) {
105         canvas.setSize(getWidth(), getHeight());
106     }
107 
108     @Override
init()109     public void init(){
110         appClass = getParameter("AppClass");
111         if (appClass == null)
112             throw new RuntimeException("The required parameter AppClass isn't specified!");
113 
114         try {
115             appCfg = new URL(getParameter("AppSettingsURL"));
116         } catch (MalformedURLException ex) {
117             ex.printStackTrace();
118             appCfg = null;
119         }
120 
121         createCanvas();
122         System.out.println("applet:init");
123     }
124 
125     @Override
start()126     public void start(){
127         context.setAutoFlushFrames(true);
128         System.out.println("applet:start");
129     }
130 
131     @Override
stop()132     public void stop(){
133         context.setAutoFlushFrames(false);
134         System.out.println("applet:stop");
135     }
136 
137     @Override
destroy()138     public void destroy(){
139         System.out.println("applet:destroyStart");
140         SwingUtilities.invokeLater(new Runnable(){
141             public void run(){
142                 removeAll();
143                 System.out.println("applet:destroyRemoved");
144             }
145         });
146         app.stop(true);
147         System.out.println("applet:destroyDone");
148     }
149 
150 }
151