Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?
Copyright © 1998-2009 Curt Hill Applets A different type of program.
-
Upload
wilfred-lamb -
Category
Documents
-
view
220 -
download
0
Transcript of Copyright © 1998-2009 Curt Hill Applets A different type of program.
![Page 1: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/1.jpg)
Copyright © 1998-2009 Curt Hill
Applets
A different type of program
![Page 2: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/2.jpg)
Copyright © 1998-2009 Curt Hill
What is an Applet?
• A small Java program run within something else
• Usually a web browser– Macro language for Corel Office Suite
• This is a very restrictive programming environment
• It can display window components and do graphics– Flow is default layout manager
![Page 3: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/3.jpg)
Copyright © 1998-2009 Curt Hill
Why write an applet?
• Need a web page with more than static text and graphics
• Need:– Animation other than animated gifs or
video/movie– Calculations or programming not in
HTML– Front end data entry for server
• Simple things often done with a script, more complicated with applet
![Page 4: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/4.jpg)
Copyright © 1998-2009 Curt Hill
Applet Restrictions• The browser is in control • Cannot create or access files from client
machine– Starting in 1.3 there is local workspace that
can be accessed– This is not a pipe into the whole file system
• Cannot run programs on client machine• Can only connect to the server machine
– Some harmless things may go elsewhere
• Cannot execute native methods– Native is machine language
![Page 5: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/5.jpg)
Copyright © 1998-2009 Curt Hill
Applet is a Panel and Container
• It has methods that can do the work• It has properties that can be
referenced from all its methods• It has events
– Methods that are called asynchronously– Called by browser– The event model has already been
integrated into the applet
![Page 6: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/6.jpg)
Copyright © 1998-2009 Curt Hill
Events• Something that happens at
unpredictable times• Event handler is executed when
the event occurs• Typical events that may need
handling:– The applet becomes visible and needs
to be drawn– A button is pushed – An error occurs
![Page 7: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/7.jpg)
Copyright © 1998-2009 Curt Hill
Applet methods• There are more than 20 standard
applet methods• Most of these are inherited from
ancestors• Five are event handlers:
– Each is automatically called by the browser or containing program
– Each has a characteristic signature• Overriding these gives the applet
desired behaviors• These do not need to be added
![Page 8: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/8.jpg)
Copyright © 1998-2009 Curt Hill
Applet Startup Events• public void init()
– Called once after applet is first loaded– Use for one time initializations– Use this instead of a constructor to
initialize instance variables
• public void start()– Called whenever the applet becomes
visible– Not when page becomes visible– May be used to start animations
![Page 9: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/9.jpg)
Copyright © 1998-2009 Curt Hill
Applet Paint Method• public void paint(Graphics g)
– Called by update to redraw the screen– A Graphics object, which holds the
panel– All displays on the panel are through
this graphics item– Only need to override if you are using
Graphics drawing items• Most GUI items (components)
repaint themselves– Paint must call super.paint() for this
![Page 10: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/10.jpg)
Copyright © 1998-2009 Curt Hill
Animations and Paint• Paint typically only needs to cause
the redraw of non components• Paint is itself called by update• public void update(Graphics g)
– update clears the screen and paints– Using the normal update causes
flicker in the screen because of the clear
– Thus update is overridden not to clear every time
![Page 11: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/11.jpg)
Copyright © 1998-2009 Curt Hill
Applet Finish Methods• public void stop()
– Called whenever the applet scrolls out of sight
– Should halt whatever start begins
• public void destroy()– Called once when applet is being
removed by browser – Use to release resources– There have been problems in the past
on browsers not calling it
![Page 12: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/12.jpg)
Copyright © 1998-2009 Curt Hill
Starting and Stopping
• start is called when applet becomes visible
• stop is called when it is not• There may be many pairs of start-
stop calls in a run• start and stop should be inverses
of each other– What one does the other reverses
![Page 13: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/13.jpg)
Notes
• Since the browser is in control there is no need for an exit button or a dialog box
• Any GUI components may be used• Event handlers for buttons are still
acceptable
Copyright © 1998-2009 Curt Hill
![Page 14: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/14.jpg)
Copyright © 1998-2009 Curt Hill
HTML
• Hyper Text Markup Language• A text based formatting language• Consists of text and tags• Text is written mostly without
regard to margins– The browser may dynamically change
margins
• XML is the successor– It may emulate HTML
![Page 15: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/15.jpg)
Copyright © 1998-2009 Curt Hill
HTML Tags
• A tag is enclosed in < > • A keyword identifies the function• Some parameters may be part of the
tag• Paired tags start with <xxx> and end
with </xxx>• Tag names are not sensitive to case• Some tags do not need a ending tag
![Page 16: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/16.jpg)
Copyright © 1998-2009 Curt Hill
Structure of HTML file
• Starts with <HTML> and ends with </HTML>
• The file is composed of two pieces within this:– Head - <HEAD> to </Head>– Body - <BODY> to </BODY>
• Different tags can be in each– These will be considered now, but
many more exist than than will be shown
![Page 17: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/17.jpg)
Copyright © 1998-2009 Curt Hill
HTML Head Tags
• <Title>Page Title</Title>– The title that most browsers show
• Various META tags that indicate author etc.
• HTML files produced by other programs (eg. Word) will produce many other tags in this area– These are usually <META> tags which
give a variety of auxiliary information like subject, program name, template, etc
![Page 18: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/18.jpg)
Copyright © 1998-2009 Curt Hill
HTML Body Tags
• The body is what shows in the browser window
• The Body tag can set the default colors, backgrounds etc
• Between <BODY> and </BODY> occurs all the content of the page as well
![Page 19: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/19.jpg)
Copyright © 1998-2009 Curt Hill
Paired Formatting tags
• <CENTER> to </CENTER>• <B> to </B> bold• <I> to </I> italics• <U> to </U> underline• <TT> to </TT> monospaced
typewriter font• <FONT> to </FONT> determines font
sizes and styles• <Hn> through </Hn> creates a
header
![Page 20: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/20.jpg)
Copyright © 1998-2009 Curt Hill
Unpaired tags
• Some HTML generators will pair these as well, but it is not necessary
• <P> new paragraph• <BR> a new line • <HR> produces a horizontal line
![Page 21: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/21.jpg)
Copyright © 1998-2009 Curt Hill
Java applet tags• <APPLET code=“X.class”
height=100 width=400> <PARAM name=p value=“parm value”>Default message </APPLET>
• code identifies the class file• size of panel is determined by height
and width• Multiple params can be entered• The default message is shown if the
browser cannot handle Java
![Page 22: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/22.jpg)
Copyright © 1998-2009 Curt Hill
Applet Methods
• getCodeBase() returns the URL• getAppletInfo() returns a string of
information about applet• getImage(URL) gets an image from
server• getParameter and
getParameterInfo describe parameters
![Page 23: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/23.jpg)
Copyright © 1998-2009 Curt Hill
Executing the applet
• Two possibilities– AppletViewer
• A minimal browser • Ignores all the other HTML
– Java Capable Web Browser• Usually harder to reload the applet
![Page 24: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/24.jpg)
Communication is Good!
• Parameters are the means that the HTML can communicate with the applet
• Two sides to this:– How the HTML passes the data– How Java catches the data
Copyright © 1998-2009 Curt Hill
![Page 25: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/25.jpg)
Recall the Java applet tags• <APPLET code=“X.class”
height=100 width=400> <PARAM name=p value=“parm value”>Default message </APPLET>
• code identifies the class file• size of panel is determined by height
and width• Params can be entered• The default message is shown if the
browser cannot handle Java
Copyright © 1998-2009 Curt Hill
![Page 26: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/26.jpg)
Required Parameters
• The height and width are special parameters
• They must be given in order that the browser knows the size of the applet to display
• However, they may be accessed from Java, just like any parameters
Copyright © 1998-2009 Curt Hill
![Page 27: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/27.jpg)
Optional Parameters
• The HTML for a parameter is a tag, which is enclosed within the applet tag
• Parameters tags have three pieces:– The PARAM tag label– The name of the parameter– The value of the parameter– These are separated by blanks in the
PARAM tag
Copyright © 1998-2009 Curt Hill
![Page 28: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/28.jpg)
<PARAM name=p value=“parm value”>
• The name is how the parameter will be identified in Java– Must be unique for this applet– Not case sensitive
• The value is always a string– Must be enclosed in quotes if it
contains special characters
Copyright © 1998-2009 Curt Hill
![Page 29: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/29.jpg)
Receiving the parameter
• The function getParameter must be used
• It returns a String and takes a String• The String it takes is the name of
the parameter• The String it returns is the value of
that parameter• If the parameter is not present, then
returned value is null
Copyright © 1998-2009 Curt Hill
![Page 30: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/30.jpg)
Obtaining parameters
• String s;s = getParameter(“width”);if (s != null) width = Integer.parseInt(s);s = getParameter(“Name”);if(s!=null) name = s;
Copyright © 1998-2009 Curt Hill
![Page 31: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/31.jpg)
Eclipse
• Use javax.swing.Japplet as the ancestor
• It will not give you much so you add the components that are needed in the init method
• It will recognize that you have an applet and not an application– It will use applet viewer
• One screen shot follows
Copyright © 1998-2009 Curt Hill
![Page 32: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/32.jpg)
Copyright © 1998-2009 Curt Hill
![Page 33: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/33.jpg)
Windows Builder
• Besides the ability to create and populate an application, we also can generate applets
• Mostly the same as applications, but no about/exit buttons are needed
Copyright © 1998-2009 Curt Hill
![Page 34: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/34.jpg)
Eclipse
• AppletViewer is the default way to view an applet
• The default sizes may changed• They are generally 200 by 200
Copyright © 1998-2009 Curt Hill
![Page 35: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/35.jpg)
Run Configuration
Copyright © 1998-2009 Curt Hill
![Page 36: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/36.jpg)
Main Tab
Copyright © 1998-2009 Curt Hill
![Page 37: Copyright © 1998-2009 Curt Hill Applets A different type of program.](https://reader035.fdocuments.in/reader035/viewer/2022062305/5697c01a1a28abf838ccee26/html5/thumbnails/37.jpg)
Parameters Tab
Copyright © 1998-2009 Curt Hill