Java Reference
In-Depth Information
Example 10•29: AppletMenuBar.java (continued)
* If we were implementing a Swing component, we'd override
* paintComponent() instead
**/
public void paint(Graphics g) {
if (remeasure) measure(); // Remeasure everything first, if needed
// Figure out Y coordinate to draw at
Dimension size = getSize();
int baseline = size.height - margins.bottom - descent;
// Set the font to draw with
g.setFont(getFont());
// Loop through the labels
int nummenus = labels.size();
for(int i = 0; i < nummenus; i++) {
// Set the drawing color. Highlight the current item
if ((i == highlightedItem) && (highlightColor != null))
g.setColor(getHighlightColor());
else
g.setColor(getForeground());
// Draw the menu label at the position computed in measure()
g.drawString((String)labels.elementAt(i),
startPositions[i], baseline);
}
// Now draw a groove at the bottom of the menubar.
Color bg = getBackground();
g.setColor(bg.darker());
g.drawLine(0, size.height-2, size.width, size.height-2);
g.setColor(bg.brighter());
g.drawLine(0, size.height-1, size.width, size.height-1);
}
/** Called when a mouse event happens over the menubar */
protected void processMouseEvent(MouseEvent e) {
int type = e.getID(); // What type of event?
int item = findItemAt(e.getX()); // Over which menu label?
if (type == MouseEvent.MOUSE_PRESSED) {
// If it was a mouse down event, then pop up the menu
if (item == -1) return;
Dimension size = getSize();
PopupMenu pm = (PopupMenu) menus.elementAt(item);
if (pm != null) pm.show(this, startPositions[item]-3, size.height);
}
else if (type == MouseEvent.MOUSE_EXITED) {
// If the mouse left the menubar, then unhighlight
if (highlightedItem != -1) {
highlightedItem = -1;
if (highlightColor != null) repaint();
}
}
else if ((type == MouseEvent.MOUSE_MOVED) ||
(type == MouseEvent.MOUSE_ENTERED)) {
// If the mouse moved, change the highlighted item, if necessary
if (item != highlightedItem) {
highlightedItem = item;
Search WWH ::




Custom Search