6. Event-Driven Programming - PowerPoint PPT Presentation

About This Presentation
Title:

6. Event-Driven Programming

Description:

... Java programs can be embedded in HTML documents and executed through Web browsers ... URL naming the directory of the document in which the applet is embedded ... – PowerPoint PPT presentation

Number of Views:251
Avg rating:3.0/5.0
Slides: 35
Provided by: gene92
Category:

less

Transcript and Presenter's Notes

Title: 6. Event-Driven Programming


1
6. Event-Driven Programming
  • 6.1 The World Wide Web and Applets
  • 6.2 Paint Events
  • 6.3 Drawing Text
  • 6.4 Using Color
  • 6.5 Interfaces
  • 6.6 Event Listeners

2
Objectives
  • Learn the uses of protocols
  • Learn basic HTML tags
  • Write and test applets
  • Understand the idea of event-handling code
  • Respond to paint events
  • Take a first look at inheritance
  • Know AWT classes needed for drawing
  • Draw in color

3
The World Wide Web
  • The Internet gives us the capability to exchange
    information
  • The World Wide Web (WWW, or The Web) makes the
    exchange of information easy
  • Computers have rules of interactions, called
    protocols, specifying how they interact
  • Common protocols include HTTP (Hypertext Transfer
    Protocol), FTP (File Transfer protocol), SMTP
    (Simple Mail Transfer Protocol), POP3 (Post
    Office Protocol - version 3), etc.

4
The World Wide Web
  • For web programs, the most important protocol is
    HTTP
  • Hypertext - text augmented with links to other
    files
  • Hypermedia - a hypertext file that also links to
    images, and other resources
  • Two computers can use HTTP to communicate and
    transfer hypermedia
  • The computer sending hypermedia is called a web
    server

5
The World Wide Web
  • The program running on the other computer that
    receives and presents the hypermedia is a
    browser, e.g. Netscape, Internet Explorer, Mosaic
  • Browsers load and interpret documents that have
    been formatted using the HyperText Markup
    Language (HTML)
  • The Java language has very intimate relationship
    with Web processing since links to Java programs
    can be embedded in HTML documents and executed
    through Web browsers

6
Uniform Resource Locator
  • Information on the Web is found by identifying an
    URL
  • Three components the protocol, domain name, and
    path
  • http//java.sun.com/applets/index.html
  • http - the protocol
  • java.sun.com - domain name of the server (a
    unique address of that computer)
  • applets/index.html - a path to the resource

7
HTML Documents
  • WebPage.html
  • Comments in HTML documents starts with lt!-- and
    ends with --gt
  • The title displays at the top of the frame, not
    in the document itself
  • The text between the h1 tags has the largest size
  • The em tag causes the text to be displayed in
    italics

8
HTML Documents
  • Each item of an unordered list is preceded by a
    bullet
  • lta href"java.sun.com"gtSun's Home Pagelt/agt
    specifies a link to the page
  • ltimg src"gittleman.gif"gt specifies to include an
    image which is a .gif file
  • ltapplet code"Sort.class" width300 height200gt
    specifies the applet code to execute and the size
    of the page that it paints

9
Figure 6.2 Some HTML tags
10
More on Applets
  • An applet is a small program that is intended not
    to be run on its own, but rather to be embedded
    inside another application
  • The Applet class provides a standard interface
    between applets and their environment
  • Applets can be executed by a Web browser or
    appletviewer in JDK.
  • HelloCount.java

11
The Applet Method
  • public void init( ) - Initialize the applet.
    Called just after the applet is loaded
  • public void start( ) - Starts the applet. Called
    just after the applet is made active
  • public void stop( ) - Stops the applet. Called
    just after the applet is made inactive
  • public void destroy( ) - Destroys the applet.
    Called when the browser is exited
  • public void paint(Graphics g) - Inherits from the
    Container class. Called automatically when an
    applet needs to be redrawn

12
The Applet Method
  • getCodeBase( ) - Returns the base URL
  • getDocumentBase( ) - Returns an absolute URL
    naming the directory of the document in which the
    applet is embedded
  • getImage(URL url) - Returns an Image object that
    can then be painted on the screen
  • getImage(URL url, String name) - Returns an Image
    object that can then be painted on the screen
  • getLocale( ) - Gets the Locale for the applet, if
    it has been set
  • getParameter(String name) - Returns the value of
    the named parameter in the HTML tag

13
The Applet Method
  • getSize( ) - Returns the size of this applet in
    the form of a Dimension object
  • getWidth( ) - Return the current width of this
    applet
  • getX( ), getY( ) - Return the current x or y
    coordinate of the components origin
  • play(URL url) - Plays the audio clip at the
    specified absolute URL
  • resize(Dimension d) - Requests that this applet
    be resized
  • resize(int width, int height) - Requests that
    this applet be resized

14
The Graphic Class
  • drawArc(int x, int y, int width, int height, int
    startAngle, int arcAngle)
  • Draws the outline of a circular or elliptical
    arc covering the specified rectangle
  • drawLine(int x1, int y1, int x2, int y2)
  • Draws a line, using the current color, between
    the points (x1, y1) and (x2, y2) in this graphics
    context's coordinate system
  • drawOval(int x, int y, int width, int height)
    Draws the outline of an oval

15
The Graphic Class
  • drawPolygon(int xPoints, int yPoints,
  • int nPoints)
  • Draws a closed polygon defined by arrays of x
    and y coordinates
  • drawPolyline(int xPoints, int yPoints,
  • int nPoints)
  • Draws a sequence of connected lines defined by
    arrays of x and y coordinates
  • drawRect(int x, int y, int width, int height)
    Draws the outline of the specified rectangle

