KEMBAR78
java Unit4 chapter1 applets | PPTX
Unit 4- chapter 01
Applets, Events,
Miscellaneous Topics
CONTENTS
 Applet Basics,
 Applet Organization and Essential Elements,
◦ The Applet Architecture,
◦ A Complete Applet Skeleton,
◦ Applet Initialization and Termination,
◦ Requesting Repainting
◦ The update() Method,
 Using the Status Window
 Passing parameters to Applets
 The Applet Class
 Event Handling The Delegation Event Model
 Events,
 Using the Delegation Event Model,
 More Java Keywords.
Applet basics
 Applets offer a secure way to dynamically
download and execute programs over web.
 Two types of applets
◦ Based on AWT
◦ Based on Swings
 Sample program
Commands required to run an applet
javac filename.java
appletviewer filename.html
Commands required to run an applet
appletviewer filename.java
 Begins with two import statements
 Extends from Applet class
 Named as SimpleApplet and should be
public in nature because it will be accessed
outside code.
 Inside SimpleApplet , paint() is declared.
 paint() is called each time the applet must
redisplay its output.
 It takes an object of graphics type as an
input
 It contains the graphics context, which
describes the graphics environment in
 Inside paint() there is call to drawString() ,
which is member of Graphics class.
 void drawString(String message, int x, int
y);
 Which draws the given text message in the
specified x,y location.
 Upper left location is 0,0
 Applet begins the execution when the name
of its class is passed to a browser or applet
enabled program.
Key points of applet
 All applets are subclass of Applet
 Applets do not need a main method
 Applets must run under appletviewer or
java compatible browser
 User i/o is not accomplished with java’s
stream i/o class, instead applet use the
interface provided by the AWT or by Swing
The applet architecture
 An applet is GUI based program
 Applets are event driven
 An applet resembles a set of interrupt
service routines.
 An applet waits until an event occurs.
 The run-time system notifies the applet
about an event by calling an event handler
that has been provided by the applet.
 Once this happens, the applet must take
appropriate action and then quickly return
control to the system
 Second, it is the user who initiates
interaction with an applet.
 In a console-based program, when the
program needs input, it will prompt the user
and then call some input method
A Complete Applet Skeleton
 Five methods are used in applet
