: I saw a menu program written with Applet a few days ago on java.sun.com. Since the applet does not currently support Menu (as well as knowing it),: Maybe this program is some help from you.
The original procedure seems to be incomplete, I can't compile, I have contracted one on java.sun.com, Source as follows: / * 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 field 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 Specifie The // Location (Rective 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 point "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 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 sale 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"); margin = INTEGER.PARSEINT (GetParameter ("Marginh")); Marginv = Integer.Parseint (Get Parameter ("Marginv")); // Get Color Parameters. INTS = PARSEINT ("BG-color", "") BGColor = New Color (INTS [0], INTS [1], INTS [2]); INTS = PARSEINT ("FG-menu-color", "" "; fgMenucolor [0] = New color (INTS [ 0], INTS [1], INTS [INTS = PARSEINT (GetParameter ("FG-Hi-Menu-Color", ""); FGMenucolor [1] = New Color (INTS [0], INTS [ 1], INTS [ 2]); INTS = PARSEINT ("BG-MENU-color", "" "; bgmenucolor [0] = new color (INTS [0], INTS [1], INTS [2]); INTS = PARSEINT ("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 ("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; istring [] fields = parse ("Menu" i), getParameter ("Separ
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
Item [i] [j] = fields [j * 2 5];
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 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
Result [I] = st.nextToken ();
}
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
Result [i] = integer.parseint (st.nextToken ());
}
Return Result;
}
Public void paint (graphics g) {
Imagedone = false;
Update (g);
}
Public void Update (graphics g) {
Graphics G2;
IF (!) {
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
DrawMenuItem (G2, 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
g.drawstring (line [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
String [] line = parse (item [curmenu] [i], "^");
For (int J = 0; J
INT W = fm.stringwidth (line [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] .x = x;
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 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
IF (RS [i] .inside (x, y)) {
Return I;
}
}
}
Return -1;
}
}
-
The wind and rain send spring .- -.
Flying snow, spring to ___) | (___
Already a cliff Wanbu Ice` --- '
I still have a branch
※ Source: · Sun and Moonlight Station bbs.fudan.edu.cn · [From: ms]