Java Reference
In-Depth Information
Example 10•29: AppletMenuBar.java (continued)
if (highlightColor != null) repaint();
}
}
}
/** This method is called when the mouse moves */
protected void processMouseMotionEvent(MouseEvent e) {
processMouseEvent(e);
}
/** This utility method converts an X coordinate to a menu label index */
protected int findItemAt(int x) {
// This could be a more efficient search...
int nummenus = labels.size();
int halfspace = spacing/2-1;
int i;
for(i = nummenus-1; i >= 0; i--) {
if ((x >= startPositions[i]-halfspace) &&
(x <= startPositions[i]+widths[i]+halfspace)) break;
}
return i;
}
/**
* Measure the menu labels, and figure out their positions, so we
* can determine when a click happens, and so we can redraw efficiently.
**/
protected void measure() {
// Get information about the font
FontMetrics fm = this.getFontMetrics(getFont());
// Remember the basic font size
ascent = fm.getAscent();
descent = fm.getDescent();
// Create arrays to hold the measurements and positions
int nummenus = labels.size();
widths = new int[nummenus];
startPositions = new int[nummenus];
// Measure the label strings and
// figure out the starting position of each label
int pos = margins.left;
for(int i = 0; i < nummenus; i++) {
startPositions[i] = pos;
String label = (String)labels.elementAt(i);
widths[i] = fm.stringWidth(label);
pos += widths[i] + spacing;
}
// Compute our preferred size from this data
prefsize.width = pos - spacing + margins.right;
prefsize.height = ascent + descent + margins.top + margins.bottom;
// We've don't need to be remeasured anymore.
remeasure = false;
}
/**
Search WWH ::




Custom Search