Chapter 18 - Applets and Multimedia Flashcards

1
Q

What is the ->main<- difference between a Java application and a Java applet?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you convert a Java application into an Applet?

A

By replacing JFrame with JApplet and deleting the main method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you make an applet run from an HTML file?

A

You must put the applet code inside tags in the HTML document, like this:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an HTML tag, and what are tag attributes?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What attributes are required in an applet tag?

A

The code, width, and height attributes are required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the ‘codebase’ attribute of applet tags do? (codebase = applet_url)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the ‘archive’ (archive = archivefile) attribute of applet tags do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the ‘vspace’ and ‘hspace’ (vspace = vertical_margin, hspace = horizontal_margin) attributes of applet tags do?

A

vspace and hspace specify the size, in pixels, of the blank margin to pad around the applet vertically and horizontally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the ‘align’ (align = applet_alignment) attribute of applet tags do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the ‘alt’ (alt = alternative_text) attribute of applet tags do?

A

alt specifies the text to be displayed in case the browser cannot run Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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?

A
  1. 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.
  2. 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.
  3. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Let’s say you had an applet named MyApplet. How could you convert it into an application?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you add components to a JApplet? What is the default layout manager of JApplet?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the “applet life-cycle methods”?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the applet life-cycle by the life-cycle methods

A
  1. Loaded. The applet container (in the browser) loads the applet.
  2. Created. The applet container creates the applet.
  3. Initialized. The applet container invokes init().
  4. Started. The applet container invokes start().
  5. Stopped. The applet container invokes stopped().

The applet container may start and stop the applet in a loop.

  1. Destroyed. After the applet is stopped, the applet container can invoke destroy().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe the init method of the Applet class.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Describe the start method of the Applet class.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Describe the stop method of the Applet class.

A

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.

19
Q

Describe the destroy method of the Applet class.

A

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.

20
Q

You can pass string parameters from an HTML file to an applet. What is the syntax of parameters?

A

Parameters are defined using the tag. The tag must be embedded in the tag. Its syntax is:

Example:

21
Q

In an applet, how do you get parameter values from the HTML file?

A

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”));

22
Q

Can Applet’s getParameter method be invoked only after an instance of the applet is created?

A

Yes. Therefore, getParameter cannot be invoked in the constructor of the applet class. You should invoke it from the init method.

23
Q

How do you create a URL object for the file image/us.gif in the class directory?

A

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.

24
Q
Which of these audio formats can a Java program play?
A. WAV
B. AIFF
C. MP3
D. MIDI
E. AU
F. RMF
A

All but C. MP3

25
Q

How do you create an audio clip in Java?

A

To create an audio clip, use the static method newAudioClip() in the java.applet.Applet class, like this:

AudioClip audioClip = Applet.newAudioClip(url);

26
Q

Can you play audio in applets? Can you play audio in applications?

A

Audio originally could be played only from Java applets. For this reason, the AudioClip interface is in the java.applet package. Since JDK 1.2, audio can be played in any Java program.

27
Q

How do you create an AudioClip for the beep.au audio file in the class directory?

A
Class metaObject = this.getClass();
URL url = metaObject.getResource("beep.au");
AudioClip audioClip = Applet.newAudioClip(url);
28
Q

What are the three methods in java.applet.AudioClip, and what do they do?

A

play() Starts playing this audio clip. Each time this method is called, the clip is restarted from the beginning.
loop() Plays the clip repeatedly.
stop() Stops playing the clip.

29
Q

If your applet does not have the init() method, which of the following will happen?

A. Your program will not compile.
B. Your program will compile, but not execute.
C. You must have a main method.
D. Your program will run just fine since the init() method is defined in the Applet class.

A

The correct answer is

D. Your program will run just fine since the init() method is defined in the Applet class.

30
Q

When you run an applet, which of the following is invoked first?

A. The init method.
B. The start method.
C. The stop method. 
D. The destroy method.
E. The applet's default constructor.
A

The correct answer is E
Explanation: When the applet is loaded to the Web browser, the Web browser creates an instance of the applet by invoking the applet’s default constructor.

31
Q

Which of the following is true?