◦ init( )
◦ start( )
◦ paint()
◦ stop( )
◦ destroy( )
// An Applet skeleton.
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletSkel" width=300 height=100>
</applet>
*/
public class AppletSkel extends Applet {
// Called first.
public void init() {
// initialization
}
/* Called second, after init(). Also called
whenever the applet is restarted. */
public void start() {
// start or resume execution
}
// Called when the applet is stopped.
public void stop() {
// suspends execution
}
/* Called when applet is terminated. This is the
last
method executed. */
public void destroy() {
// perform shutdown activities
}
// Called when an applet's window must be
restored.
public void paint(Graphics g) {
// redisplay contents of window
}
Applet Initialization and
Termination
 When an applet begins, the following
methods are called in this sequence:
1. init( )
2. start( )
3. paint( )
 When an applet is terminated, the following
sequence of method calls takes place:
1. stop( )
2. destroy( )
 The init( ) method is the first method to be
called.
 In init( ) your applet will initialize variables
and perform any other start up activities
 The start( ) method is called after init( ).
 It is also called to restart an applet after it
has been stopped, such as when the user
returns to a previously displayed Web page
that contains an applet.
 The paint( ) method is called each time
your applet’s output must be redrawn
 When the page containing your applet is
left, the stop( ) method is called.
 You will use stop( ) to suspend any child
threads created by the applet and to
perform any other activities required to put
the applet in a safe, idle state.
 The destroy( ) method is called when the
applet is no longer needed.
Requesting Repainting
 Whenever your applet needs to update the
information displayed in its window, it
simply calls repaint( ).
 The repaint( ) method is defined by the
AWT’s Component class
 It causes the run-time system to execute a
call to your applet’s update( ) method,
which, in its default implementation, calls
paint( ).
 void repaint( );
◦ This version causes the entire window to be
repainted.
 void repaint(int left, int top, int width, int
height)
◦ the coordinates of the upper-left corner of the
region are specified by left and top, and the width
and height of the region are passed in width and
height.
The update( ) Method
 This method is defined by the Component
class
 it is called when your applet has requested
that a portion of its window be redrawn.
Using the Status Window
 an applet can also output a message to the
status window of the browser or applet
viewer on which it is running.
 The general form of showStatus( ) is
shown here:
 void showStatus(String msg)
 Here, msg is the string to be displayed.
Passing Parameters to
Applets
 You can pass parameters to your applet
 To do so, use the PARAM attribute of the
APPLET tag, specifying the parameter’s
name and value.
 To retrieve a parameter, use the
getParameter( )
 String getParameter(String paramName)
 It returns the value of the specified
parameter in the form of a String object.
 If the specified parameter cannot be found,
null is returned.
 Example: param.java
The applet class
Event Handling
 Most events to which your applet will
respond are generated by the user.
 These events are passed to your applet in a
variety of ways, with the specific method
depending upon the actual event.
 The most commonly handled events are
those generated by the mouse, the
keyboard, and various controls, such as a
push button.
 Events are supported by the
java.awt.event package.
The Delegation Event Model
 The delegation event model defines
standard and consistent mechanisms to
generate and process events
 a source generates an event and sends it to
one or more listeners
 the listener simply waits until it receives an
event
 Once received, the listener processes the
event and then returns.
Events
 an event is an object that describes a state
change in a source
 It can be generated as a consequence of a
person interacting with the elements in a
graphical user interface, such as pressing a
button, entering a character via the
keyboard, selecting an item in a list, and
clicking the mouse.
Event Sources
 An event source is an object that generates
an event.
 A source must register listeners in order for
the listener to receive notifications about a
specific type of event.
 Each type of event has its own registration
method
 public void addTypeListener(TypeListener
el)
 Here, Type is the name of the event, and el
is a reference to the event listener
 the method that registers a keyboard event
listener is called addKeyListener( ).
 The method that registers a mouse motion
listener is called
addMouseMotionListener( ).
 When an event occurs, all registered
listeners are notified and receive a copy of
the event object.
 A source must also provide a method that
allows a listener to unregister an interest in
a
 specific type of event. The general form of
such a method is this:
 public void
removeTypeListener(TypeListener el)
 Here, Type is the name of the event, and el
is a reference to the event listener. For
example, to
 remove a keyboard listener, you would call
removeKeyListener( ).
Event Listeners
 A listener is an object that is notified when
an event occurs.
 It has two major requirements.
◦ it must have been registered with one or more
sources to receive notifications about specific
types of events.
◦ it must implement methods to receive and
process these notifications.
Event Classes
 Java event class hierarchy is EventObject,
which is in java.util
 The class AWTEvent, defined within the
java.awt package, is a subclass of
EventObject
Event Listener Interfaces
 Event listeners receive event notifications
 When an event occurs, the event source
invokes the appropriate method defined by
the listener and provides an event object as
its argument.
 commonly used listener interfaces and
provides a brief description of the methods
they define.
Using the Delegation Event
Model
 Just follow these two steps:
◦ Implement the appropriate interface in the
listener so that it will receive the type of event
desired.
◦ Implement code to register and unregister (if
necessary) the listener as a recipient for the
event notifications
Handling Mouse Events
 you must implement the MouseListener
and the MouseMotionListener interfaces.
 The MouseListener interface defines five
methods.
◦ If a mouse button is clicked, mouseClicked( ) is
invoked.
◦ When the mouse enters a component, the
mouseEntered( ) method is called.
◦ When it leaves, mouseExited( ) is called.
◦ The mousePressed( ) and mouseReleased( )
methods are invoked when a mouse button is
pressed and released, respectively
 General forms
 void mouseClicked(MouseEvent me)
 void mouseEntered(MouseEvent me)
 void mouseExited(MouseEvent me)
 void mousePressed(MouseEvent me)
 void mouseReleased(MouseEvent me)
 The MouseMotionListener interface
defines two methods.
 The mouseDragged( ) method is called
multiple times as the mouse is dragged.
The mouseMoved( ) method is called
multiple times as the mouse is moved
 void mouseDragged(MouseEvent me)
 void mouseMoved(MouseEvent me)
More Java Keywords
 transient
 volatile
 instanceof
 native
 strictfp
 assert
The transient and volatile
Modifiers
 When an instance variable is declared as
transient, then its value need not persist
when an object is stored.
 Modifying a variable with volatile tells the
compiler that the variable can be changed
unexpectedly by other parts of your
program
instanceof
 object instanceof type
 object is an instance of a class, and type is
a class or interface type.
 If object is of the specified type or can be
cast into the specified type, then the
instanceof operator evaluates to true.
Otherwise, its result is false.
 Thus, instanceof is the means by which
your program can obtain run-time type
information about an object.
strictfp
 does not require the truncation of certain
intermediate values that occur during a
computation.
 By modifying a class or a method with
strictfp, you ensure that floating-point
calculations
 When a class is modified by strictfp, all of
the methods in the class are also strictfp
automatically
assert
 The assert keyword is used during program
development to create an assertion, which
is a condition that is expected to be true
during the execution of the program
 At run time, if the condition actually is true,
no other action takes place. However, if the
condition is false, then an AssertionError
is thrown.
 Two forms
 assert condition;
◦ Here, condition is an expression that must
evaluate to a Boolean result
 assert condition : expr;
◦ In this version, expr is a value that is passed to
the AssertionError constructor

java Unit4 chapter1 applets

