320 likes | 693 Views
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()
E N D
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 getCodeBase() Returns URL of the directory that contains the applet’s .class file • public URL getDocumentBase() 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 getFile() String getHost() int getPort() String getProtocol()
Loading Audios / Images • public AudioClip getAudioClip(URL absolute) • public AudioClip getAudioClip(URL base,String rel) • public Image getImage(URL absolute) • public Image getImage(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 testURL = new URL(getCodeBase(), “/images/test.gif”); Image testImage = getImage(testURL);
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:MouseEvent, ActionEvent, 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:MouseListener, ActionListener, etc. • A listener class implements one of the listener interfaces.
Event Source Listener This object may generate an event This object waits for and responds to an event Events and Listeners 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 MouseListener • void mousePressed(MouseEvent event) • void mouseReleased(MouseEvent event) • void mouseClicked(MouseEvent event) • void mouseEntered(MouseEvent event) • void mouseExited(MouseEvent event)
Class MouseEvent • Point getPoint() • int getX() • int getY() • int getClickCont()
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 clickPoint = null; public void init() { DotsMouseListener listener = new DotsMouseListener(this); addMouseListener(listener); setBackground(Color.black); setSize(APPLET_WIDTH, APPLET_HEIGHT); }
Example: Dots.java public void paint(Graphics page) { page.setColor (Color.green); if (clickPoint != null) page.fillOval(clickPoint.x - RADIUS, clickPoint.y - RADIUS, RADIUS * 2, RADIUS * 2); } public void setPoint(Point point) { clickPoint = point; } }
Example: DotsMouseListener.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; class DotsMouseListener implements MouseListener { private Dots applet; public DotsMouseListener(Dots applet) { this.applet = applet; } public void mouseClicked(MouseEvent event) { Point clickPoint = event.getPoint(); applet.setPoint (clickPoint); applet.repaint(); } public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} }
Interface MouseMotionListener • void mouseMoved(MouseEvent event) • void mouseDragged(MouseEvent event)
Example RubberLines.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class RubberLines extends Applet implements MouseListener, MouseMotionListener { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 200; private Point point1 = null; private Point point2 = null; public void init() { addMouseListener(this); addMouseMotionListener(this); setBackground(Color.black); setSize(APPLET_WIDTH, APPLET_HEIGHT); }
Example RubberLines.java public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseMoved(MouseEvent event) {} }
Handling Key Presses • Interface KeyListener • void keyPressed(KeyEvent event) • void keyReleased(KeyEvent event) • void keyTyped(KeyEvent event) • ClassKeyEvent • int getKeyCode() • constants for all keys
Example: Direction.java import java.applet.*; import java.awt.*; 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, currentImage; private AudioClip bonk; private int x, y;
Example: Direction.java public void init() { requestFocus(); // make sure the applet has the keyboard focus addKeyListener(new DirectionKeyListener()); x = y = 0; up = getImage (getCodeBase(), "cyanUp.gif"); down = getImage (getCodeBase(), "cyanDown.gif"); left = getImage (getCodeBase(), "cyanLeft.gif"); right = getImage (getCodeBase(), "cyanRight.gif"); currentImage = right; bonk = getAudioClip (getCodeBase(), "bonk.au"); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); } public void paint (Graphics page) { page.drawImage (currentImage, x, y, this); }
Example: Direction.java private class DirectionKeyListener implements KeyListener { public void keyPressed (KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: currentImage = up; if (y > 0) y -= JUMP; break; case KeyEvent.VK_DOWN: currentImage = down; if (y < APPLET_HEIGHT-IMAGE_SIZE) y += JUMP; break; case KeyEvent.VK_LEFT: currentImage = left; if (x > 0) x -= JUMP; break;
Example: Direction.java case KeyEvent.VK_RIGHT: currentImage = right; if (x < APPLET_WIDTH-IMAGE_SIZE) x += JUMP; break; default: bonk.play(); } repaint(); } public void keyTyped (KeyEvent event) {} public void keyReleased (KeyEvent event) {} } }
Example RubberLines.java public void paint(Graphics page) { page.setColor (Color.green); if (point1 != null && point2 != null) page.drawLine (point1.x, point1.y, point2.x, point2.y); } public void mousePressed(MouseEvent event) { point1 = event.getPoint(); } public void mouseDragged(MouseEvent event) { point2 = event.getPoint(); 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, ActionListener listener) • void addActionListener(ActionListener listener) • boolean isRunning() • 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, moveX, moveY;
Example: Rebound.java public void init() { addMouseListener(new ReboundMouseListener()); timer = new Timer(DELAY, new ReboundActionListener()); timer.start(); x = 0; y = 40; moveX = moveY = 3; image = getImage(getCodeBase(), "happyFace.gif"); setBackground(Color.black); setSize(APPLET_WIDTH, APPLET_HEIGHT); } public void paint(Graphics page) { page.drawImage(image, x, y, this); }
Example: Rebound.java private class ReboundMouseListener implements MouseListener { public void mouseClicked(MouseEvent event) { if (timer.isRunning()) timer.stop(); else timer.start(); } public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} }
Example: Rebound.java private class ReboundActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { x += moveX; y += moveY; if (x <= 0 || x >= APPLET_WIDTH-IMAGE_SIZE) moveX = moveX * -1; if (y <= 0 || y >= APPLET_HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); } } }