1 frameworks. 2 framework set of cooperating classes/interfaces –structure essential mechanisms of...
Post on 19-Dec-2015
225 views
TRANSCRIPT
![Page 1: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/1.jpg)
1
Frameworks
![Page 2: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/2.jpg)
2
Framework
• Set of cooperating classes/interfaces– Structure essential mechanisms of a problem
domain– Programmer can extend framework classes,
creating new functionality
• Example: Swing package is framework for problem domain of GUI programming
![Page 3: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/3.jpg)
3
Frameworks Vs. Patterns
• Pattern: general design rule for solving a problem
• Framework: set of classes providing functionality in a particular domain; often uses multiple patterns
![Page 4: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/4.jpg)
4
Application Frameworks
• Implement services common to an type of application
• Supply sets of classes for programmer to augment to build applications (often by subclassing framework classes)
![Page 5: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/5.jpg)
5
Inversion of Control
• Framework classes, not application-specific classes, control flow of execution
• Framework determines which methods to call and when
![Page 6: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/6.jpg)
6
Framework example: Applets
• Applet: Java program that can be included in a web document to add interactions and dynamism
• Java source is referenced by an HTML document• Package java.applet is a simple application
framework– consists of superclasses to make applets
– programmer adds classes, overrides methods to create actual applets
![Page 7: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/7.jpg)
7
Applet characteristics
• No main() method
• Programmer overrides some or all of the following methods:– init()– start()– stop()– destroy()– paint()
![Page 8: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/8.jpg)
8
How applets work
• Since an applet has no main() method, initialization of object takes place in method init(), which takes the place of both main() and the constructor
• Method init() is typically used to perform initializations of short duration; longer running initializations, e.g. loading a file across a network, should be done in separate threads
![Page 9: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/9.jpg)
9
Methods of Applet class
• Method init() is empty, and must be overridden by the child class
• Method start() is called to set up the applet to run– may create several threads– called when applet is loaded & initialized the
first time, resized, or when user returns to page from elsewhere
– paint() automatically called after start()
![Page 10: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/10.jpg)
10
Methods of Applet class
• Method stop() is called to stop the applet - usually means halting threads started by start() when, for example, the user leaves the page
• Method destroy() performs final cleanup before the applet is unloaded
![Page 11: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/11.jpg)
11
Methods of Applet class
• Control sequence is:– init()– start()– paint()– then stop() or destroy(), as appropriate
• Applet can transition many times between start() and stop() states
• May define all, some, or none of these methods when extending an Applet; should only define those necessary for specific applet
![Page 12: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/12.jpg)
12
HTML for applet inclusion
<APPLET codebase = “URL for code location”code = class namewidth = #height = #>
<param name = somename value = value>Alternate text to display in lieu of applet</APPLET>
![Page 13: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/13.jpg)
13
Applet HTML dissected
• Codebase: URL web address where code can be found (can be just a file name)
• code: name of class (name of file in an application)
• height and width tell browser how much space to allocate for applet
• param tags: provide parameters, analogous to command-line parameters - values can be accessed within applet via call to getParameter()
![Page 14: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/14.jpg)
14
More fun facts about applets
• Applet class is actually subclass of Panel - so inherits graphical component attributes
• Events, in particular, are handled same way as other graphical components, and window repainting same as application
• Default layout for applet is flow layout, not border layout
![Page 15: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/15.jpg)
15
Other methods provided by Applet class
• getImage(URL): takes URL and retrieves image from that location (must be gif or jpg)
• getAudioClip(URL): returns audio object from given location; can subsequently be asked to play - shorthand version play(URL) combines get and play methods
![Page 16: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/16.jpg)
16
Other methods provided by Applet class
• getCodeBase() returns URL for codebase specified for applet
• getParameter() takes String argument and returns associated String value if <param> tag was provided - returns null if none was provided
![Page 17: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/17.jpg)
17
Java class URL
• URL class is provided by java.net.*
• URL object supplies methods to parse URL, open network connections, and retrieve information
• Example:URL newURL = new URL(String s);
creates URL object newURL with specification s
![Page 18: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/18.jpg)
18
Java class URL
• Can create URLs using Java’s URL class - address is formed from a String, or previous URL plus a String - latter from could be used, for example, to retrieve several files from the same directory
• URL class provides method openStream, which returns an InputStream value - once an URL object is created, can use this method to read from the URL using normal InputStream methods
![Page 19: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/19.jpg)
19
Using applet to open a new page
• Can use showDocument() method of class appletContext to instruct browser to load a new web page:
appletContext.showDocument(URL)– URL refers to a Java URL object (not a string)– browser will display the page corresponding to
the URL
![Page 20: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/20.jpg)
20
Applets vs. Applications
• An applet has no main method, as it is structured to run inside another application
• The Java Virtual Machine runs applications, but not applets
• Can use appletviewer program to test and debug applet code
• Most applets are event-driven (but don’t have to be)
![Page 21: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/21.jpg)
21
Applets vs. Applications
• Applets do I/O through the browser GUI, and displays error messages (only) to stdout
• An applet is a subclass of Applet from java.applet
• Applets have security restrictions: can’t read or write files, run local executable programs, communicate with hosts other than the server, or learn many facts about the client computer
![Page 22: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/22.jpg)
22
Creating Applet/Application Hybrids
• Applet class pays no attention to static methods contained in class definition
• Key idea is that Applet is a Panel– can nest inner class within Applet to create Frame necessary for
application
– only component created for this Frame will be Panel constructed by Applet
– Method main(), which is ignored when executing as applet, creates instance of applet when run as an application
– Applet then creates instance of Frame, placing itself in center
– Constructor for Frame executes requisite init() and start()
![Page 23: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/23.jpg)
23
Applets as Framework
• Programmer for individual applet uses inheritance to extend the applet framework class to specific program
• Applet class deals with behavior common to all applets; programmer only fills in custom behavior for specific application
![Page 24: 1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,](https://reader031.vdocuments.site/reader031/viewer/2022032703/56649d375503460f94a1087e/html5/thumbnails/24.jpg)
24
Applets as Framework
• Inversion of control means applet programmer not concerned with overall control flow - fills in handlers for initialization, starting, stopping and painting, but has no control over when these methods are called