Chapter 18 - Applets and Multimedia Flashcards
What is the ->main<- difference between a Java application and a Java applet?
Every application must have a main method, which is invoked by the Java interpreter. Applets are embedded in web pages, and are controlled by the web browser. Applets don’t need a main method.
How can you convert a Java application into an Applet?
By replacing JFrame with JApplet and deleting the main method
How do you make an applet run from an HTML file?
You must put the applet code inside tags in the HTML document, like this:
What is an HTML tag, and what are tag attributes?
An HTML tag is an instruction to the Web browser. The browser interprets the tag and decides how to display or otherwise treat the subsequent contents of the HTML document.
The first word in a tag, called the tag name, describes tag functions. A tag can have additional attributes, sometimes with values after an equals sign, which further define the tag’s action.
What attributes are required in an applet tag?
The code, width, and height attributes are required.
What does the ‘codebase’ attribute of applet tags do? (codebase = applet_url)
codebase specifies the base from which your classes are loaded. If this attribute is not used, the Web browser loads the applet from the directory in which the HTML page is located. If your applet is located in a different directory from the HTML page, you must specify the ‘applet_url’ for the browser to load this applet.
What does the ‘archive’ (archive = archivefile) attribute of applet tags do?
archive instructs the browser to load an archive file that contains all the class files needed to run the applet. Archiving allows the Web browser to load all the classes from a single compressed file at one time, thus reducing loading time and improving performance.
What does the ‘vspace’ and ‘hspace’ (vspace = vertical_margin, hspace = horizontal_margin) attributes of applet tags do?
vspace and hspace specify the size, in pixels, of the blank margin to pad around the applet vertically and horizontally.
What does the ‘align’ (align = applet_alignment) attribute of applet tags do?
align specifies how the applet will be aligned in the browser. One of nine values is used:
left, right, top, texttop, middle, absmiddle, baseline, bottom, or absbottom.
What does the ‘alt’ (alt = alternative_text) attribute of applet tags do?
alt specifies the text to be displayed in case the browser cannot run Java
Java uses the so-called “sandbox security model” for executing applets to prevent destructive programs from damaging the system on which the browser is running. What activities does the sandbox restrict?
- Applets are not allowed to read from, or write to, the file system of the computer. Otherwise, they could damage the files and spread viruses.
- Applets are not allowed to run programs on the browser’s computer. Otherwise, they might call destructive local programs and damage the local system on the user’s computer.
- Applets are not allowed to establish connections between the user’s computer and any other computer, except for the server where the applets are stored. This restriction prevents the applet from connecting the user’s computer to another computer without the user’s knowledge.
Let’s say you had an applet named MyApplet. How could you convert it into an application?
By creating a new class with a main method, and instantiating MyApplet, adding it to a JFrame, and setting the frame options:
JFrame frame = new JFrame();
MyApplet applet = new MyApplet();
frame.add(applet, BorderLayout.CENTER);
frame. setSize
frame. setLocationRelativeTo
frame. setDefaultCloseOperation
frame. setVisible
How do you add components to a JApplet? What is the default layout manager of JApplet?
JApplet and JFrame are both subclasses of Container. That means all their user-interface components, layout managers, and event-handling features are the same. You add components to a JApplet just as you would to a JFrame. The default layout manager is BorderLayout.
What are the “applet life-cycle methods”?
The browser controls and executes applets using the applet life-cycle methods. The methods are:
init(), start(), stop(), and destroy().
They are implemented with an empty bodyd in the Applet class, so they do nothing by default. You may override them in a subclass of Applet to perform desired operations.
Describe the applet life-cycle by the life-cycle methods
- Loaded. The applet container (in the browser) loads the applet.
- Created. The applet container creates the applet.
- Initialized. The applet container invokes init().
- Started. The applet container invokes start().
- Stopped. The applet container invokes stopped().
The applet container may start and stop the applet in a loop.
- Destroyed. After the applet is stopped, the applet container can invoke destroy().
Describe the init method of the Applet class.
The init method is invoked after the applet is created.The functions usually implemented in this method include getting string parameter values from the tag in the HTML document.
Describe the start method of the Applet class.
The start method is invoked after the init method. It is also called when the user returns to the Web page containing the applet after surfing other pages.
A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might start the timer to resume animation.
Describe the stop method of the Applet class.
The stop method is the opposite of the start method. The start method is called when the user moves back to the page that contains the applet. The stop method is invoked when the user leaves the page.
A subclass of Applet overrides this method if it has any operation to be performed each time the Web page containing the applet is no longer visible. An applet with animation, for example, might stop the timer to pause animation.
Describe the destroy method of the Applet class.
The destroy method is invoked when the browser exits normally to inform the applet that it is no longer needed and should release any resources it has allocated. The stop method is always called before the destroy method.
A subclass of Applet overrides this method if it has any operation to be performed before it is destroyed. Usually, you won’t need to override this method unless you want to release specific resources that the applet created.
You can pass string parameters from an HTML file to an applet. What is the syntax of parameters?
Parameters are defined using the tag. The tag must be embedded in the tag. Its syntax is:
Example:
In an applet, how do you get parameter values from the HTML file?
in the init() method, you use the ‘getParameter(“parameterName”)’ method to get the parameter values.
Example:
public void init() {
String message = getParameter(“MESSAGE”);
int x = Integer.parseInt(getParameter(“X”));
int y = Integer.parseInt(getParameter(“Y”));
Can Applet’s getParameter method be invoked only after an instance of the applet is created?
Yes. Therefore, getParameter cannot be invoked in the constructor of the applet class. You should invoke it from the init method.
How do you create a URL object for the file image/us.gif in the class directory?
Class metaObject = this.getClass();
URL url = metaObject.getResource(“image/us.gif”);
or just:
URL url = getClass().getResource(“image/us.gif”);
url contains the uniform resource locator for the image. You can use url to do several things, like creating an image icon of the image.
Which of these audio formats can a Java program play? A. WAV B. AIFF C. MP3 D. MIDI E. AU F. RMF
All but C. MP3