Sample Quiz Answers
For Chapter 7


THIS PAGE CONTAINS SAMPLE ANSWERS to the Quiz on Chapter 7 of this on-line Java textbook. Note that in many cases, there are lots of correct answers to a given question.


Question 1: What is the FontMetrics class used for?

Answer: An object that belongs to the class FontMetrics can be used to obtain information about the sizes of characters and strings that are drawn in a specific font. The font is specified when the FontMetrics object is created. If fm is a variable of type FontMetrics, then, for example, fm.stringWidth(str) gives the width of the string str and fm.getHeight() is the usual amount of vertical space allowed for one line of text.


Question 2: An off-screen canvas can be used to do double buffering. Explain this. (What are off-screen canvases? How are they used? Why are they important? What does this have to do with animation?)

Answer: In Java, an off-screen canvas is an object belonging to the class Image and created with the createImage() function. An off-screen canvas is a segment of the computer's memory that can be used as a drawing surface. What is drawn to the off-screen canvas is not visible on the screen, but the Image can be quickly copied onto the screen with a drawImage() command. It is important to use an off-screen canvas in a situation where the process of drawing the image should not be seen by the user. This is true, for example, in animation. Each frame of the animation can be composed in an off-screen Image and then copied to the screen when it is complete. The alternative would be to erase the screen and draw the next frame directly on the screen. This causes unacceptable flickering of the image.


Question 3: One of the main classes in the AWT is the Component class. What is meant by a component? What are some examples?

Answer: A Component represents a visual component of the computer's graphical user interface. Components can be added to "containers," such as applets and frames. Examples of Components are Buttons, TextFields, and Canvases. The Component class is abstract, which means that any actual component objects belong to subclasses of Component.


Question 4: What is the function of a LayoutManager in Java?

Answer: A LayoutManager implements some policy for laying out all the visual components that have been added to a container, such as an applet or panel. That is, it sets the sizes and positions of the components. Different types of layout managers have different rules about how components are to be arranged. Some standard layout manager classes are BorderLayout and GridLayout.


Question 5: What does it mean to use a null layout manager, and why would you want to do so?

Answer: If the layout manager for a container is set to null, then the programmer takes full responsibility for setting the sizes and positions of all of the components in that container. This gives the programmer more control over the layout, but the programmer has to do more work. For simple layouts in a container that does not change size, the setBounds() method of each component can be called when it is added to the container. If the container can change size, then the sizes and positions have to be recomputed when a size change occurs. This is done automatically by a layout manager, and this is one good reason to use a layout manager for a container that can change size.


Question 6: What is a Checkbox and how is it used?

Answer: A Checkbox is a component that has two possible states, "checked" and "unchecked". The user can change the state by clicking on the Checkbox. If box is a variable of type Checkbox, then a program can check the box by calling box.setState(true) and can uncheck the box by calling box.setState(false). The current state can be determined by calling box.getState(), which is a boolean-valued function. A Checkbox generates an event of type ItemEvent when it changes state. A program can listen for these events if it wants to take some action at the time the state changes.


Question 7: What is a thread?

Answer: A thread, like a program, executes a sequence of instructions from beginning to end. Several threads can execute "in parallel" at the same time. It is possible for threads to communicate by sharing variables. In Java, a thread is represented by an object of type Thread. A Thread object has a run() method to execute. It begins executing the run() routine when its start() method is called. At the same time, the rest of the program continues to execute in parallel with the thread.


Question 8: If you want to program an applet to show an animation, you have to create a thread. Why?

Answer: Unless it starts a thread, an applet can only respond to events that occur outside its control. We don't want the user to have to click on the applet to show each new frame of the animation! To do an animation, the applet can create a thread to show the frames continually, one after another.


Question 9: What is meant by a non-static nested class? What are the advantages of using one?

Answer: In Java, one class can be nested inside another. A nested class can be static or non-static. A non-static nested class is not actually part of the class that contains it. Instead, each object that belongs to the outer class has its own (logical) copy of the non-static nested class. Suppose that Outer is a class and Inner is a non-static nested class inside Outer. An object of type Inner can only be created through an object of type Outer. The Inner object has access to all the instance variables of the Outer object. This allows the two objects to communicate easily. But the objects are still distinct, and each one can have its own separate, well-defined responsibilities.


Question 10: What is the purpose of the Frame class?

Answer: An object belonging to the class Frame is an independent window on the screen. You don't need a Web browser to create a Frame like you do for an Applet. A Frame can be used as a user interface for a stand-alone program. It is also possible for an applet to open a Frame.


[ Chapter Index | Main Index ]