Java Applets Objective Learn how to develop Java
Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser CSTP WS 00 CS 423 (cotter) 1
Java Applet Lifecycle Methods • init ( ) – Called when applet is loaded onto user’s machine. Prep work or one-time-only work done at this time. • start ( ) – Called when applet becomes visible (page called up). Called every time applet becomes visible. • stop ( ) – Called when applet becomes hidden (page loses focus). • destroy ( ) – Guaranteed to be called when browser shuts down. 2
Applets vs. Applications • • Extend Applet instead of Frame Use of init( ) vs. constructor No use of main( ) Default layout managers – application: – applet: Border. Layout Flow. Layout • Security 3
Applet Presentation • Uses applet Tag in HTML <HTML> <TITLE> My. New. Applet </TITLE> <BODY> Here’s My first Applet <APPLET CODE = “My. New. Applet” WIDTH = 300 HEIGHT = 200> </APPLET> </BODY> </HTML> 4
Applet Inheritance (extension) • • • Object - “cosmic base class” Component - button, list. Box Container - dialog box Panel - “sub-window”, group of buttons, etc. Applet - small application, web enabled My. Applet - specific instance of an applet 5
My. Text. Applet import java. applet. *; import java. awt. event. *; public class My. Text. Applet extends Applet { public void init() { set. Background(Color. gray); Panel p = new Panel(); Button. Action a. Button. Action = new Button. Action(); p. set. Layout(new Flow. Layout()); tick. Button = new Button("Tick"); tick. Button. add. Action. Listener(a. Button. Action); p. add(tick. Button); 6
My. Text. Applet set. Button = new Button("Set time"); set. Button. add. Action. Listener(a. Button. Action); p. add(set. Button); hour. Field = new Text. Field("12", 3); p. add(hour. Field); minute. Field = new Text. Field("00", 3); p. add(minute. Field); time. Field = new Text. Field("", 12); p. add(time. Field); add(p); } CSTP WS 00 CS 423 (cotter) 7
My. Text. Applet Class Button. Action implements Action. Listener { public void action. Performed(Action. Event evt) { String buttoncommand = evt. get. Action. Command(); if (buttoncommand. equals("Tick")) { int minutes = Integer. parse. Int(minute. Field. get. Text()); minutes += 1; String min = String. value. Of(minutes); minute. Field. set. Text(min); } 8
My. Text. Applet else if (buttoncommand. equals("Set time")) { int hours = Integer. parse. Int(hour. Field. get. Text()); int minutes = Integer. parse. Int(minute. Field. get. Text()); String tim = hour. Field. get. Text() + ": " + minute. Field. get. Text(); time. Field. set. Text(tim); } } } //end of Button. Action private Text. Field hour. Field; private Text. Field minute. Field; private Text. Field time. Field; private Button tick. Button; private Button set. Button; } 9
HTML file for My. Text. Applet <HTML> <HEAD> <TITLE> My Text Applet Demo </TITLE> </HEAD> <BODY background="Image 1. jpg"> <H 1> Here's my Demo Applet </H 1> <HR SIZE = 3> <APPLET CODE = My. Text. Applet WIDTH = 300 HEIGHT = 100> </APPLET> </BODY> </HTML> 10
My. Text. Applet Output 11
Parameter Passing to Applets import java. applet. *; import java. awt. *; public class Font. Applet extends Applet { public void paint (Graphics g) { String font. Name = get. Parameter ("font"); int font. Size = Integer. parse. Int (get. Parameter("size")); Font f = new Font(font. Name, Font. BOLD, font. Size); g. set. Font (f); g. draw. String("Welcome to CS 423", 25, 50); } } 12
HTML file for Font. Applet <HTML> <TITLE> Font. Applet </TITLE> <BODY background=“Image 1. jpg”> Here’s a demo of parameter passing to an Applet <hr> <APPLET CODE = "Font. Applet" WIDTH = 300 HEIGHT = 200> <PARAM NAME = font VALUE = "Helvetica"> <PARAM NAME = size VALUE = "24"> </APPLET> </BODY> </HTML> 13
Font. Applet. htm CSTP WS 00 CS 423 (cotter) 14
Using Applet Parameters in VJ++
Java Applet Lifecycle Methods (again. . . ) • init ( ) – Called when applet is loaded onto user’s machine. Prep work or one-time-only work done at this time. • start ( ) – Called when applet becomes visible (page called up). Called every time applet becomes visible. • stop ( ) – Called when applet becomes hidden (page loses focus). • destroy ( ) – Guaranteed to be called when browser shuts down. 16
Lifecycle. Applet. java import java. applet. Applet; import java. awt. Graphics; public class Lifecycle. Applet extends Applet { public Lifecycle. Applet() { System. out. println("Constructor running. . . "); } public void init() { System. out. println("This is init. "); } public void start() { System. out. println("Applet started. "); } public void paint(Graphics the. Graphics) { the. Graphics. draw. String("Hello, World!", 0, 50); System. out. println("Applet just painted. "); } public void stop() { System. out. println("Applet stopped. "); } public void destroy() { System. out. println("Applet destroyed. "); } CSTP CS 423 (cotter) 17 } WS 00
Lifecycle. Applet. java CSTP WS 00 CS 423 (cotter) 18
Lifecycle. Applet. java CSTP WS 00 CS 423 (cotter) 19
Summary • Java Applet managed through applet lifecycle • Applet can interact with browser through HTML code • Uses Event programming model to interact with user (using buttons, textfields, etc. ) CSTP WS 00 CS 423 (cotter) 20
- Slides: 20