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. This information could be used, for example, for positioning the string is a component.


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

Answer: In Java, an off-screen image is an object belonging to the class Image and created with the createImage() function. An off-screen image is a segment of the computer's memory that can be used as a drawing surface. What is drawn to the off-screen image 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 image 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. By default, Swing already uses an off-screen image for double-buffering an applet or frame, so you don't have to program it yourself just to do simple animation.


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

Answer: A JComponent represents a visual component of the computer's graphical user interface. A JComponent is not completely independent. It must be added to a "container," such as an applet or a frame. Examples of JComponents are JButtons, JTextFields, and JPanels.


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 a JPanel or the content pane of a JApplet. 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 should 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 JCheckBox and how is it used?

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


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. In Java, a thread is represented by an object of type Thread. A Thread object has a run() method to execute (usually the run() method of a Runnable object that is provided when the Thread is constructed). The thread 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: Explain how Timers are used to do animation.

Answer: Displaying an animation requires showing a sequence of frames. The frames are shown one after the other, with a short delay between each frame and the next. A Timer can generate a sequence of ActionEvents. When a timer is used to do animation, each event triggers the display of another frame. The ActionListener that processes events from the timer just needs to be programmed to display a frame when its actionPerformed() method is called.


Question 9: Menus can contain sub-menus. What does this mean, and how are sub-menus handled in Java?

Answer: Menus can be "hierarchical." A menu can contain other menus, which are called sub-menus. A sub-menu is represented as a single item in the menu that contains it. When the user selects this item, the full sub-menu appears, and the user can select an item from the sub-menu. In Java, a sub-menu is no different from any other menu. A menu can be added to a menu bar, or it can be added to another menu. In the latter case, it becomes a sub-menu.


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

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


[ Chapter Index | Main Index ]