IFSC3340 Exam 2 Name_______________________________
1. Give three examples that the author would characterize as Component Bloopers and for each specify what is bad about the usage and what is the proper or good way to deal with the specified situation. (9 pts.)
2. What evidence exists that one layout is better than another? Can it be demonstrated that having a space or a divider makes any difference in the operation of the software? Are layout and appearance bloopers really rather insignificant mistakes that only a purist need be concerned about? (9 pts.)
3. What will your remember from studying Johnson after the course is over? Are the bloopers that you presented easier to understand and remember than those that other students presented? Would it be better if I did the presentations instead of students (as far and making a lasting impact)? (9 pts.)
4. For each blooper Johnson gives a “design rule” (actually usually several). How are these different from his 8 principles? Would there be any use in collecting in one place the list of all the design rules he presents? (9 pts.)
5. You have been
struggling for the past month to animate rectangles and to control their
motion. The model we have used is to
have a Timer activate an actionPerformed() method periodically. The question is what this actionPerformed
code does before it calls the repaint() method of the GUI component. When it repaint() is called the system
places a call to the GUI component’s paintComponent() in the event queue and
when it’s turn to execute comes up, the paintComponent() methods code will
execute g.drawImage() commands for all of the rectangles that must be
redisplayed. These drawImage() calls
are placed in the event queue and eventually cause the pixels in screen memory
to be changed and then those changes are reflected on the screen when it is
refreshed. Explain what your team has
decided to accomplish with the code of the actionPerformed method and how that
relates to what will be done with the code
in the paintComponent method.
(18 pts.)
6. Consider the BoxLayout and compare
it to the GridLayout, highlighting as many differences as you can (reasons why
you would use one rather than the other. (6 pts.)
7. Explain the Event handling model that is used in Java by defining what is meant by registering and listening. Give an example with as much Java as you can remember and use it to discuss both what the programmer must do at compile time and how the JVM processes the event at run time. (9 pts.)
8. Write comments that will explain the code statements that follow the comment space.
/*
* 1.1 version. For the 1.2 version, you would probably
* change getSize() to getWidth() or getHeight().
*/
import java.awt.*;
import java.util.Vector;
// what do we know from the class signature? (3 pts.)
//
//
//
public class DiagonalLayout implements LayoutManager {
private int vgap;
private int minWidth = 0, minHeight = 0;
private int preferredWidth = 0, preferredHeight = 0;
private boolean sizeUnknown = true;
//what does this constructor accomplish? (2 pts.)
//
//
public DiagonalLayout() {
this(5);
}
public DiagonalLayout(int v) {
vgap = v;
}
/* Required by LayoutManager. */
public void addLayoutComponent(String name, Component comp) {
}
/* Required by LayoutManager. */
public void removeLayoutComponent(Component comp) {
}
// the setSizes method ….. (2 pts.)
//
//
private void setSizes(Container parent) {
int nComps = parent.getComponentCount();
Dimension d = null;
//Reset preferred/minimum width and height.
preferredWidth = 0;
preferredHeight = 0;
minWidth = 0;
minHeight = 0;
for (int i = 0; i < nComps; i++) {
Component c = parent.getComponent(i);
if (c.isVisible()) {
d = c.getPreferredSize();
//This if statement is needed because… (2 pts.)
//
//
if (i > 0) {
preferredWidth += d.width/2;
preferredHeight += vgap;
} else {
preferredWidth = d.width;
}
preferredHeight += d.height;
minWidth = Math.max(c.getMinimumSize().width,
minWidth);
minHeight = preferredHeight;
}
}
}
/* Required by LayoutManager. */
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
int nComps = parent.getComponentCount();
setSizes(parent);
//Always add the container's insets!
Insets insets = parent.getInsets();
dim.width = preferredWidth
+ insets.left + insets.right;
dim.height = preferredHeight
+ insets.top + insets.bottom;
sizeUnknown = false;
return dim;
}
/* Required by LayoutManager. */
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
int nComps = parent.getComponentCount();
//Always add the container's insets!
Insets insets = parent.getInsets();
dim.width = minWidth
+ insets.left + insets.right;
dim.height = minHeight
+ insets.top + insets.bottom;
sizeUnknown = false;
return dim;
}
/* Required by LayoutManager. */
/*
* This is called when the panel is first displayed,
* and every time its size changes.
* Note: You CAN'T assume preferredLayoutSize or
* minimumLayoutSize will be called -- in the case
* of applets, at least, they probably won't be.
*/
public void layoutContainer(Container parent) {
// The next three statements… (3 pts.)
//
//
//
//
Insets insets = parent.getInsets();
int maxWidth = parent.getSize().width
- (insets.left + insets.right);
int maxHeight = parent.getSize().height
- (insets.top + insets.bottom);
int nComps = parent.getComponentCount();
int previousWidth = 0, previousHeight = 0;
int x = 0, y = insets.top;
int rowh = 0, start = 0;
int xFudge = 0, yFudge = 0;
boolean oneColumn = false;
// Go through the components' sizes, if neither
// preferredLayoutSize nor minimumLayoutSize has
// been called.
if (sizeUnknown) {
setSizes(parent);
}
if (maxWidth <= minWidth) {
oneColumn = true;
}
//What is the meaning of the value in xFudge and yFudge? (3 pts.)
//
//
//
if (maxWidth != preferredWidth) {
xFudge = (maxWidth - preferredWidth)/(nComps - 1);
}
if (maxHeight > preferredHeight) {
yFudge = (maxHeight - preferredHeight)/(nComps - 1);
}
for (int i = 0 ; i < nComps ; i++) {
Component c = parent.getComponent(i);
if (c.isVisible()) {
Dimension d = c.getPreferredSize();
// increase x and y, if appropriate
//What will decides if it is appropriate to increase x and y? (2 pts.)
//
//
if (i > 0) {
if (!oneColumn) {
x += previousWidth/2 + xFudge;
}
y += previousHeight + vgap + yFudge;
}
// If x is too large,
if ((!oneColumn) &&
(x + d.width) >
(parent.getSize().width - insets.right)) {
// reduce x to a reasonable number.
// what is reasonable about this formula for reducing x? (2 pts.)
//
//
x = parent.getSize().width
- insets.bottom - d.width;
}
// If y is too large,
if ((y + d.height)
> (parent.getSize().height - insets.bottom)) {
// do nothing.
// Another choice would be to do what we do to x.
//Write the code the is suggest as the other choice: (5 pts.)
//
//
//
//
//
//
//
//
//
//
}
// Set the component's size and position.
c.setBounds(x, y, d.width, d.height);
previousWidth = d.width;
previousHeight = d.height;
}
}
}
public String toString() {
String str = "";
return getClass().getName() + "[vgap=" + vgap + str + "]";
}
}
//What does this program illustrate (what happens when it is executed)? (7 pts.)
//
//
//
//
//