In this lecture you will complete our applet tutorial and learn about event handling in applets and about including audio files in your applets. Then we will talk about security and Java. We will also look at ActiveX, and lastly, we will look at what is next: Virtual Reality on the Internet.
So far, all the applets you have seen have only displayed output. If an applet is to be interactive in any way, it has to receive and respond to user input. Whenever the user moves the mouse over a running applet, clicks the mouse in the applet, pushes a button, scrolls a list, etc., events get generated. It is up to the writer of the applet to either ignore the event or perform some action. For instance, when the user clicks the mouse button in an applet, a mouseDown event gets genetated. To ignore it, you just do not specify a function called mouseDown in your class. If, on the other hand, you want some action to occur, e.g. changing the background image, then you overwrite the mouseDown method in your class. The mouseDown method takes three parameters: an Event and the (x,y) coordinates of where the mouse was clicked.
Some mouse events are: mouseDown, mouseUp, mouseMove, mouseDrag, mouseEnter, and mouseExit. As an example:
public boolean mouseDown(Event event, int x, int y) { // mouse down at position (x,y) ....... return true; }Events
triggered by mouse activity are not the only possible events. The user could also press a key on the keyboard; this would generate events like keyDown and keyUp. Furthermore, events are also triggered when the applet gets focus or loses focus: gotFocus and lostFocus. Lastly, events get triggered when the user interacts with some Graphical User Interface (GUI) component. This could be clicking on a button in the applet, selecting a choice from a choice menu, scrolling in a list, etc. The most used event that gets triggered for GUI components is: action. The action function takes two parameters: an event and an object:
public boolean action(Event event, Object arg)
In the function, you check what kind of GUI component trigered the event:
if (event.target == choice) { ...... else if (event.target == button) { ...... else { ......
Let's look at an example that draws a simple box by responding to mouseDown, mouseUp, and mouseDrag events: draw box.
Let's extend our DrawBox example by adding a choice menu of colors and changing the drawing color when a new color is selected. The applet reponds to the action event: DrawBox with a choice menu.
Let's look at a little more complicated example. This example scales a portion of an image that is being displayed in the applet. The applet responds to the mouseMove event: magnify.
Let's extend our Magnify applet by removing some of the flickering: Magnify without the flickering.
The following version of the Magnify applet contains even less flickering: Best Magnify Applet.
In Java it is easy to play an audio file with an applet. You simply use the play() method, which is part of the java.applet.Applet class. The play method provides easy access to audio files:
play(URL soundDir, String soundfile); play(URL soundDir);
An example of the use of play would be:
play(getDocumentBase(), "cheers.au");
Security is an important concern, since Java is meant to be used in networked environments. Applets loaded over the network are untrustworthy. Without some assurance of security, you certainly wouldn't want to download an applet from a random site on the net and let it run on your computer. Therefore, Web browsers and applet viewers carefully restrict what an applet is allowed to do.
Since security is so important, a whole page is devoted to it: security
The battle has begun over who will shape the future of the World Wide Web. On one side stands Netscape, creator of the enormously popular Navigator browser, with an impressive array of Internet armor and artillery. On the other side is Microsoft, which is trying to use its leadership role in desktop operating systems to make an online strike with Internet Explorer. The victor will likely determine not only which Web browser you'll use in the future but also what the Web will look like.
Navigator was ahead with support for Sun's Java. Microsoft's answer is ActiveX technology, which produces essentially Internet-enabled OLE custom controls that provide in-place, interactive viewing and editing. It is another way of inserting life into the Web, which is widely seen as being far too static. Like Java, Active Controls are little programs that add functionality to Web pages. Controls can be written to add new features like dynamic charts, animation or audio. And those controls don't necessarily require the developer to learn new tricks; the controls can be written using mature development systems like Visual C++, Visual Basic and Borland's Delphi. Internet Studio, the high-end publishing system that Microsoft is working on, will also facilitate the development of interactive Web pages using Active Controls.
What is Active X?
ActiveX is not really Microsoft's answer to Java, because Microsoft is actually positioning Active Controls as a platform that can accommodate many languages, including Java. Furthermore, Microsoft is developing Jakarta, which can write Java applications, applets and COM controls.
Jakarta is Microsoft's long-awaited Java implementation, and provides a rich visual programming environment that's modeled after Visual C++, replete with wizards and a class library that make it possible to build powerful Java applets in no time.
Perhaps the most interesting thing about Jakarta is that it will include pre-defined collections of Java classes, known as packages, that Microsoft developed in order to make it possible for Java programs to access the functionality of ActiveX controls. At the same time, Java applets themselves can become ActiveX controls. This is an extremely powerful feature that will give Jakarta programmers the ability to take advantage of the functionality offered by both Java and the Win32 interface.
For more information, refer to Datamation magazine.
The human eye is extremely adept at pattern recognition - a capability that generally has
been underexploited in data presentation. But this is about to change with the next
generation of data visualization tools, the most cognizant yet of the sophistication of
human perception:
Have fun with your last Java lab: Lab 3.