Javanotes 5.1.2, Answers For Quiz On Chapter 6
Javanotes 5.1.2, Answers For Quiz On Chapter 6
Question 1: Programs written for a graphical user interface have to deal with
"events." Explain what is meant by the term event. Give at least two
different examples of events, and discuss how a program might respond
to those events.
Answer: An event is anything that can occur asynchronously, not under the
control of the program, to which the program might want to respond.
GUI programs are said to be "event-driven" because for the most part,
such programs simply wait for events and respond to them when they
occur. In many (but not all) cases, an event is the result of a user
action, such as when the user clicks the mouse button, types a
character, or clicks a button. The program might respond to a mouse-
click on a canvas by drawing a shape, to a typed character by adding
the character to an input box, or to a click on a button by clearing a
drawing. More generally, a programmer can set up any desired response
to an event by writing an event-handling routine for that event.
Answer: The repaint() method of a component is called to notify the system that
the component needs to be redrawn. It does not itself do any drawing
(neither directly nor by calling the paintComponent() routine). You should
call repaint() when you have made some change to the state of the
applet that requires its appearance to change. Sometime shortly after
you call it, the system will call the component's paintComponent()
routine.
Question 4: Java has a standard class called JPanel. Discuss two ways in which
JPanels can be used.
Answer: The outer loop is executed for values of i equal to 10, 60, 110, 160, and
210. For each of these values, the inner loop is executed for j equal to
10, 60, 110, 160, and 210. The drawLine command is therefore
executed 25 times -- and so, 25 different lines are drawn. These lines
connect the five points (10,10), (60,10), (110,10), (160,10), and
(210,10) to the five points (10,60), (60,60), (110,60), (160,60), and
(210,60) in all possible pairings. Here is the picture:
Question 6: Suppose you would like a panel that displays a green square inside a red
circle, as illustrated. Write a paintComponent() method for the panel
class that will draw the image.
Answer: (The size of the square and circle are not specified in the problem, so
any size would be acceptable, as long as the square is in the middle of
the circle. Notice that the drawing commands are fillOval and
fillRect . There are no special routines for drawing circles or squares.)
Question 7: Java has a standard class called MouseEvent . What is the purpose of this
class? What does an object of type MouseEvent do?
Answer: When an event occurs, the system packages information about the
event into an object. That object is passed as a parameter to the event-
handling routine. Different types of events are represented by different
classes of objects. An object of type MouseEvent represents a mouse or
mouse motion event. It contains information about the location of the
mouse cursor and any modifier keys that the user is holding down. This
information can be obtained by calling the instance methods of the
object. For example, if evt is a MouseEvent object, then evt.getX() is the
x-coordinate of the mouse cursor, and evt.isShiftDown() is a boolean
value that tells you whether the user was holding down the Shift key.
Question 8: One of the main classes in Swing is the JComponent class. What is
meant by a component? What are some examples?
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 10: What type of layout manager is being used for each of the three panels
in the following illustration from Section 6.7?
Answer: The main panel, shown in blue, seems to be using a GridLayout with two
rows and one column. A GridLayout is most likely since the two
components in the main panel -- the other two panels -- are exactly the
same size. Similarly, the bottom subpanel, shown in green, seems to be
using a GridLayout with one row and three columns. The top subpanel,
shown in red, could be using a BorderLayout . The components on the
left and right ends of the subpanel would be in the WEST and EAST
positions of the BorderLayout . Each of these components would then be
shown at its own preferred width, which would explain how their widths
could be different. The third component, in the center of the subpanel,
would then be in the CENTER position.
Answer: A JCheckBox is a component that has two possible states, "selected" and
"not selected". The user can change the state by clicking on the
JCheckBox . If box is a variable of type JCheckBox , then a program can
set the state of the box to "selected" by calling box.setSelected(true)
and can unselect 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.