Using the Object Tag

The newest version of HTML, HTML5, has replaced the <applet> tag with an <object> tag for loading Java applets, Flash programs, and other forms of interactive content. This tag has height and width attributes just like <applet>. There’s also a type attribute that must be “application/x-java-applet”, the designated MIME type of Java applets. (MIME types categorize file formats that can be delivered over the Internet.) Here’s the start to the formatting of an object:

<object type="application/x-java-applet" height="300" width="400">
</object>

The code and codebase of an applet are not designated as attributes. Instead, parameters named code and codebase are placed within the opening <object> tag and closing </object> tag.

The following HTML5 markup displays an applet:

<object type="application/x-java-applet" height="300" width="400">
  <param name="code" value="StripYahtzee" />
  <param name="codebase" value="javadir" />
  <p>Sorry, no dice ... this requires a Java-enabled browser.</p>
</object>

Summary

Most of the hours in this book focus on applications, primarily because most Java programmers today don’t do a lot of work designing applets for the Web.

Applets are limited by a set of default security restrictions that make them safe to be executed on user computers in a web browser. They can’t save files to the computer, read files from the computer, list file folders, or create pop-up windows that are not identified as Java applets, among other safeguards.

These restrictions can be overcome by signing an applet with a digital signature and asking a user to approve the applet. An alternative to deploying Java programs as applets is to use Java Web Start, a technology for launching Java applications from a web browser.

Q&A

Q. Is there a reason why the codebase attribute should be used in an applet tag?

A. If all Java programs are grouped into their own subfolder using codebase, this structure might improve the way a website is organized, but there’s no other reason why using codebase is better than omitting it. The choice is a matter of personal preference.

Q. Why don’t applets have a main() method?

A. Applets don’t use main() because they have a more complicated life cycle than applications. An application starts, runs until its work is complete, and exits. An applet can be started and stopped multiple times in a browser as the page on which it is contained is displayed.

If a user uses the back button to leave the page and then the forwards button to return, the applet’s start() method is called again. If a pop-up window that obscures the applet is closed, the applet’s paint() method is called.

The JApplet class was designed to make these more complex interactions work inside a browser.

Q. Have the Washington Generals ever beaten the Harlem Globetrotters?

A. The Generals have beaten the Globetrotters seven times over the decades, most recently in 1971. Playing in Martin, Tennessee, the Generals won 100–99 on a shot by team owner Louis “Red” Klotz.

Although the Generals are known today for being patsies, they began in 1921 as the Philadelphia Sphas, a legitimate team that played in the Eastern and American basketball leagues. The Sphas—an acronym for South Philadelphia Hebrew Association—won 10 championships. Klotz was a former Sphas player who bought the team and changed their name to the Generals in 1952 when they became the permanent touring partner of Harlem’s famous team.

The 1971 victory ended a 2,495-game winning streak for the Globetrotters.

Workshop

The following questions test your knowledge of applets.

Quiz

1. What type of argument is used with the paint() method?

A. A Graphics object

B. A Graphics2D object

C. None

2. Which method is handled right before an applet finishes running?

A. decline()

B. destroy()

C. defenestrate()

3. Why can’t all variables needed in an applet be created inside the init() method?

A. The scope of the variables would be limited to the method only.

B. Federal legislation prohibits it.

C. They can be created there without any problems.

Answers

1. A. The Graphics object keeps track of the behavior and attributes needed to display things on-screen in the applet window. You might create a Graphics2D object inside the method, but it isn’t sent as an argument.

2. B. The destroy() method can be used to free up resources used by the applet.

3. A. Variables that are used in more than one method of a class should be created right after the class statement but before any methods begin.

Activities

You can apply your applet programming knowledge with the following activities:

• Write an applet in which the text that is displayed moves each time the applet window is repainted.

• Install the Java Plug-in with your preferred browser and try the applets at www.javaonthebrain.com.

To see Java programs that implement these activities, visit the book’s website at www.java24hours.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.7.151