Java AppletsIntroduction to Java Applet ProgramsSlide 3Java Applet ClassesSlide 5Contrast Application with AppletApplet DeclarationBody of an AppletAppletsApplets and Web Pages – HTMLApplets and Web Pages – HTMLSlide 12Slide 13Thinking About ObjectsSlide 15Slide 16A Simple Java Applet: Drawing a StringRunning the AppletRunning the Applet - AlternativelySlide 20Running An AppletSlide 22Java AppletsJava Applets22Introduction to Java Applet Introduction to Java Applet ProgramsProgramsApplications are stand alone programsApplications are stand alone programsexecuted with Java interpreterexecuted with Java interpreterApplet is a small programApplet is a small programcan be placed on a web pagecan be placed on a web pagewill be executed by the web browserwill be executed by the web browsergive web pages “dynamic content”give web pages “dynamic content”33Java AppletsJava AppletsBuilt using one of general definitions of Built using one of general definitions of appletsappletsAppletApplet class classJAappletJAapplet class classJava applets are usually graphicalJava applets are usually graphicalDraw graphics in a defined screen areaDraw graphics in a defined screen areaEnable user interaction with GUI elementsEnable user interaction with GUI elements44Java Applet ClassesJava Applet ClassesAAbstract bstract WWindowing indowing TToolkit oolkit AWTAWTEarlier versions of JavaEarlier versions of JavaAppletApplet class is one of the AWT components class is one of the AWT componentsJJava ava FFoundation oundation CClasses lasses JFCJFCExtension to Java in 1997Extension to Java in 1997Has a collection of Has a collection of Swing componentsSwing components for for enhanced GUIsenhanced GUIsSwing component classes begin with Swing component classes begin with JJ55Java AppletsJava AppletsApplets are Java programs that can be Applets are Java programs that can be embedded in HTML documentsembedded in HTML documentsTo run an applet you must create a .html file which To run an applet you must create a .html file which references the appletreferences the appletReady to ProgramReady to Program also will run an applet also will run an appletWhen browser loads Web page containing appletWhen browser loads Web page containing appletApplet downloads into Web browserApplet downloads into Web browserbegins executionbegins executionCan be tested using appletviewer programCan be tested using appletviewer program66Contrast Application with Applet Contrast Application with Applet Application Applet•Object class extended•Class not declared public•Has a main()•static keyword used•Uses System.exit(1)•JApplet class extended•class declared to be public•init() instead of main()•init() not declared with static keyword77Applet DeclarationApplet DeclarationSyntax (note difference from Syntax (note difference from application application declaration)declaration)public class ClassName extends JAappletpublic class ClassName extends JAapplet ClassName is an object that will be a subclass of JApplet88Body of an AppletBody of an AppletNote there is no Note there is no main()main() method in an method in an appletappletJAppletJApplet class provides other methods instead class provides other methods instead of a of a mainmain method methodFirst method executed is the First method executed is the init()init() methodmethod99AppletsAppletsAppletAppletProgram that runs in Program that runs in •appletviewerappletviewer (test utility for applets) (test utility for applets)•Web browser (IE, Communicator)Web browser (IE, Communicator)Executes when HTML (Hypertext Markup Language) Executes when HTML (Hypertext Markup Language) document containing applet is openeddocument containing applet is openedApplications run in command windowsApplications run in command windowsNotesNotesFocus on fundamental programming concepts firstFocus on fundamental programming concepts first•Explanations will come laterExplanations will come later1010Applets and Web Pages – Applets and Web Pages – HTML HTML Applets embedded in a web pageApplets embedded in a web pageExecuted when web page loaded by browserExecuted when web page loaded by browserWeb pages structured with Web pages structured with HTMLHTML codes codesHHyperyperTText ext MMark-upark-up LLanguageanguageSyntaxSyntax<command><command> . . . . . .</command></command>Turns format onTurns format onTurns the format offTurns the format off1111Applets and Web Pages – Applets and Web Pages – HTMLHTMLEmbedding Java appletsEmbedding Java appletsInsert applet tagsInsert applet tags<APPLET><APPLET></APPLET></APPLET>Call the specific applet by its file nameCall the specific applet by its file name<APPLET CODE = "Whatever.class" <APPLET CODE = "Whatever.class" WIDTH = nnn HEIGHT = mmmm> WIDTH = nnn HEIGHT = mmmm><\APPLET><\APPLET>Where Where nnnnnn and and mmmmmm are specific pixel sizes are specific pixel sizes1212Applets and Web Pages – Applets and Web Pages – HTMLHTMLCreate the web Create the web page code using a page code using a text editortext editorSave it with an Save it with an .html suffix.html suffixOpen this file with Open this file with appletviewer or with appletviewer or with a web browser that a web browser that supports Javasupports JavaJava Plug-in must Java Plug-in must be installed (part of be installed (part of J2SDK 1.4.1 from J2SDK 1.4.1 from Sun)Sun)<HTML><HEAD></HEAD><BODY><APPLET CODE = . . . ></APPLET></BODY></HTML>1313Applets and Web Pages – Applets and Web Pages – HTMLHTMLClient Web browser anywhere can access Client Web browser anywhere can access this web page from its host serverthis web page from its host serverEmbedded Java applet runs on client Embedded Java applet runs on client browser (of any type platform)browser (of any type platform)This means a client anywhere on any type This means a client anywhere on any type of platform can run a piece of software of platform can run a piece of software developed on any other type of platformdeveloped on any other type of platformPlatform Independence1414Thinking About ObjectsThinking About ObjectsJava an object-oriented languageJava an object-oriented languageHowever, Java has constructs from structured However, Java has constructs from structured programmingprogrammingObject orientationObject
View Full Document