6. event-driven programming

34
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

Upload: brick

Post on 21-Jan-2016

38 views

Category:

Documents


1 download

DESCRIPTION

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. Objectives. Learn the uses of protocols Learn basic HTML tags Write and test applets Understand the idea of event-handling code - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 6. Event-Driven Programming

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

Page 2: 6. Event-Driven Programming

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

Page 3: 6. Event-Driven Programming

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.

Page 4: 6. Event-Driven Programming

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

Page 5: 6. Event-Driven Programming

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

Page 6: 6. Event-Driven Programming

Uniform Resource Locator

• Information on the Web is found by identifying an URL

• Three components: the protocol, domain name, and pathhttp://java.sun.com/applets/index.html

http - the protocoljava.sun.com - domain name of the server (a

unique address of that computer)

applets/index.html - a path to the resource

Page 7: 6. Event-Driven Programming

HTML Documents

• WebPage.html• Comments in HTML documents starts with

<!-- and ends with -->• 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

Page 8: 6. Event-Driven Programming

HTML Documents

• Each item of an unordered list is preceded by a bullet

• <a href="java.sun.com">Sun's Home Page</a> specifies a link to the page

• <img src="gittleman.gif"> specifies to include an image which is a .gif file

• <applet code="Sort.class" width=300 height=200> specifies the applet code to execute and the size of the page that it paints

Page 9: 6. Event-Driven Programming

A Java applet.<applet>...</applet>

An image.<img>

An anchor, a hypertext link.<a>... </a>

Element of a list.<1i>

An unordered list.<u1> ... </u1>

Third-level header., (lowest is sixth).<h3>... </h3>

Top-level header.<h1>... </h1>

Title, displayed separately from text.<title>... </title>

Strongly emphasize the text.<strong>...</strong>

Emphasize the text.<em> ... </em>

New paragraph (after a blank line).<p>

Break to the next line.<br>

Figure 6.2 Some HTML tags

Page 10: 6. Event-Driven Programming

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

Page 11: 6. Event-Driven Programming

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

Page 12: 6. Event-Driven Programming

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

Page 13: 6. Event-Driven Programming

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

Page 14: 6. Event-Driven Programming

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

Page 15: 6. Event-Driven Programming

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

Page 16: 6. Event-Driven Programming

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

Page 17: 6. Event-Driven Programming

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

Page 18: 6. Event-Driven Programming

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

Page 19: 6. Event-Driven Programming

Center this string

n

w

Figure 6.8 Centering a string

Page 20: 6. Event-Driven Programming

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

Page 21: 6. Event-Driven Programming

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

Page 22: 6. Event-Driven Programming

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

Page 23: 6. Event-Driven Programming

public static void goForward(Drivable d) {

d.start();

d.accelerate();

d.decelerate();

}

Figure 6.13 Using Drivable

Page 24: 6. Event-Driven Programming

Interfaces

• SportsCar.java• Van.java• TestDrivable.java

Page 25: 6. Event-Driven Programming

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

Page 26: 6. Event-Driven Programming

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

Page 27: 6. Event-Driven Programming

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

Page 28: 6. Event-Driven Programming

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

Page 29: 6. Event-Driven Programming

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

Page 30: 6. Event-Driven Programming

Examples

• Dots.java (extra)

• DotsMouseListener.java (extra)

• RubberLines.java (extra)

• TicketEvent.java, TicketListener.java, Student.java, Scalper.java, Promoter.java, TicketTest.java

Page 31: 6. Event-Driven Programming

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

Page 32: 6. Event-Driven Programming

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)

Page 33: 6. Event-Driven Programming

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

Page 34: 6. Event-Driven Programming

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)