A. The Applet class is in javax.swing package.
B. The JApplet class is in javax.swing package.
C. Applet is a subclass of JApplet.
D. JApplet is a subclass of Applet.
A

B and D are true

32
Q
The default layout of the contentPane of a JApplet is \_\_\_
A. FlowLayout
B. GridLayout
C. BorderLayout
D. None
A

C. BorderLayout

33
Q

To use an applet in the HTML document, you use ____ in the tag.

A. .java source code file
B. .class bytecode
C. .exe executable file
D. .html file

A

B. .class bytecode

34
Q

Analyze the following code.

import javax.swing.*;

public class Test extends JApplet {
  public void init() {
    add(new JLabel("OK"));
  }
  public static void main(String[] args) {
    Test applet = new Test();
  }
}

A. When you run the applet from a browser, the OK label is displayed.
B. When you run the applet standalone from the main method, nothing is displayed.
C. When you run the applet standalone from the main method, the OK label is not displayed because the init() was not invoked.
D. All of the above.

A

D. All of the above.

35
Q

Which of the following statements are true?

A. If an applet has a main method, it can be executed standalone.
B. An applet is a GUI component and it can be added to a container.
C. When an applet runs from a browser, its main method (if exists) is not invoked by the browser.
D. When an applet runs from a browser, its init method is not invoked after its no-arg constructor is invoked.

A

All but D are true

36
Q

If you try to retrieve an HTML parameter that is not defined in the HTML page, what will happen?

A. You will get a compile error.
B. You will get a runtime error.
C. Your program will run with the parameter’s value null.
D. Your program will run with an empty string in the parameter’s value.

A

The correct answer is C

37
Q

Suppose you pass parameter named message from HTML to the following applet, analyze the code:

import javax.swing.*;

public class Test extends JApplet {
  public Test() {
    String message = getParameter("MESSAGE");
    add(new JLabel(message));
  }
}

A. The program has a compile error because parameter names are case-sensitive. You should replace MESSAGE with message.
B. The program has a compile error because you cannot invoke getParameter(“MESSAGE”) from the constructor.
C. The program has a runtime error because you cannot invoke getParameter(“MESSAGE”) from the constructor.
D. The program runs fine and displays the label with the message passed from the HTML file.

A

The correct answer is C

Explanation: The getParameter(‘MESSAGE’) method must be invoked from the init() method.

38
Q

Which of the following statements are true?

A. You can create a URL object for any public accessible resource on the Internet.
B. You can create a URL object for a local file.
C. A URL object may be created using the new URL(urlString) constructor.
D. A URL object may be returned using the getSource method on a meta object (instance of java.lang.Class).

A

All are true

39
Q

Which of the following statements are true?

A. Classes are dynamically loaded when needed.
B. When a class (e.g., java.lang.Math) is loaded, the JVM creates a meta-object for the class.
C. Two objects of the same class share the same meta-object.
D. A class will be loaded only once in an application or applet.
A

All are true

40
Q

To get an ImageIcon for the a specified URL, you use ____

A. getImage(url)
B. createImage(url)
C. url.getImage()
D. url.createImage()
E. new ImageIcon(url);
A

E. new ImageIcon(url);

41
Q

The ImageViewer class was presented in Chapter 17. You can use the method _________ to create an Image from a file “image/us.gif” in the same directory of the this class.

A. ImageViewer.createImage(“image/us.gif”, this)
B. ImageViewer.createImageIcon(“image/us.gif”, this)
C. ImageViewer.createImage(“image/us.gif”, null)
D. ImageViewer.createImageIcon(“image/us.gif”, null)

A

A. ImageViewer.createImage(“image/us.gif”, this)

42
Q

The ImageViewer class was presented in Chapter 17. Which of the following statements are true?

A. ImageViewer is a subclass of JPanel.
B. You can display an image in an ImageViewer.
C. You can specify where an image is displayed in an ImageViewer.
D. You can specify whether an image can be stretched in an ImageViewer.

A

All are true

43
Q

When creating an URL object for a resource in the classpath (“filename.ext”), where exactly is the resource supposed to be?

A

In the bin folder! workspace/package/bin