Java Reference
In-Depth Information
Apart from this difference, there are also other minor differences between programming
for applets and applications. For example, the method init is used instead of the construc-
tor in applets. Further information on this issue can be found from the Java Applet API
documentation at http://java.sun.com/products/jdk/1.2/docs/api/java/applet/Applet.html
and http://mercury.tvu.ac.uk/oop/oop_11.html. Figures 16 and 17 present an example code
segment for an applet. Note the use of the method Load starting from line 29, and the fact
that java.net.* has to be imported for using the URL class. Lastly, the code segment should
be run as an index.html file in the context of a browser.
Figure 16. First code segment for Myearth_applet.java
1.
import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Frame;
2.
import java.awt.event.*; import java.awt.GraphicsConfiguration;
3.
import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*;
4.
import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.geometry.*;
5.
import javax.media.j3d.*; import java.net.*;
6.
7.
public class Myearth_applet extends Applet {
8.
private java.net.URL go = null;
9.
private Texture texture = null;
10.
private SimpleUniverse simpleU= null;
11.
12.
public void init() { // initialize applet, equivalent to constructor in application
13.
setLayout(new BorderLayout());
14.
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
15.
Canvas3D canvas3D = new Canvas3D(config);
16.
add("Center", canvas3D); canvas3D.setStereoEnable(false);
17.
BranchGroup scene = createSceneGraph();
18.
simpleU = new SimpleUniverse(canvas3D);
19.
simpleU.getViewingPlatform().setNominalViewingTransform();
20.
simpleU.addBranchGraph(scene); }
21.
22.
public void destroy() { simpleU.removeAllLocales(); }
23.
24.
Appearance createAppearance() {
25.
Appearance Appear = new Appearance();
26.
Appear.setTexture(Load());
27.
return Appear; }
28.
29.
public Texture Load() { // load texture
30.
try go= new java.net.URL(getCodeBase().toString()+"earth.jpg");
31.
catch (java.net.MalformedURLException ex) {
32.
System.out.println(ex.getMessage());
33.
System.exit(1); }
34.
texture = new TextureLoader(go, this).getTexture();
35.
return texture; }
36.

Search WWH ::

Custom Search