Copyright © 1998-2009 Curt Hill Applets A different type of program
Jan 19, 2016
Copyright © 1998-2009 Curt Hill
Applets
A different type of program
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
<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
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
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
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
Copyright © 1998-2009 Curt Hill
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
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
Run Configuration
Copyright © 1998-2009 Curt Hill
Main Tab
Copyright © 1998-2009 Curt Hill
Parameters Tab
Copyright © 1998-2009 Curt Hill