|
Writing HTML | About | FAQ | References | Tags | Lessons | previous | next |29e. Small Cup of Java (to go)
ObjectivesAfter this lesson you will be able to:
LessonThe promise of Java, when introduced by Sun Microsystems in the early 1990s, was huge and some may say, "hyped". In about as brief as we can try to state it, Java is a new programming langauge that allows someone to create applications, or "applets" that can run on any system, any place, that supports the "Java Platform": With JavaTM technology, you can use the same application from any kind of machine -- a PC, a Macintosh computer, a network computer, or even new technologies like Internet screen phones. So a Java applet does not need any extra software to run, when it is requested, all of its functional pieces are sent over a network to whomever/whatever requested it, and then the Java runtime technology on the receiving end then runs it. For more about Java, see http://java.sun.com/
As we mentioned in lesson 27, despite the name similarity, Javascript-- a browser scripting language, is quite different from Java, a computer programming language. JavaScript is literally interpreted line by line as the browser accesses it. Java applets are written as line computer code which is then "compiled" or converted to a run-time application. A compiled application can be more powerful, secure, and faster than interpreted script, but is also more complex to create. Unfortunately, beyond a number of well-integrated uses of Java, the most common use you will find on the web is a Java applet to display a scrolling banner of text, which may roll horizontally or vertically (see lesson 17 for our thoughts on this!), or the common effect of adding ripples to an image.
Like we saw in our previous lesson on Shockwave, the Java files tend to be closed black boxes that we do not even worry about how they work. With some applets we can alter or control their functions through information we send the applet from our HTML code. This lesson will not cover the creation aspects of Java programing, but will show you how to add an existing applet to your web site.
The simple HTML code for including a Java applet is <APPLET CODE=MyApplet.class WIDTH=XX HEIGHT=YY> </APPLET> where MyApplet.class is the file name of a compiled Java applet file, and WIDTH and HEIGHT are the pixel dimensions that the applet occupies on screen. Quite often, you may include other information that are sent to the applet via parameters: <APPLET CODE=MyApplet.class WIDTH=XX HEIGHT=YY> <param name="param1" value=my1Value> <param name="param2" value=my2Value> </APPLET> where each parameter has a name the applet is looking for and some value, which may be a string of text or a number: <APPLET CODE=MyApplet.class WIDTH=XX HEIGHT=YY> <param name="param1" value="The Meaning of Life is Cheese; <param name="param2" value=129; </APPLET> Finally, you can include text that will be displayed only if the web browser does not support Java (remember, the browser will ignore anything inside tags it does not understand), similar to using the ALT option for <img ...> tags (lesson 7a) or the <NOFRAMES> tag for frames (lesson 26). <APPLET CODE=MyApplet.class WIDTH=XX HEIGHT=YY> <param name="param1" value=my1Value> <param name="param2" value=my2Value> Sorry, but it looks as thought your web browser cannot display this cool Java applet. </APPLET> So any string of text inside the <APPLET>...</APPLET> tags is ignored by a Java-enabled browser since it is not written as am applet tag or a parameter tag) and is the only portion displayed for a Java-disabled web browser. Adding JavaNote: If you do not have the working documents from the previous lessons, download them now. In this lesson we are going to use a Java applet that allows us to send it an image file, and in our web page, allow the viewer to zoom in and out. The pictures we are going to use are of volcanic rocks taken with a special microscope that allows us to see the minerals and structures in the rock. In our web page, we can use the Java applet to act like a virtual microscope. The Java applet used here is called "ImageZoom" and more information is available from http://www.vivaorange.com/ImageZoom/. This applet is free for non-commercial use (we found it from using one of the Java resource sites listed below). The general HTML for using this applet is: <applet code="ImageZoom.class" width="[width]" height="[height]"> <param name="IMAGE" value="[image file]"> <param name="ZoomLevel" value="[zoom level]"> <param name="PanSpeed" value="[speed]"> <param name="Cursor" value="[cursor]"> <param name="Preload" value="[preload]"> </applet> where:
The applet file, ImageZoom.class itself is only 5k in size, and the image it uses is read in as a parameter, as well as the other options listed above. Check the source web site for more information about this applet.
NOTE: If your web page does not work as expected, compare your HTML to the source of the example page. We now have seen how to load the ImageZoom applet with a defined image so you can zoom and pan as if you were using a microscope (actually the image would be much more clear at high magnifications on a real microscope; on the computer we are simply enlarging it and we start to see the artifacts of square image pixels). But we can do something more flexible by using some JavaScript to dynamically create the HTML for the applet, allowing us to choose from a series of images to load in the applet. For more on this aspect of JavaScript, review lesson 27b. To set this up, we are going to create a framed web page (see lesson 26) that will look like:
<html> <head> <script language="JavaScript"> <!-- function scope( rockmenu ) { // Called from menu to either load static content into the top frame // or to dynamically write code for embedding a Java applet // rock identifies the file name, blurb is the caption rock = rockmenu[rockmenu.selectedIndex].value; blurb = rockmenu[rockmenu.selectedIndex].text; if (rock != "") { // ignore blank menu values and reselect the first menu item rockmenu.selectedIndex = 0; if (rock== "help") { // selected help, load the opening page parent.frames[0].location.href="stage.html"; rockmenu.selectedIndex = 0; } else if (rock== "close") { // call function to close the microscope close_scope(); rockmenu.selectedIndex = 0; } else { // load applet with selected image with (parent.frames[0]) { document.write('<html><head><title>' + blurb + '</title></head>'); document.write('<body bgcolor="#000000" text="#EEEEEE">'); document.write('<center><applet code="ImageZoom.class"'); document.write(' width=400 height=265 vspace=14>'); document.write('<param name="IMAGE" value="'+ rock +'.jpg">'); document.write('<param name="ZoomLevel" value="6">'); document.write('<param name="PanSpeed" value="4">'); document.write('<param name="cursor" value="1">'); document.write('<param name="Preload" value="on">' ); document.write('Sorry, but your web browser cannot load this Java Applet :-('); document.write('<p>Here at least is a picture of the sample:<br>'); document.write('<img src="'+ rock +'.jpg" width=400 height=265><p>'); document.write('</applet>'); document.write('<font face="verdana,helvetica" size="2">'); document.write('<br>BISHOP TUFF: ' + blurb); document.write('click to zoom, move mouse to edge to pan</center>'); document.write('</body></html>'); document.close(); } } } } function close_scope() { // provide a confirmation dialog box before closing the window if ( confirm( "Are you sure that you want to close the microscope?" ) ) { parent.close(); } } //--> </script> </head> <body bgcolor=#333333 text=#FFFFFF link="#CCFFFF" vlink="#FFCC99"> <center> <form> <font face="verdana,helvetica" size=1>java microscope viewer</font><br> <select name="rock" onChange="scope(this)"> <option value="">Select a sample... <option value="dw_vis">Densely Welded Bishop Tuff (visible light) <option value="dw_pol">Densely Welded Bishop Tuff (polarized light) <option value="pw_vis">Partly Welded Bishop Tuff (visible light) <option value="pw_pol">Partly Welded Bishop Tuff (polarized light) <option value="">------------------ <option value="close">Close Microscope <option value="help">Help </select> </form> </center> </body> </html> NOTE: We have quite a bit of code here! The menu created in the body of the document sends a message each time it is changed, sending a reference to the menu. The Javascript function looks at the value of the selected item. If it is empty, then we do nothing. If it is "help", we simply load the first page into the top frame. If it is "close" we call a JavaScript function that first displays a confirmation dialog box, and if the viewer click OK, it closes the entire window. Once our java page is working, we need to add a link that will open it in its own browser window, like we have done in the previous multimedia lessons.
Check Your WorkCompare your web pages with this sample of how it should appear. If your pages are different from the sample or the hypertext links do not work correctly, review the text you entered in the text editor. Compare your work to the HTML of the samples (look for something like Source from your browser's View menu). ReviewReview topics for this lesson:
Independent PracticeExperiment with the ImageZoom applet using one of your own images. Try to see how it works if you adjust some of the parameters. More InformationAlthough Java may be a high-level computer programming language, you can find many web sites that provide free applets that you can use in your own pages.
For more Java resources, see our small, but high quality, reference list of Java tutorials. Also, see the CNET Java Center as well as Sun's main Java site. Coming Next....Watch out as we completely revamp everything to take advantage of the next generation of web page design... let's rev up to HTML 4.0! GO TO.... | Lesson Index | previous: "Shockwave" | next: "HTML4.0" |
Writing HTML: Lesson 29e: Small Cup of Java (to go)
|