Title: Lecture 09 Applet and Advanced GUI
1Lecture 09Applet and Advanced GUI
2Applet
- Applets run within the Web browser environment
- Applets bring dynamic interaction and live
animation to an otherwise static HTML page - Java is used not only as applets, but also as
standalone applications and as a programming
language for developing sever-side applications - Applications and applets share many common
programming features, although they differ
slightly in some respects - Every application must have a main method
- Java applets do not need a main method
3The Applet Class
- public class MyApplet extends JApplet
-
- public void init()
- ...
- public void start()
- ...
- public void stop()
- ...
- public void destroy()
- ...
- //your other methods
4Browser Calling Applet Methods
5The inti method
- Invoked when the applet is first loaded and again
if the applet is reloaded. - public void init( )
- can be seen and used by other
classes and programs, such as the browser - Common functions implemented in this method
include loading images, setting up user-interface
components, and getting parameters from the
ltappletgt tag in the HTML page.
6The start method
- Invoked after the init() method is executed
also called whenever the applet becomes active
again after a period of inactivity - For example, when the user returns to the page
containing the applet after surfing other Web
pages.
7The stop Method
- The opposite of the start() method, which is
called when the user moves back to the page
containing the applet the stop() method is
invoked when the user moves off the page.
8The destroy Method
- Invoked when the browser exits normally to inform
the applet that it is no longer needed and that
it should release any resources it has allocated - Usually, you will not need to override this
method unless you need to release specific
resources, such as threads that the applet
created.
9The JApplet Class
- To use swing components, it is necessary to
create a Java Applet that extends
javax.swing.JApplet - JApplet inherits all the methods from the Applet
class
import javax.swing. public class Welcome
extends JApplet
10Writing Applets
- Always extends the JApplet class, which is a
subclass of Applet for Swing components. - Override init(), start(), stop(), and destroy()
if necessary. By default, these methods are
empty. - Add your own methods and data if necessary.
- Applets are always embedded in anHTML page.
11Viewing Applets
lthtmlgt ltheadgt lttitlegt lt/headgt ltbodygt ltapplet co
de test.Welcome.class width 300 height
100gt lt/appletgt lt/bodygt lt/htmlgt
12Example
13Applications vs. Applets
- Similarities
- Since they both are subclasses of the Container
class, all the user interface components, layout
managers, and event-handling features are the
same for both classes. - Differences
- Applications are invoked by the Java interpreter,
and applets are invoked by the Web browser. - Applets have security restrictions
- Web browser creates graphical environment for
applets, GUI applications are placed in a frame.
14Font class
- Font constructor takes three arguments
- Font name, font style, and font size
- General Format
- Font (FontName, FontStyle, FontSize)
- Font fntName new Font (Sans Serif,
Font.BOLD, 12) - Methods
- getFont ( )
- setFont (Font f )
- Sets the current font to font, style, and size
specified by the Font object reference f - E.g
- lblName.setFont(fntName)
15The FontMetrics Class
- Can be used to know precise information about a
font, such as height, descent, ascent, and leading
16FontMetrics Methods
- getAscent( )
- Return a value representing the ascent of a font
- getDescent( )
- Return a value representing the descent of a font
- getLeading( )
- Return a value representing the leading of a font
- getHeight( )
- Return a value representing the height of a font
17Color class
- Defines methods and constants for manipulating
colors - Pre-defined color
- RGB based 256 colors
- Constructors
- Three arguments
- Integer and float
- public Color (int r, int g, int b)
- public color (float r, float g, float b)
18Color Class Methods
- getRed( )
- Return a value between 0 to 255 representing the
red content - getGreen( )
- Return a value between 0 to 255 representing the
green content - getBlue( )
- Return a value between 0 to 255 representing the
blue content - getColor
- Return the current color
- setColor
- Sets the current color
19Changing the Color of Text
- setForeground method
- Changes the color of the letter
- E.g
- lblName.setForeground(Color.red)
- setBackground method
- Changes the background color
- E.g.
- setBackground(Color.cyan)
- Pre-defined colors
- black, blue, cyan, darkGray, gray, green, pink,
lightGray, magenta, orange, red, white, yellow
20Example
21Mouse Event
- Many actions taken by the user cause events to
occur with mouse - Click the mouse, move it over an object, move
away from an object - Java allows you to listen for mouse events using
a MouseListner or MouseMotionListner
22Handling Mouse Events
- The MouseListener listens for actions such as
when the mouse is pressed, released, entered,
exited, or clicked. - The MouseMotionListener listens foractions such
as dragging or moving themouse.
23MouseEvent Handlers
- mouseEnter(MouseEvent e) and mouseExit(MouseEvent
e) - invoked when a mouse enters a component or exits
the component - mousePressed(MouseEvent e)
- Invoked when a mouse is pressed or released
- mouseClicked(MouseEvent e)
- Invoked when a mouse is pressed and then released
- mouseMoved(MouseEvent e)
- Invoked when the mouse is moved without being
pressed
24Example
25Keyboard Event Handling
- Key events are generated when key on the keyboard
are pressed and released - A class that implements KeyListener must provide
definitions for method keyPressed, keyReleased
and keyTyped
26InputEvent Methods
- public long getWhen( )
- Returns the time stamp indicating when the event
occured - public boolean isAltDown( )
- Returns true if the Alt key is down on the event
- public boolean isControlDown( )
- Returns true if the Control key is down on the
event - public boolean is MetaDown
- Returns true if the right mouse button is pressed
- public boolean is ShiftDown
- Returns true if the Shift key is down on the event
27Handling Keyboard Events
To process a keyboard event, use the following
handlers in the KeyListener interface
- keyPressed(KeyEvent e)
- Called when a key is pressed.
- keyReleased(KeyEvent e)
- Called when a key is released.
- keyTyped(KeyEvent e)
- Called when a key is pressed and then released.
28The KeyEvent Class
- Methods
- getKeyChar() method
- getKeyCode() method
- Keys
- Home VK_HOME
- End VK_End
- Page Up VK_PGUP
- Page Down VK_PGDN
- etc..
29Event Adapters
- Standard adapters
- Class that extends a convenience listener adapter
or implements a listener interface - Anonymous adapters
- The adapters implemented using inner classes
30Event Adapter Class and Interface
Event Adapter Class
Implements Interface
ComponentAdapter ContainerAdapter FocusAdapter Key
Adapter MouseAdapter MouseMotionAdapter WindowAdap
ter
CompoenentListerner ContainerListerner FocusLister
ner KeyListerner MouseListerner MouseMotionListern
er WindowListerner