Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1998-2009 Curt Hill Applets A different type of program.

Similar presentations


Presentation on theme: "Copyright © 1998-2009 Curt Hill Applets A different type of program."— Presentation transcript:

1 Copyright © 1998-2009 Curt Hill Applets A different type of program

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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 and end with Tag names are not sensitive to case Some tags do not need a ending tag

16 Copyright © 1998-2009 Curt Hill Structure of HTML file Starts with and ends with The file is composed of two pieces within this: –Head - to –Body - to Different tags can be in each –These will be considered now, but many more exist than than will be shown

17 Copyright © 1998-2009 Curt Hill HTML Head Tags Page 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 tags which give a variety of auxiliary information like subject, program name, template, etc

18 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 and occurs all the content of the page as well

19 Copyright © 1998-2009 Curt Hill Paired Formatting tags to to bold to italics to underline to monospaced typewriter font to determines font sizes and styles through creates a header

20 Copyright © 1998-2009 Curt Hill Unpaired tags Some HTML generators will pair these as well, but it is not necessary new paragraph a new line produces a horizontal line

21 Copyright © 1998-2009 Curt Hill Java applet tags Default message 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

22 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

23 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

24 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

25 Recall the Java applet tags Default message 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

26 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

27 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

28 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

29 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

30 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

31 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

32

33 AppletShell The web page contains Has the following methods: –init and destroy –start and stop –paint –Delete the ones not used Copyright © 1998-2009 Curt Hill

34 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

35 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

36 Run Configuration Copyright © 1998-2009 Curt Hill

37 Main Tab Copyright © 1998-2009 Curt Hill

38 Parameters Tab Copyright © 1998-2009 Curt Hill


Download ppt "Copyright © 1998-2009 Curt Hill Applets A different type of program."

Similar presentations


Ads by Google