Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Using Java
Java Applet a Michael Fung, CS&E, The Chinese University of HK
2
Michael Fung, CS&E, The Chinese University of HK
Overview Executing our Java program everywhere Java Applet (Application-let [small/mini]) Applet in Action Transformation Mix and Match with HTML a Michael Fung, CS&E, The Chinese University of HK
3
Michael Fung, CS&E, The Chinese University of HK
What’s an Applet? A Java program. Operates within a browser e.g. I.E., Netscape, … Appears in a web page. Runs on the viewer’s computer. Loaded from the web. a Michael Fung, CS&E, The Chinese University of HK
4
JVM Everywhere Java Program [Welcome.java] Compile
Java Compiler [javac] Compile Java Byte Code [Welcome.class] native code Translate Java Virtual Machine (JVM) [java] and BROWSERS a Michael Fung, CS&E, The Chinese University of HK
5
Applet Around the World
Web site [ Applet (Java Byte Code) Lake.class Browser I.E. at my home PC [M-II-2GHz] Browser Netscape at Bill’s office PC [Fiii-5GHz] Internet Embedded JVM Embedded JVM a Michael Fung, CS&E, The Chinese University of HK
6
Olympic Rings Example Again
import java.awt.*; // import Java packages class Rings extends Frame { public Rings ( ) { // constructor setTitle ("Olympic Rings"); // the window title bar } public void paint (Graphics g) { // a method for painting g.setColor(Color.red); g.drawOval( 80,80,30,30); // the rings g.setColor(Color.blue); g.drawOval(105,80,30,30); g.setColor(Color.green); g.drawOval(130,80,30,30); g.setColor(Color.yellow); g.drawOval(155,80,30,30); g.setColor(Color.black); g.drawOval(180,80,30,30); g.drawString("Olympic Rings", 110, 140); // the text public static void main (String [ ] args) { // main method Rings f = new Rings (); f.setSize (300, 200); // window dimensions f.setVisible (true); // show the window a Michael Fung, CS&E, The Chinese University of HK
7
Olympic Rings Applet Example
Embedded in browser. There’s no window frame and title bar. No Close mechanism. Fit seamlessly in the web page. Started automatically! a Michael Fung, CS&E, The Chinese University of HK
8
Olympic Rings Example Again
import java.awt.*; // import AWT package import java.applet.*; // import Applet package // inherits from Applet, a kind of container, c.f. Frame public class Rings extends Applet { // public class // constructor Rings() and main() replaced by init() public void paint (Graphics g) { // method defined in class Applet g.setColor(Color.red); g.drawOval( 80,80,30,30); // the rings g.setColor(Color.blue); g.drawOval(105,80,30,30); g.setColor(Color.green); g.drawOval(130,80,30,30); g.setColor(Color.yellow); g.drawOval(155,80,30,30); g.setColor(Color.black); g.drawOval(180,80,30,30); g.drawString("Olympic Rings", 110, 140); // the text } public void init() { // init(): instance method // setTitle() removed setSize(300, 200); setVisible(true); a Michael Fung, CS&E, The Chinese University of HK
9
Michael Fung, CS&E, The Chinese University of HK
The Beds for Applets Applets are compiled using javac as usual. Applets cannot be run using java. Why? Applets should be run with a HTML file using the tool appletviewer in the JDK using any web browser supporting Java Applet (i.e. with JVM embedded). a Michael Fung, CS&E, The Chinese University of HK
10
Michael Fung, CS&E, The Chinese University of HK
7-Minute Break a Michael Fung, CS&E, The Chinese University of HK
11
Actions in Applet Internet Web site [http://mike.earth/Index.html]
<HTML>… <APPLET CODE=Rings.class …>... </HTML> Browser I.E. at my home PC [M-II-2GHz] Internet User visits a web page. Inside the HTML file, there is a tag linking to an applet. Embedded JVM a Michael Fung, CS&E, The Chinese University of HK
12
Actions in Applet Internet Web site [http://mike.earth/Rings.class]
(Java Byte Code) Rings.class Browser I.E. at my home PC [M-II-2GHz] Internet Assuming the user’s browser supports Java Applet, the JBC is downloaded. Embedded JVM a Michael Fung, CS&E, The Chinese University of HK
13
Actions in Applet On (re)loading the web page
Browser I.E. at my home PC [M-II-2GHz] On (re)loading the web page the browser reserves a space; the JVM creates a new object from the class Rings; the JVM calls the instance method init() of the new object (instead of static main(), it’d be ignored if any) to initialize the applet. Embedded JVM Applet (Java Byte Code) Rings.class a Michael Fung, CS&E, The Chinese University of HK
14
Actions in Applet On scrolling the web page
Browser I.E. at my home PC [M-II-2GHz] On scrolling the web page the applet may become invisible; the applet may appear again; the JVM calls the instance methods stop() and start() respectively in hope of halting/restarting the activities of the applet. Embedded JVM Applet (Java Byte Code) Rings.class a Michael Fung, CS&E, The Chinese University of HK
15
Actions in Applet On surfing to another page
Browser I.E. at my home PC [M-II-2GHz] On surfing to another page the JVM calls the instance method destroy() of the object in order to perform some finalizations (clear house actions); kills the applet object; the browser reclaims the space; the browser moves on to another page. Embedded JVM Applet (Java Byte Code) Rings.class a Michael Fung, CS&E, The Chinese University of HK
16
7-Steps in Transformation
There are some precautions and advice in transforming a Java Application into an Applet: Using the GUI for user interactions e.g. System.out.println(“Hello”); g.drawString(“Hello”, 0, 0); a Michael Fung, CS&E, The Chinese University of HK
17
7-Steps in Transformation
Avoid program termination Remove all occurrences of, say, System.exit(); Catch all Exceptions to avoid abnormal termination. Let the browser do the job and make use of our destroy() to do what we need before termination. a Michael Fung, CS&E, The Chinese University of HK
18
7-Steps in Transformation
Beware of layout within the applet An applet object acts part of the role of a Frame object. An applet is a Container. The default layout is FlowLayout. We may override it by calling setLayout(). We may add children to an applet. a Michael Fung, CS&E, The Chinese University of HK
19
7-Steps in Transformation
Import the package java.applet.* and extends Applet instead of Frame in the main window. As a matter of taste, you may want the applet to appear thoroughly within the browser. Alternatively, you may call up another Frame object in order to pop up another window. In either case, you have to extend Applet in the main class/ window. a Michael Fung, CS&E, The Chinese University of HK
20
7-Steps in Transformation
Replace (combine) the class’s constructor method and the static main() method by the instance method public void init() It initializes the applet object on kicking-off. In fact, we may still keep the main() method so that our program becomes dual executable. a Michael Fung, CS&E, The Chinese University of HK
21
7-Steps in Transformation
Make sure our class is public Let it be accessible to other packages Simply add the key word public before the class declaration. e.g. public class Rings { ... } a Michael Fung, CS&E, The Chinese University of HK
22
7-Steps in Transformation
Create a HTML file to refer to the applet e.g. Rings.html: <HTML> <APPLET CODE=Rings.class> </APPLET> </HTML> Keep the applet JBC with the HTML file. Feed the HTML file to any Applet-enabled viewers. a Michael Fung, CS&E, The Chinese University of HK
23
Michael Fung, CS&E, The Chinese University of HK
7-Minute Break a Michael Fung, CS&E, The Chinese University of HK
24
Michael Fung, CS&E, The Chinese University of HK
Write it from Scratch There’s not much difference as compared with programming in Java GUI. Let’s see some examples: 7-Steps Transformation LazyConversion Java Image Viewer a Michael Fung, CS&E, The Chinese University of HK
25
Michael Fung, CS&E, The Chinese University of HK
Wet our Feet with HTML An applet is a small application embedded in a web page. A HTML file is responsible for calling up and accommodating the applet. We may place both the HTML and the JBC files in the same directory in our local machines or in the Internet. For the latter case, mind the URL’s and the file access permissions. a Michael Fung, CS&E, The Chinese University of HK
26
Michael Fung, CS&E, The Chinese University of HK
Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> Tag <APPLET> indicates that an applet is to be inserted here. a Michael Fung, CS&E, The Chinese University of HK
27
Michael Fung, CS&E, The Chinese University of HK
Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> Tag <APPLET> accepts several parameters, for example, code, width, height, ... a Michael Fung, CS&E, The Chinese University of HK
28
Michael Fung, CS&E, The Chinese University of HK
Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> code tells where the applet is located and its class name. a Michael Fung, CS&E, The Chinese University of HK
29
Michael Fung, CS&E, The Chinese University of HK
Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=50%> </APPLET> </HTML> width and height specifies how much space is to be reserved by the browser to accommodate the applet. a Michael Fung, CS&E, The Chinese University of HK
30
Michael Fung, CS&E, The Chinese University of HK
Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> Pity that this width and height do not correspond very well to the size of an applet. a Michael Fung, CS&E, The Chinese University of HK
31
Michael Fung, CS&E, The Chinese University of HK
Do Them Together We may add the HTML source to the project and test our applet within the integrated environment. If we got several class files (JBC), sound files, image files, etc., we’d better JAR them into a single archive. a Michael Fung, CS&E, The Chinese University of HK
32
Michael Fung, CS&E, The Chinese University of HK
What’s Next? Parameter Passing from HTML Limitations Security Issues Compare with Javascript Image and Sound with Applet a Michael Fung, CS&E, The Chinese University of HK
33
Michael Fung, CS&E, The Chinese University of HK
End Note Readings and References Appendix G a Michael Fung, CS&E, The Chinese University of HK
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.