Lecture 09 Applet and Advanced GUI - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Lecture 09 Applet and Advanced GUI

Description:

... the user returns to the page containing the applet after surfing other Web pages. ... Anonymous adapters. The adapters implemented using inner classes ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 31
Provided by: jaeki8
Learn more at: http://jsong.ba.ttu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 09 Applet and Advanced GUI


1
Lecture 09Applet and Advanced GUI
  • Jaeki Song

2
Applet
  • 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

3
The Applet Class
  • public class MyApplet extends JApplet
  • public void init()
  • ...
  • public void start()
  • ...
  • public void stop()
  • ...
  • public void destroy()
  • ...
  • //your other methods

4
Browser Calling Applet Methods
5
The 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.

6
The 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.

7
The 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.

8
The 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.

9
The 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
10
Writing 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.

11
Viewing Applets
lthtmlgt ltheadgt lttitlegt lt/headgt ltbodygt ltapplet co
de test.Welcome.class width 300 height
100gt lt/appletgt lt/bodygt lt/htmlgt
12
Example
  • HelloWorld
  • Mortgage

13
Applications 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.

14
Font 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)

15
The FontMetrics Class
  • Can be used to know precise information about a
    font, such as height, descent, ascent, and leading

16
FontMetrics 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

17
Color 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)

18
Color 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

19
Changing 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

20
Example
  • FontApplet

21
Mouse 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

22
Handling 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.

23
MouseEvent 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

24
Example
  • MouseListener

25
Keyboard 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

26
InputEvent 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

27
Handling 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.

28
The KeyEvent Class
  • Methods
  • getKeyChar() method
  • getKeyCode() method
  • Keys
  • Home VK_HOME
  • End VK_End
  • Page Up VK_PGUP
  • Page Down VK_PGDN
  • etc..

29
Event Adapters
  • Standard adapters
  • Class that extends a convenience listener adapter
    or implements a listener interface
  • Anonymous adapters
  • The adapters implemented using inner classes

30
Event 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
Write a Comment
User Comments (0)
About PowerShow.com