Download Perl basics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Fundamentals of
Web Programming
Lecture 16: Java Applets &
AWT
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
1
Outline
• Why use Java Applets?
• What Java Applets can and can’t
do (example)?
• Compiling and adding an Applet to
a web page.
• Applet structure.
• Abstract Window Toolkit (AWT).
• Events.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
2
Why use Java Applets?
• Easily added to html.
• Executed within the browser
(client-side).
• Extensive computation at the client
possible, a full-blown programming
language (Java vs. JavaScript).
• Good support of graphics.
• Platform independence.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
3
Structure of an Applet
• All applets extend the Applet
class.
• They inherit a bunch of methods
that need to be redefined.
• They usually do not have the main
method (unless standalone).
• States in the browser: loaded,
running, stopped, unloaded.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
4
Applet states and methods
• Loaded: the code of the applet is
loaded with the web page;
• The browser calls init();
• Destroyed: when the applet is
unloaded (new page);
• The browser calls destroy()just
before unloading;
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
5
Applet states and methods
• Running: the applet is visible to
the user.
• When it becomes visible the
browser runs start().
• Stopped: when the applet
becomes temporarily invisible.
• The browser runs stop().
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
6
Applet states and methods
• init()typically allocates needed
resources, gets images/sounds
from the server (saves time), etc.
• start() is usually used to start
an animation.
• stop() stops an animation.
• destroy()deallocates system
resources other than memory.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
7
Applet states and methods
• Applets that provide graphical
contents usually redefine the
paint() method.
• The browser will call this method
whenever the applet window needs
to be updated.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
8
Restrictions on Applets
• Applets contain foreign code.
• It can be used to attack the client.
• Certain restrictions on what
applets can do.
• E.g., Applets have very restricted
access to the file system.
• More in the next lecture.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
9
Abstract Window Toolkit
• HTML provides ways to encode
standard elements of a web page
(checkboxes, labels, text fields,
etc).
• The user can assume that his web
page will look similar in different
browsers.
• AWT also platform independent.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
10
AWT
• AWT is a Java package, a collection
of reusable objects and methods.
• AWT provides graphical
components that look similar on all
platforms running Java.
• Provides labels, checkboxes, radio
buttons, buttons, choices, text
fields and areas (same as HTML).
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
11
AWT
• But also menus, containers and
various ways to align elements in
the applet window.
• Basic graphics supported: you can
draw lines, rectangles, ovals, etc
(not possible in HTML, unless you
load an image).
• You can have different buttons, not
only SUBMIT and CLEAR...
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
12
Layout Managers
• HTML doesn’t support many layout
choices.
• Typical HTML elements are laid out
in sequence as they are described
in the HTML code according to the
available space.
• This corresponds to the flow
layout.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
13
Layout Managers
• In HTML you can use a table to
align components.
• You can use the grid layout in an
Applet.
• Border layout is very handy;
divides the window in a five areas:
“North”, “South”, “East”, “West”
and “Center”.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
14
Layout Managers
• Components can be placed in
these areas.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
15
Layout Managers
• Grid Bag layout: like the grid
layout but you don’t have to
specify how may rows and
columns you need.
• Other layouts: card layout a stack
of containers with one visible at a
time.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
16
Containers
• In addition to Layout Managers,
AWT provides containers.
• Containers are used to organize
components into manageable
groups.
• Some containers (Panel) are
invisible and they only hold a
bunch of components together.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
17
Basic Graphics in AWT
• Basic methods for displaying
graphics in the Graphics object.
• An applet has a Graphics object
associated with it.
• It provided the context which all
graphical elements are drawn.
• It’s the argument of the paint
method.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
18
Graphics in AWT
• Graphics provides a coordinate
system in the applet window.
• The origin is in the upper left
corner.
• public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
}
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
19
Interaction with the user
• Interaction with the user is mediated by
events.
• An event is an object that AWT creates
when a user performs an action such as
moving the mouse.
• The event has to be handled by the
Applet code if you wish to react to the
action the user performed.
• You can also ignore the event.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
20
Examples of events
• Events are associated with a
component.
• Different events can be generated
for different components.
• Examples: button pressed, mouse
down, mouse dragged over a
component, changed the contents
of a text field.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
21
Handling events
• Unfortunately, Java 1.1 redefined
the event handling model.
• Will look only at the old one, since
it’s easier.
• You should use the new model in
your code.
• Yet another function to redefine:
action(Event e, Object arg)
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
22
Handling events
• public boolean
action(Event e, Object arg) {
if (e.target == button) {
buttonClicked();
return true;
}
return false;
}
• arg contains the parameter of e.
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
23
Other window toolkits
• AWT is very simple.
• It’s possible to use your own,
customized window toolkit.
• Sun provided the Swing
components with Java 1.2.
• New look, customizable, but not all
browsers support Java 1.2 yet...
20-753: Fundamentals of
Web Programming
Lecture 16: Java Applets & AWT
Copyright © 1999, Carnegie Mellon. All Rights Reserved.
24