COP2800 : Java Programming Chapter 6

Applet

Section A: HTML and Applet Basic

(q stands for end of chapter questions)

Applets are programs that are called from within another application. You run applets within a Web page, or within another program called appletviewer, which comes with the Java Developer's Kit.
q1- Applets are called from within another application.
(PG 269)
q2- Appletviewer comes with the Java Developer's Kit.
(PG 269)

An applet must be called from within an HTML (Hypertext Markup Language) document.
q3- HTML stands for HyperText Markup Language.
(PG 269)

A Web browser is a program that allows you to display HTML documents on your computer screen; such documents often contain Java applets.
q4- When you write a Java applet, you save the code with the .java file extension.
(PG 269)
q5- Java applications and Java applets are simular because both are compiled using the javac command.
(PG 269)
q6- To use an applet within an HTML document, you include the name of the .class complied file.
(PG 269)
q7- A program that allows you to display HTML documents on your computer screen is a browser.
(PG 269)

HTML commands are called tags. Tags usually come in pairs. The tag that begins every HTML document is <HTML> and the tag that ends every HTML document is </HTML>.
q8- HTML commands also are called tags.
(PG 270)

To run an applet from within an HTML document, you add the <APPLET> and </APPLET> tags to your HTML document.
q9- All HTML commands are surrounded by angle brackets.
(PG 270)
q10- The ending half of any HTML tag pair is preceded with a backslash.
(PG 270)

You can place 3 attributes within the <APPLET> tag: CODE, WIDTH, and HEIGHT.
q11-The name of any applet called using CODE within an HTML document must use the .class extension.
(PG 270)
q12- Usually, you should create your applets to run on a monitor that measures 640 X 480 pixels so most users can see the entire applet.
(PG 270)

Most applets contain at least 2 import statements; import java.applet.*; and import java.awt.*; The java.awt package is the Abstract Windows Toolkit, or AWT. It contains commonly used Windows components such as Labels, Menus, and Buttons.
q13- Labels and Buttons are components.
(PG 271)

Label is a built-in class that holds text that can be displayed within an applet. The setText() method assigns text to a Label or any other component.
q14- The method that places a value within a previously constructed Label is setText().
(PG 272)

You use the add() method to add a component to an Applet Viewer window.
q15- The add() method places a component within an Applet Viewer Window..
(PG 272)

Applet class headers include the words extends Applet.

Four methods that are included in every applet are public void init(), public void start(), public void stop(), public void destroy().
q16- The method that might Not be included in Every applets is: add().
(PG 272)

The init() method is the 1st method called in any applet. You use this method to perform initialization tasks, such as setting variables to initial values or placing applet components on the screen.
q17- The 1st method called in any applet is: init().
(PG 272)

A Font object holds typeface and size information. To construct a Font object, you need 3 arguments: typeface, style, and point size. The typeface is a String representing a font. Common fonts are Arial, Helvetica, Courier, and TimesRoman. The style applies an attribute to displayed text and is 1 of 3 arguments: font.PLAIN, font.BOLD, or font.ITALIC. The point size is an integer that represents 1/72 of an inch.
q18- A Font object contain the fallowing arguments: typeface, style, and point size.
(PG 272)

To give a Label object a new Font, you create the Font and then use the setFont() method to assign the Font to a Label.

A TextField is a Windows component into which a user can type a single line of text data. Typically, a user types a line into a TextField and then inputs the data by pressing the Enter key on the keyboard or clicking a Button with the mouse.
q19- A Windows component into which a user can type a single line of text data is a TextField.
(PG 272).

You can create a TextField with or without initial text, and with or without a specified size.

The setText() method allows you to change the text in a TextField that has already been created. The getText() method allows you to retrieve the String of text in a TextField.

You can create a Button with or without a label. You can change a Button's Label with the setLabel() method, or get the Label and assign it to a String object with the getLabel() method.
q20- The constructor public Button("4") creates a Button with a "4" on it.
Q21- You can change a Button's label using the setLabel() method.
(PG 278)

An event occurs when your applet's user takes action on a component, such as using the mouse to click a Button object. In event-driven programs, the user might initiate any number of events in any order.
q22- A user might initiate any number of events in any order in an event-driven program.
(PG 279)

Within an event-driven program, a component on which an event is generated is the source of event. An object that is interested in an event is a listener.

To respond to user events within any applet you create, you must prepare your applet to accept event messages, tell your applet to expect events to happen, and then tell your applet how to respond to any events that happen.

Adding implements ActionListener to an applet's class header prepares an applet to receive event messages.

An interface is a set of specifications for methods that you can use with events.
q23- ActionListener is an example of an interface.
(PG 280)

An ActionEvent is a type of event that occurs when a user clicks a Button. You tell your applet to expect ActionEvents with the addActionListener() method. The ActionListener interface containg the actionPerformed(ActionEvent e) method specification. In the body of the method, you write any statements that you want to execute when an action takes place.
q24- When an applet is registered as a listener with a Button, if a user clicks the Button, the method that executes is: actionPerformed().
(PG 281 top)

You can alert the applet when it is out of date by using the invalidate() method, which marks the window as not up to date with recent changes. Using the validate() method redraws any invalid window.
q25- You alert an applet when it is out of date by using the: invalidate() method.
(PG 284)

Back to the Top


Section B The Applet Life Cycle and More Sophisticated Applets

When you write a method that has the same method header as an automatically provided method, you replace or override the original version.
q1- When you write a method that has the same method header as an automatically provided method, you override the original version.
(PG 291)
q2- If you do not write an init() method for an applet, then an automatically provided init() method executes.
(PG 291)

The start() method executes after the init() method and every time the applet becomes active after it has been inactive. You write your own start() method if there are any actions you want to take when an applet is revisited.
q3- The method that executes immediately after init() is start().
(PG 291)
q4- The start() method executes every time the applet becomes active after it has been inactive.
(PG 291)

When a user moves off the page, perhaps by minimizing a window or traveling to a different Web page, the stop() method is invoked.
q5- The method that executes when a user leaves a page is stop().
(PG 292)

The destroy() method is called when the user closes the browser or Applet Viewer; this releases any resources the applet may have allocated.
q6- The destroy() method is called when the user closes the browser or Applet Viewer window.
(PG 292)
q7- The stop()-and-start() sequence might occur any number of times within an applet.
(PG 292)

When you use the add() method to add a component to an applet, you do not determine the physical location of the component. The setLocation() method allows you to place a component at a specific location within an Applet Viewer window.
q10- The method that positions a component within an applet is setLocation().
(PG 302)

Any Applet Viewer window consists of a number of horizontal and vertical pixels on the screen, called the x-axis positions and y-axis positions, respectively.
q11- The y-axis position within a window refers to the vertical position.
(PG 302)

When you use setLocation(x,y), the top-left corner of the component is placed at the specified x- and y- coordinates.

You can use the setEnabled() method with a component to make it unavailable and , in turn, to make it available again. The setEnable() method takes an argument of true if you want to enable a component, or false if you want to disable a component.
q15- You use the setEnable() method to make a component available or unavailable.
(PG 304)

When you are writing Java programs, you can get help from the Sun Microsystems Web site of from Java newsgroups.

Back to the Top


Links to COP2800 Java Programming Chapters:
Links to the java.sun.com to help with Java Programming

Back to the Top