Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Implementing Comparable public class Rational implements Comparable { public int compareTo (Object obj) { final double TOLERANCE =.0001; Rational other.
Event-Driven Programming
Event-Driven Programming
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 14: Applets and More Starting Out with Java: From Control Structures.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Applets Session 8. Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Timer Objects  regularly generates action events at programmer-specified time intervals  Timer class is in javax.swing package  Timer (int delay, ActionListener.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Chapter 12: Accessing the Web URL (Uniform Resource Locator) class Applet methods –for audio clips –for images –context interface.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
CHAPTER 14 Applets and More Copyright © 2016 Pearson Education, Ltd.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Events and Event Handling
Chapter 14 Event-Driven Programming
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Chapter 5: Enhancing Classes
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
Presentation transcript:

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 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 Relative URL (operates within context of above URL) packages.html (replaces tree.html)tree.html foo/hello.html (replaces tree.html)tree.html /bar/notes.html (replaces /api/tree.html)/api/tree.html

URL Classpackage 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.

Events and Listeners Source This object may generate an event Listener This object waits for and responds to an event 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 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) Class KeyEvent –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 = APPLET_WIDTH-IMAGE_SIZE) moveX = moveX * -1; if (y = APPLET_HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); }