16
The Graphic Class
  • drawRoundRect(int x, int y, int width,
  • int height, int arcWidth, int arcHeight)
  • Draws an outlined round-cornered rectangle using
    this graphics context's current color
  • drawString(String str, int x, int y)
  • Draws the text given by the specified string,
    using this graphics context's current font and
    color
  • fill3DRect(int x, int y, int width, int height,
    boolean raised)
  • Paints a 3-D highlighted rectangle filled with
    the current color

17
The Graphic Class
  • fillOval(int x, int y, int width, int height)
  • Fills an oval bounded by the specified rectangle
    with the current color
  • fillPolygon(int xPoints, int yPoints, int
    nPoints)
  • Fills a closed polygon defined by arrays of x
    and y coordinates
  • fillRect(int x, int y, int width, int height)
  • Fills the specified rectangle
  • fillRoundRect(int x, int y, int width, int
    height,
  • int arcWidth, int arcHeight)
  • Fills the specified rounded corner rectangle
    with the current color

18
The Graphic Class
  • getColor( )
  • Gets this graphics context's current color
  • getFont( )
  • Gets the current font
  • getFont(Font f)
  • Gets the font metrics for the specified font
  • setColor(Color c)
  • Sets this graphics context's current color to
    the specified color
  • setFont(Font f)
  • Sets this graphics context's font to the
    specified font

19
Center this string
n
w
Figure 6.8 Centering a string
20
Drawing Text
  • Text.java
  • The three arguments to the Font constructors are
    the font name, the style, and the point size.
    Styles are constants of the Font class
  • The FontMetrics class has many methods to provide
    data about the font. Each font has its own
    FontMetrics object
  • The getName method returns the name of the font
  • The stringWidth method of the FontMetrics returns
    the width in pixels of its string argument as
    drawn in this font

21
Using Color
  • ColorChips.java
  • MoreColor.java
  • SnowMan.java (extra)
  • Bullseye.java (extra)

22
  • public interface Drivable
  • public static final int LEFT 0
  • public static final int RIGHT 1
  • public void start()
  • public void stop()
  • public void accelerate()
  • public void decelerate()
  • public void turn(int direction)

Figure 6.12 The Drivable interface
23
  • public static void goForward(Drivable d)
  • d.start()
  • d.accelerate()
  • d.decelerate()

Figure 6.13 Using Drivable
24
Interfaces
  • SportsCar.java
  • Van.java
  • TestDrivable.java

25
Events
  • An event is an object that represents some
    activity to which we may want to respond
  • For example, we may want our program to perform
    some action when the following occurs
  • the mouse is moved
  • a mouse button is clicked
  • the mouse is dragged
  • a graphical button is clicked
  • a keyboard key is pressed
  • a timer expires

26
Events
  • Often events corresponds to user actions, but not
    always
  • The Java standard library contains several
    classes that represent typical events
  • Certain objects, such as an applet or a graphical
    button, generate (fire) an event when it occurs
  • Other objects, called listener, respond to events
  • We can write listener objects to do whatever we
    want when an event occurs

27
Listener Interfaces
  • We can create a listener object by writing a
    class that implements a particular listener
    interface
  • The Java standard class library contains several
    interfaces that correspond to particular event
    categories
  • For example, the MouseListener interfcae contains
    methods that correspond to mouse events
  • After creating the listener, add the listener to
    the component that might generate the event to
    set up a relationship between the generator and
    listener

28
Mouse Events
  • The following are mouse events
  • mouse pressed - the mouse button is pressed down
  • mouse released - the mouse button is released
  • mouse clicked - the mouse button is pressed and
    released
  • mouse entered - the mouse pointer is moved over a
    particular component
  • mouse exited - the mouse pointer is moved off of
    a particular component
  • Any given program can listen for some, none, or
    all of these

29
Mouse Motion Events
  • The following are mouse motion events
  • mouse moved - the mouse is moved
  • mouse dragged - the mouse is moved while the
    mouse button is held down
  • There is a corresponding MouseMotionListener
    interface
  • One class can serve as both a generator and a
    listener
  • One class can serve as a listener for mutiple
    event type

30
Examples
  • Dots.java (extra)
  • DotsMouseListener.java (extra)
  • RubberLines.java (extra)
  • TicketEvent.java, TicketListener.java,
    Student.java, Scalper.java, Promoter.java,
    TicketTest.java

31
  • The student registers with the promoter as a
    TicketListener.
  • The scalper registers with the promoter as a
    TicketListener.
  • The promoter creates a TicketEvent.
  • The promoter passes the TicketEvent to the
    student's concertComing method.
  • The student's concertComing method decides
    whether to buy tickets.
  • The promoter passes the TicketEvent to the
    scalper's concertComing method.
  • The scalper's concertComing method decides
    whether to buy tickets.

Figure 6.14 Student, scalper, and promoter
interactions
32
Key Events
  • The following are called key events
  • key pressed - a keyboard key is pressed down
  • key released - a keyboard key is released
  • key typed - a keyboard key is pressed and
    released
  • The KeyListener interface handles key events
  • Listener classes are often implemented as inner
    class, nested within the component that they are
    listening to
  • Direction.java (extra)

33
Animations
  • An animation is a constantly changing series of
    pictures or images that create the illusion of
    movement
  • We can create animation in Java bu changing a
    picture slightly over time
  • The speed of a Java animation is usually
    controlled by a Timer object
  • The Timer class is defined in the javax.swing
    package

34
Animations
  • A Timer object generates ActionEvent every n
    milliseconds (where n is set by the object
    creator)
  • The ActionListener interface contains an
    actionPerformed method
  • Whenever the timer expires (generates an
    ActionEvent) the animation can be updated
  • Rebound.java (extra)
Write a Comment
User Comments (0)
About PowerShow.com