Applet Class Hierarchy Applet Methods called by the
Applet Class Hierarchy
Applet Methods called by the system • public void init() When the applet is first loaded. Do initialization here. • public void start() When the applet is displayed. • public void stop() When the browser leaves the page containing the applet. • public void destroy() • public void paint(Graphics g)
Applet Methods Applets may require additional data, image or sound files that are stored on the same host as the applet. The following methods locate the host. • public URL get. Code. Base() Returns URL of the directory that contains the applet’s. class file • public URL get. Document. Base() Returns URL of the current HTML document
URLs The URL form is: protocol: //host_name/path_name/file_name protocol is the format for transferring files (http, ftp, gopher) host_name is a hierarchical listing of java domains listed in increasing order of generality (java. sun. com, cs. some. edu: 8080 – port number) path_name is a hierarchical listing of the file structure listed in decreasing order of generality (products/jdk/1. 1/docs/api)
Absolute & Relative URLs • Absolute URL http: //java. sun. com/products/jdk/1. 1/docs/api/tree. html • Relative URL (operates within context of above URL) packages. html (replaces tree. html) foo/hello. html (replaces tree. html) /bar/notes. html (replaces /api/tree. html)
URL Class package java. net • URL Constructors URL(String name) URL(URL base, String relative) URL(String protocol, String host, String file) URL(String protocol, String hostg, int port, String file) • URL Methods String get. File() String get. Host() int get. Port() String get. Protocol()
Loading Audios / Images • • public Audio. Clip get. Audio. Clip(URL absolute) Audio. Clip get. Audio. Clip(URL base, String rel) Image get. Image(URL absolute) Image get. Image(URL base, String rel) If the image named test. gif is contained in the relative URL /images then the following code will load the image URL test. URL = new URL(get. Code. Base(), “/images/test. gif”); Image test. Image = get. Image(test. URL);
Event • an object that represents a user action at the computer’s interface – user actions include: keyboard, mouse, etc. – Can also can be generated by other programs. (timer) – Different types of events are represented by different classes: Mouse. Event, Action. Event, etc.
Listeners • an object that waits for and responses to particular types of events. – There are different types of listeners represented by different listener interfaces: Mouse. Listener, Action. Listener, etc. – A listener class implements one of the listener interfaces.
Events and Listeners Event Source Listener This object may generate an event This object waits for and responds to an event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event
Delegation Model Has two players – • • The component that generates events An object that deals with the event. Important features of this model • • • Any component can be the source of an event Any class can be a listener for an event if it implements the appropriate listener interface The event is only sent to listeners that are registered with the source of the event
Interface Mouse. Listener • • • void void mouse. Pressed(Mouse. Event event) mouse. Released(Mouse. Event event) mouse. Clicked(Mouse. Event event) mouse. Entered(Mouse. Event event) mouse. Exited(Mouse. Event event)
Class Mouse. Event • Point get. Point() • int get. X() • int get. Y() • int get. Click. Cont()
Example: Dots. java import java. applet. Applet; import java. awt. *; public class Dots extends Applet { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 100; private final int RADIUS = 6; private Point click. Point = null; public void init() { Dots. Mouse. Listener listener = new Dots. Mouse. Listener(this); add. Mouse. Listener(listener); set. Background(Color. black); set. Size(APPLET_WIDTH, APPLET_HEIGHT); }
Example: Dots. java public void paint(Graphics page) { page. set. Color (Color. green); if (click. Point != null) page. fill. Oval(click. Point. x - RADIUS, click. Point. y - RADIUS, RADIUS * 2); } public void set. Point(Point point) { click. Point = point; } }
Example: Dots. Mouse. Listener. java import java. applet. Applet; import java. awt. *; import java. awt. event. *; class Dots. Mouse. Listener implements Mouse. Listener { private Dots applet; public Dots. Mouse. Listener(Dots applet) { this. applet = applet; } public void mouse. Clicked(Mouse. Event event) { Point click. Point = event. get. Point(); applet. set. Point (click. Point); applet. repaint(); } public void mouse. Pressed(Mouse. Event event) {} public void mouse. Released(Mouse. Event event) {} public void mouse. Entered(Mouse. Event event) {} public void mouse. Exited(Mouse. Event event) {} }
Interface Mouse. Motion. Listener • void mouse. Moved(Mouse. Event event) • void mouse. Dragged(Mouse. Event event)
Example Rubber. Lines. java import java. applet. Applet; import java. awt. *; import java. awt. event. *; public class Rubber. Lines extends Applet implements Mouse. Listener, Mouse. Motion. Listener { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 200; private Point point 1 = null; private Point point 2 = null; public void init() { add. Mouse. Listener(this); add. Mouse. Motion. Listener(this); set. Background(Color. black); set. Size(APPLET_WIDTH, APPLET_HEIGHT); }
Example Rubber. Lines. java public public } void void mouse. Clicked(Mouse. Event event) {} mouse. Released(Mouse. Event event) {} mouse. Entered(Mouse. Event event) {} mouse. Exited(Mouse. Event event) {} mouse. Moved(Mouse. Event event) {}
Handling Key Presses • Interface Key. Listener – void key. Pressed(Key. Event event) – void key. Released(Key. Event event) – void key. Typed(Key. Event event) • Class Key. Event – int get. Key. Code() – constants for all keys
Example: Direction. java import java. applet. *; import java. awt. event. *; public class Direction extends Applet { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 200; private final int JUMP = 5; // increment for image movement private final int IMAGE_SIZE = 31; private Image up, down, right, left, current. Image; private Audio. Clip bonk; private int x, y;
Example: Direction. java public void init() { request. Focus(); // make sure the applet has the keyboard focus add. Key. Listener(new Direction. Key. Listener()); x = y = 0; up = get. Image (get. Code. Base(), "cyan. Up. gif"); down = get. Image (get. Code. Base(), "cyan. Down. gif"); left = get. Image (get. Code. Base(), "cyan. Left. gif"); right = get. Image (get. Code. Base(), "cyan. Right. gif"); current. Image = right; bonk = get. Audio. Clip (get. Code. Base(), "bonk. au"); set. Background (Color. black); set. Size (APPLET_WIDTH, APPLET_HEIGHT); } public void paint (Graphics page) { page. draw. Image (current. Image, x, y, this); }
Example: Direction. java private class Direction. Key. Listener implements Key. Listener { public void key. Pressed (Key. Event event) { switch (event. get. Key. Code()) { case Key. Event. VK_UP: current. Image = up; if (y > 0) y -= JUMP; break; case Key. Event. VK_DOWN: current. Image = down; if (y < APPLET_HEIGHT-IMAGE_SIZE) y += JUMP; break; case Key. Event. VK_LEFT: current. Image = left; if (x > 0) x -= JUMP; break;
Example: Direction. java case Key. Event. VK_RIGHT: current. Image = right; if (x < APPLET_WIDTH-IMAGE_SIZE) x += JUMP; break; default: bonk. play(); } repaint(); } public void key. Typed (Key. Event event) {} public void key. Released (Key. Event event) {} } }
Example Rubber. Lines. java public void paint(Graphics page) { page. set. Color (Color. green); if (point 1 != null && point 2 != null) page. draw. Line (point 1. x, point 1. y, point 2. x, point 2. y); } public void mouse. Pressed(Mouse. Event event) { point 1 = event. get. Point(); } public void mouse. Dragged(Mouse. Event event) { point 2 = event. get. Point(); repaint(); }
Animation • An animation is a constantly changing series of pictures or images that create the illusion of movement • We can create animations in Java by changing a picture slightly over time • The speed of a Java animation is usually controlled by a Timer object • Timer class – – – Timer(int delay, Action. Listener listener) void add. Action. Listener(Action. Listener listener) boolean is. Running() void start() void stop()
Example: Rebound. java import java. applet. Applet; import java. awt. *; import java. awt. event. *; import javax. swing. Timer; public class Rebound extends Applet { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 100; private final int IMAGE_SIZE = 35; private final int DELAY = 20; private Timer timer; private Image image; private int x, y, move. X, move. Y;
Example: Rebound. java public void init() { add. Mouse. Listener(new Rebound. Mouse. Listener()); timer = new Timer(DELAY, new Rebound. Action. Listener()); timer. start(); x = 0; y = 40; move. X = move. Y = 3; image = get. Image(get. Code. Base(), "happy. Face. gif"); set. Background(Color. black); set. Size(APPLET_WIDTH, APPLET_HEIGHT); } public void paint(Graphics page) { page. draw. Image(image, x, y, this); }
Example: Rebound. java private class Rebound. Mouse. Listener implements Mouse. Listener { public void mouse. Clicked(Mouse. Event event) { if (timer. is. Running()) timer. stop(); else timer. start(); } public void mouse. Entered(Mouse. Event event) {} public void mouse. Exited(Mouse. Event event) {} public void mouse. Pressed(Mouse. Event event) {} public void mouse. Released(Mouse. Event event) {} }
Example: Rebound. java private class Rebound. Action. Listener implements Action. Listener { public void action. Performed(Action. Event event) { x += move. X; y += move. Y; if (x <= 0 || x >= APPLET_WIDTH-IMAGE_SIZE) move. X = move. X * -1; if (y <= 0 || y >= APPLET_HEIGHT-IMAGE_SIZE) move. Y = move. Y * -1; repaint(); } } }
- Slides: 31