前几天在java.sun.com上看见一个用applet写的菜单程序。由于applet目前不支持menu(
据我所知),: 也许这个程序对你有些帮助。
原来的程序好像不完整,无法编译,我特地到java.sun.com上又载了一个,
source如下:
/*
copyright: sun microsystems 1997. all rights reserved.
author: patrick chan (www.xeo.com) 7/19/96
version: 1.1
*/
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
public class xeomenu extends applet {
// the background image. this had better not be null.
image image;
// these two fields are used to do double-buffering.
// the dimensions of bbuf is exactly the dimensions of the applet.
image bbuf;
graphics bbufg;
// this field is set to true only when the background image has
// completely loaded.
boolean imagedone;
/* menu data */
rectangle[] hitarea;
rectangle[] srcrect;
point[] dstpt;
boolean[] down;
string[] url;
/* submenu data */
string[][] itemurl;
string[][] item;
// if >= 0, this fields holds the index of the current menu.
// if -1, no menu is current.
int curmenu;
// if >= 0, this fields holds the index of the current menu item.
// if -1, no menu item is current.
int curmenuitem;
// this is an array of rectangles - one rectangle for each menu item.
// each rectangle specifies the
// location (relative to the left-corner of the applet) of a menu item.
//
// menuitemrect is null when curmenu is -1.
// it becomes non-null when curmenu >= 0.
//
// note: it would have been better programming to define classes for
// the menu and menu items. however, i decided for this little applet
// to keep the number of class files to a minimum to minimize the download
// time.
rectangle[] menuitemrect;
// this is the color to paint "behind" the image.
color bgcolor;
// [0] is the text color of a menu item; [1] is the text color of a highlig
hted // menu item.
color fgmenucolor[] = new color[2];
// this is the background of a menu item; [1] is the background color of a
// highlighted menu item.
color bgmenucolor[] = new color[2];
// marginh is the number of pixels on the left and right edges of the menu.
// marginv is the number of pixels on the top and bottom edges of the menu.
int marginh, marginv;
// this is the font used to display the menu item labels.
font f;
// this is the font metrics of 'f'.
fontmetrics fm;
public void init() {
int[] ints;
// grab applet parameters.
image = getimage(getcodebase(), getparameter("image"));
marginh = integer.parseint(getparameter("marginh"));
marginv = integer.parseint(getparameter("marginv"));
// get color parameters.
ints = parseint(getparameter("bg-color"), " ");
bgcolor = new color(ints[0], ints[1], ints[2]);
ints = parseint(getparameter("fg-menu-color"), " ");
fgmenucolor[0] = new color(ints[0], ints[1], ints[2]);
ints = parseint(getparameter("fg-hi-menu-color"), " ");
fgmenucolor[1] = new color(ints[0], ints[1], ints[2]);
ints = parseint(getparameter("bg-menu-color"), " ");
bgmenucolor[0] = new color(ints[0], ints[1], ints[2]);
ints = parseint(getparameter("bg-hi-menu-color"), " ");
bgmenucolor[1] = new color(ints[0], ints[1], ints[2]);
// create back buffer for double-buffering.
bbuf = createimage(size().width, size().height);
bbufg = bbuf.getgraphics();
// determine the font from the font-height.
int fh = integer.parseint(getparameter("font-height"));
int i = fh;
while (i > 10) {
f = new font(getparameter("font"), font.plain, i);
fm = getfontmetrics(f);
if (fm.getheight() <= fh) {
break;
}
i--;
}
// get the menu parameters.
for (i=0; ; i++) {
if (getparameter("menu"+i) == null) {
hitarea = new rectangle[i];
srcrect = new rectangle[i];
dstpt = new point[i];
url = new string[i];
down = new boolean[i];
itemurl = new string[i][];
item = new string[i][];
break;
}
}
for (i=0; i
ator"));
// get the hit area.
ints = parseint(fields[0], " ");
hitarea[i] = new rectangle(ints[0], ints[1], ints[2], ints[3]);
// get the source image.
ints = parseint(fields[1], " ");
srcrect[i] = new rectangle(ints[0], ints[1], ints[2], ints[3]);
// get the destination point.
ints = parseint(fields[2], " ");
dstpt[i] = new point(ints[0], ints[1]);
down[i] = fields[3].equals("d");
url[i] = fields[4];
item[i] = new string[(fields.length-5)/2];
itemurl[i] = new string[(fields.length-5)/2];
for (int j=0; j
itemurl[i][j] = fields[j*2+6];
}
}
}
// s is a string containing 'sep' separators. this method
// breaks up the string at the separators and returns the resulting
// strings in an array. the result may have zero length but is never null.
string[] parse(string s, string sep) {
stringtokenizer st = new stringtokenizer(s, sep);
string result[] = new string[st.counttokens()];
for (int i=0; i
}
return result;
}
// this method is similar to parse() except that the strings are
// assumed to be decimal integers. this method coverts these integer
// strings into integers and returns them in an array.
// the result may have zero length but is never null.
int[] parseint(string s, string sep) {
stringtokenizer st = new stringtokenizer(s, sep);
int[] result = new int[st.counttokens()];
for (int i=0; i
}
return result;
}
public void paint(graphics g) {
imagedone = false;
update(g);
}
public void update(graphics g) {
graphics g2;
if (!imagedone) {
imagedone = g.drawimage(image, 0, 0, this);
return;
}
bbufg.setcolor(bgcolor);
bbufg.fillrect(0, 0, size().width, size().height);
bbufg.drawimage(image, 0, 0, this);
if (curmenu >= 0) {
g2 = bbuf.getgraphics();
// paint the overlay image
g2.cliprect(dstpt[curmenu].x, dstpt[curmenu].y,
srcrect[curmenu].width, srcrect[curmenu].height);
g2.drawimage(image, dstpt[curmenu].x-srcrect[curmenu].x,
dstpt[curmenu].y-srcrect[curmenu].y, this);
g2.dispose();
g2 = bbuf.getgraphics();
for (int i=0; i
}
g2.dispose();
}
g.drawimage(bbuf, 0, 0, this);
}
void drawmenuitem(graphics g, int i) {
int x, y, w, height;
// break the menu item label into lines.
string[] line = parse(item[curmenu][i], getparameter("newline"));
int hi = 0;
if (i == curmenuitem) {
hi = 1;
getappletcontext().showstatus(itemurl[curmenu][i]);
}
g.setcolor(bgmenucolor[hi]);
g.fillrect(menuitemrect[i].x, menuitemrect[i].y,
menuitemrect[i].width, menuitemrect[i].height);
// set color for text and box
g.setcolor(fgmenucolor[hi]);
// draw box around menu item.
g.drawrect(menuitemrect[i].x, menuitemrect[i].y,
menuitemrect[i].width, menuitemrect[i].height);
// draw label
g.setfont(f);
y = menuitemrect[i].y + marginv;
for (i=0; i
menuitemrect[i].x+menuitemrect[i].width-fm.stringwidth(line[i])
-marginh, y + fm.getascent());
y += fm.getheight();
}
}
public boolean mouseexit(event evt, int x, int y) {
curmenuitem = curmenu = -1;
repaint();
return true;
}
public boolean mouseenter(event evt, int x, int y) {
return mousemove(evt, x, y);
}
public boolean mousedown(event evt, int x, int y) {
try {
string u = null;
if (curmenuitem >= 0 && itemurl[curmenu].length > 0) {
u = itemurl[curmenu][curmenuitem];
} else if (curmenu >= 0) {
u = url[curmenu];
}
if (u != null) {
url url = new url (getdocumentbase(), u);
if (getparameter("target") != null) {
getappletcontext().showdocument(url, getparameter("target")
); } else {
getappletcontext().showdocument(url);
}
}
} catch (exception e) {
e.printstacktrace();
}
return true;
}
public boolean mousemove(event evt, int x, int y) {
if (curmenu >= 0) {
int sm = inmenu(menuitemrect, x, y);
if (curmenuitem != sm) {
curmenuitem = sm;
repaint();
}
if (sm >= 0) {
return true;
}
curmenu = -1;
}
int m = inmenu(hitarea, x, y);
if (m != curmenu) {
curmenu = m;
// a new menu is now active so compute menuitemrect.
if (m >= 0) {
// minimum width
int maxwidth = 50;
int maxheight = 0;
menuitemrect = new rectangle[item[curmenu].length];
for (int i=0; i
for (int j=0; j
if (w > maxwidth) {
maxwidth = w;
}
}
menuitemrect[i] = new rectangle();
menuitemrect[i].height =
parse(item[curmenu][i], "^").length * fm.getheight()
+ 2 * marginv;
maxheight += menuitemrect[i].height;
}
// determine domain of submenus
// add one extra pixel for the left edge.
maxwidth += 2 * marginh + 1;
if (down[m]) {
y = math.max(0, math.min(size().height-maxheight-1,
dstpt[curmenu].y + srcrect[curmenu].height-1));
} else {
y = math.max(0, math.min(size().height-maxheight-1,
dstpt[curmenu].y - maxheight));
}
x = dstpt[curmenu].x + srcrect[curmenu].width-maxwidth-1;
for (int i=0; i
menuitemrect[i].y = y;
menuitemrect[i].width = maxwidth;
y += menuitemrect[i].height;
}
getappletcontext().showstatus(url[curmenu]);
}
}
repaint();
}
return true;
}
// returns the index of the rectangle in rs containing x and y.
// returns -1 if either rs is null or x and y is not in rs.
int inmenu(rectangle[] rs, int x, int y) {
if (rs != null) {
for (int i=0; i
return i;
}
}
}
return -1;
}
}
闽公网安备 35060202000074号