  • 1.
    Unit 4- chapter01 Applets, Events, Miscellaneous Topics
  • 2.
    CONTENTS  Applet Basics, Applet Organization and Essential Elements, ◦ The Applet Architecture, ◦ A Complete Applet Skeleton, ◦ Applet Initialization and Termination, ◦ Requesting Repainting ◦ The update() Method,  Using the Status Window  Passing parameters to Applets  The Applet Class  Event Handling The Delegation Event Model  Events,  Using the Delegation Event Model,  More Java Keywords.
  • 3.
    Applet basics  Appletsoffer a secure way to dynamically download and execute programs over web.  Two types of applets ◦ Based on AWT ◦ Based on Swings  Sample program
  • 4.
    Commands required torun an applet javac filename.java appletviewer filename.html
  • 5.
    Commands required torun an applet appletviewer filename.java
  • 6.
     Begins withtwo import statements  Extends from Applet class  Named as SimpleApplet and should be public in nature because it will be accessed outside code.  Inside SimpleApplet , paint() is declared.  paint() is called each time the applet must redisplay its output.  It takes an object of graphics type as an input  It contains the graphics context, which describes the graphics environment in
  • 7.
     Inside paint()there is call to drawString() , which is member of Graphics class.  void drawString(String message, int x, int y);  Which draws the given text message in the specified x,y location.  Upper left location is 0,0  Applet begins the execution when the name of its class is passed to a browser or applet enabled program.
  • 8.
    Key points ofapplet  All applets are subclass of Applet  Applets do not need a main method  Applets must run under appletviewer or java compatible browser  User i/o is not accomplished with java’s stream i/o class, instead applet use the interface provided by the AWT or by Swing
  • 9.
    The applet architecture An applet is GUI based program  Applets are event driven  An applet resembles a set of interrupt service routines.  An applet waits until an event occurs.  The run-time system notifies the applet about an event by calling an event handler that has been provided by the applet.
  • 10.
     Once thishappens, the applet must take appropriate action and then quickly return control to the system  Second, it is the user who initiates interaction with an applet.  In a console-based program, when the program needs input, it will prompt the user and then call some input method
  • 11.
    A Complete AppletSkeleton  Five methods are used in applet ◦ init( ) ◦ start( ) ◦ paint() ◦ stop( ) ◦ destroy( )
  • 12.
    // An Appletskeleton. import java.awt.*; import java.applet.*; /* <applet code="AppletSkel" width=300 height=100> </applet> */ public class AppletSkel extends Applet { // Called first. public void init() { // initialization }
  • 13.
    /* Called second,after init(). Also called whenever the applet is restarted. */ public void start() { // start or resume execution } // Called when the applet is stopped. public void stop() { // suspends execution }
  • 14.
    /* Called whenapplet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities } // Called when an applet's window must be restored. public void paint(Graphics g) { // redisplay contents of window }
  • 15.
    Applet Initialization and Termination When an applet begins, the following methods are called in this sequence: 1. init( ) 2. start( ) 3. paint( )  When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( )
  • 16.
     The init() method is the first method to be called.  In init( ) your applet will initialize variables and perform any other start up activities  The start( ) method is called after init( ).  It is also called to restart an applet after it has been stopped, such as when the user returns to a previously displayed Web page that contains an applet.  The paint( ) method is called each time your applet’s output must be redrawn
  • 17.
     When thepage containing your applet is left, the stop( ) method is called.  You will use stop( ) to suspend any child threads created by the applet and to perform any other activities required to put the applet in a safe, idle state.  The destroy( ) method is called when the applet is no longer needed.
  • 18.
    Requesting Repainting  Wheneveryour applet needs to update the information displayed in its window, it simply calls repaint( ).  The repaint( ) method is defined by the AWT’s Component class  It causes the run-time system to execute a call to your applet’s update( ) method, which, in its default implementation, calls paint( ).
  • 19.
     void repaint(); ◦ This version causes the entire window to be repainted.  void repaint(int left, int top, int width, int height) ◦ the coordinates of the upper-left corner of the region are specified by left and top, and the width and height of the region are passed in width and height.
  • 20.
    The update( )Method  This method is defined by the Component class  it is called when your applet has requested that a portion of its window be redrawn.
  • 21.
    Using the StatusWindow  an applet can also output a message to the status window of the browser or applet viewer on which it is running.  The general form of showStatus( ) is shown here:  void showStatus(String msg)  Here, msg is the string to be displayed.
  • 24.
    Passing Parameters to Applets You can pass parameters to your applet  To do so, use the PARAM attribute of the APPLET tag, specifying the parameter’s name and value.  To retrieve a parameter, use the getParameter( )  String getParameter(String paramName)  It returns the value of the specified parameter in the form of a String object.
  • 25.
     If thespecified parameter cannot be found, null is returned.  Example: param.java
  • 26.
  • 29.
    Event Handling  Mostevents to which your applet will respond are generated by the user.  These events are passed to your applet in a variety of ways, with the specific method depending upon the actual event.  The most commonly handled events are those generated by the mouse, the keyboard, and various controls, such as a push button.  Events are supported by the java.awt.event package.
  • 30.
    The Delegation EventModel  The delegation event model defines standard and consistent mechanisms to generate and process events  a source generates an event and sends it to one or more listeners  the listener simply waits until it receives an event  Once received, the listener processes the event and then returns.
  • 31.
    Events  an eventis an object that describes a state change in a source  It can be generated as a consequence of a person interacting with the elements in a graphical user interface, such as pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse.
  • 32.
    Event Sources  Anevent source is an object that generates an event.  A source must register listeners in order for the listener to receive notifications about a specific type of event.  Each type of event has its own registration method  public void addTypeListener(TypeListener el)  Here, Type is the name of the event, and el is a reference to the event listener
  • 33.
     the methodthat registers a keyboard event listener is called addKeyListener( ).  The method that registers a mouse motion listener is called addMouseMotionListener( ).  When an event occurs, all registered listeners are notified and receive a copy of the event object.
  • 34.
     A sourcemust also provide a method that allows a listener to unregister an interest in a  specific type of event. The general form of such a method is this:  public void removeTypeListener(TypeListener el)  Here, Type is the name of the event, and el is a reference to the event listener. For example, to  remove a keyboard listener, you would call removeKeyListener( ).
  • 35.
    Event Listeners  Alistener is an object that is notified when an event occurs.  It has two major requirements. ◦ it must have been registered with one or more sources to receive notifications about specific types of events. ◦ it must implement methods to receive and process these notifications.
  • 36.
    Event Classes  Javaevent class hierarchy is EventObject, which is in java.util  The class AWTEvent, defined within the java.awt package, is a subclass of EventObject
  • 38.
    Event Listener Interfaces Event listeners receive event notifications  When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.  commonly used listener interfaces and provides a brief description of the methods they define.
  • 40.
    Using the DelegationEvent Model  Just follow these two steps: ◦ Implement the appropriate interface in the listener so that it will receive the type of event desired. ◦ Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications
  • 41.
    Handling Mouse Events you must implement the MouseListener and the MouseMotionListener interfaces.  The MouseListener interface defines five methods. ◦ If a mouse button is clicked, mouseClicked( ) is invoked. ◦ When the mouse enters a component, the mouseEntered( ) method is called. ◦ When it leaves, mouseExited( ) is called. ◦ The mousePressed( ) and mouseReleased( ) methods are invoked when a mouse button is pressed and released, respectively
  • 42.
     General forms void mouseClicked(MouseEvent me)  void mouseEntered(MouseEvent me)  void mouseExited(MouseEvent me)  void mousePressed(MouseEvent me)  void mouseReleased(MouseEvent me)
  • 43.
     The MouseMotionListenerinterface defines two methods.  The mouseDragged( ) method is called multiple times as the mouse is dragged. The mouseMoved( ) method is called multiple times as the mouse is moved  void mouseDragged(MouseEvent me)  void mouseMoved(MouseEvent me)
  • 44.
    More Java Keywords transient  volatile  instanceof  native  strictfp  assert
  • 45.
    The transient andvolatile Modifiers  When an instance variable is declared as transient, then its value need not persist when an object is stored.  Modifying a variable with volatile tells the compiler that the variable can be changed unexpectedly by other parts of your program
  • 46.
    instanceof  object instanceoftype  object is an instance of a class, and type is a class or interface type.  If object is of the specified type or can be cast into the specified type, then the instanceof operator evaluates to true. Otherwise, its result is false.  Thus, instanceof is the means by which your program can obtain run-time type information about an object.
  • 47.
    strictfp  does notrequire the truncation of certain intermediate values that occur during a computation.  By modifying a class or a method with strictfp, you ensure that floating-point calculations  When a class is modified by strictfp, all of the methods in the class are also strictfp automatically
  • 48.
    assert  The assertkeyword is used during program development to create an assertion, which is a condition that is expected to be true during the execution of the program  At run time, if the condition actually is true, no other action takes place. However, if the condition is false, then an AssertionError is thrown.
  • 49.
     Two forms assert condition; ◦ Here, condition is an expression that must evaluate to a Boolean result  assert condition : expr; ◦ In this version, expr is a value that is passed to the AssertionError constructor