import java.applet.*; import java.awt.*; import java.awt.image.FilteredImageSource; import java.net.MalformedURLException; import java.net.URL; public class hover extends Applet { Color BackColor; Color ForeColor; Color HoverColor; String Text; Font UsedFont; FontMetrics UsedMetrics; Image SourceImage; Image HoverImage; Image PressedImage; Image AnimImage; String URLText; String TargetFrame; int TextHAlign; int TextVAlign; int TextWidth; URL DocURL; int CurrentImage = 0; //*************// // functions: // //*************// //******************************// // gets the specified parameter // // and decodes it to a Color // //******************************// Color DecodeColor(String param) { String pString = getParameter(param); Color Result = new Color(Integer.parseInt(pString.substring(0,2), 16), Integer.parseInt(pString.substring(2,4), 16), Integer.parseInt(pString.substring(4,6), 16)); return Result; } //**************************************// // checks, if string contains substring // //**************************************// boolean SubStringExists(String MainString, String SubString) { int index = MainString.indexOf(SubString); boolean Result; if ((index >= 0) & (index < MainString.length())) Result = true; else Result = false; return Result; } //*****************************// // get the specified parameter // // and decodes it to a Font // //*****************************// Font DecodeFont(String param) { int FontStyle; int FontSize; String FontName; Font Result; String pString = getParameter(param); if (pString == null) Result = new Font("Arial", Font.PLAIN, 12); else { int ende = pString.indexOf(","); FontSize = Integer.parseInt(pString.substring(0, ende)); int ende1 = pString.indexOf(",", ende+1); FontName = pString.substring(ende+2, ende1); pString = pString.toUpperCase(); FontStyle = Font.PLAIN; if (SubStringExists(pString, "ITALIC")) FontStyle += Font.ITALIC; if (SubStringExists(pString, "BOLD")) FontStyle += Font.BOLD; Result = new Font(FontName, FontStyle, FontSize); } return Result; } //****************************// // gets specified parameter // // and converts it to integer // //****************************// int DecodeInteger(String param) { String pString = getParameter(param); return Integer.parseInt(pString); } //**************************// // checks, if param exists // //**************************// boolean ParamExists(String param) { String pString = getParameter(param); boolean Result; if (pString == null) Result = false; else Result = true; return Result; } //*****************************// // ligthens or darkens a color // // by the given parameter // //*****************************// Color ModifyColor(Color color, int amount) { int r = color.getRed(); int g = color.getGreen(); int b = color.getBlue(); r += amount; if (r > 255) r = 255; else if (r < 0) r = 0; g += amount; if (g > 255) g = 255; else if (g < 0) g = 0; b += amount; if (b > 255) b = 255; else if (b < 0) b = 0; Color Result = new Color(r, g, b); return Result; } public boolean mouseEnter(Event event, int i, int j) { CurrentImage = 2; repaint(); return true; } public boolean mouseExit(Event event, int i, int j) { CurrentImage = 1; repaint(); return true; } public void paint(Graphics g) { switch (CurrentImage) { default: break; case 1: g.drawImage(SourceImage, 0, 0, this); break; case 2: g.drawImage(HoverImage, 0, 0, this); break; case 3: g.drawImage(PressedImage, 0, 0, this); break; } } public boolean mouseUp(Event event, int i, int j) { if ((TargetFrame != null) || (TargetFrame != "")) { getAppletContext().showDocument(DocURL, TargetFrame); } else { getAppletContext().showDocument(DocURL); } return true; } public boolean mouseDown(Event event, int i, int j) { CurrentImage = 3; repaint(); return true; } public void destroy() { } public void update(Graphics g) { paint(g); } public void start() { } public void stop() { } public void init() { // get various colors: if (ParamExists("background")) BackColor = DecodeColor("background"); else BackColor = Color.white; if (ParamExists("foreground")) ForeColor = DecodeColor("foreground"); else ForeColor = Color.black; if (ParamExists("hovercolor")) HoverColor = DecodeColor("hovercolor"); else HoverColor = Color.black; // get the button text: if (ParamExists("text")) Text = getParameter("text"); else Text = "no text given"; // get the url: if (ParamExists("url")) URLText = getParameter("url"); else URLText = ""; // get the target frame: if (ParamExists("target")) TargetFrame = getParameter("target"); else TargetFrame = ""; // get font and its style: UsedFont = DecodeFont("font"); // get the font align: if (ParamExists("align")) { /******************/ /** 1 .. center **/ /** 2 .. left **/ /** 3 .. right **/ /******************/ String align = getParameter("align"); align = align.toUpperCase(); if (SubStringExists(align, "CENTER")) TextHAlign = 1; else if (SubStringExists(align, "LEFT")) TextHAlign = 2; else if (SubStringExists(align, "RIGHT")) TextHAlign = 3; else TextHAlign = 3; /******************/ /** 1 .. center **/ /** 2 .. top **/ /** 3 .. bottom **/ /******************/ if (SubStringExists(align, "CENTER")) TextVAlign = 1; else if (SubStringExists(align, "TOP")) TextVAlign = 2; else if (SubStringExists(align, "BOTTOM")) TextVAlign = 3; else TextVAlign = 3; } else { TextHAlign = 1; TextVAlign = 1; } // set background color and font setBackground(BackColor); setFont(UsedFont); //get the font metrics UsedMetrics = getFontMetrics(UsedFont); TextWidth = UsedMetrics.stringWidth(Text); // get the document URL: if ((URLText != "") || (URLText != null)) try { DocURL = new URL(getDocumentBase(), URLText); } catch(MalformedURLException e) {} // create 3 images and clear them: // - main image, when mouse not in applet // - hover image, when mouse is in applet // - pressed image, when mouse button is pressed int w = getSize().width; int h = getSize().height; int d_c = 20; //holds delta color if (ParamExists("framecol")) d_c = DecodeInteger("framecol"); { SourceImage = createImage(w, h); Graphics g = SourceImage.getGraphics(); g.setColor(BackColor); g.fillRect(0, 0, w, h); Color bevel = ModifyColor(BackColor, -d_c); { g.setColor(bevel); g.drawLine(2, 2, getSize().width-3, 2); g.drawLine(2, 2, 2, getSize().height-3); } bevel = ModifyColor(bevel, -d_c); { g.setColor(bevel); g.drawLine(1, 1, getSize().width-2, 1); g.drawLine(1, 1, 1, getSize().height-2); } bevel = ModifyColor(bevel, -d_c); { g.setColor(bevel); g.drawLine(0, 0, getSize().width, 0); g.drawLine(0, 0, 0, getSize().height); } bevel = ModifyColor(BackColor, d_c); { g.setColor(bevel); g.drawLine(3, getSize().height-3, getSize().width-4, getSize().height-3); g.drawLine(getSize().width-3, getSize().height-3, getSize().width-3, 3); } bevel = ModifyColor(bevel, d_c); { g.setColor(bevel); g.drawLine(2, getSize().height-2, getSize().width-3, getSize().height-2); g.drawLine(getSize().width-2, getSize().height-2, getSize().width-2, 2); } bevel = ModifyColor(bevel, d_c); { g.setColor(bevel); g.drawLine(1, getSize().height-1, getSize().width-2, getSize().height-1); g.drawLine(getSize().width-1, getSize().height-1, getSize().width-1, 1); } g.setColor(ForeColor); int l = w/2 - UsedMetrics.stringWidth(Text)/2; int t = h/2 + UsedFont.getSize()/2; g.drawString(Text, l, t-1); } { HoverImage = createImage(w, h); Graphics g = HoverImage.getGraphics(); g.setColor(HoverColor); g.fillRect(0, 0, w, h); Color bevel = ModifyColor(HoverColor, -d_c); { g.setColor(bevel); g.drawLine(2, 2, getSize().width-3, 2); g.drawLine(2, 2, 2, getSize().height-3); } bevel = ModifyColor(bevel, -d_c); { g.setColor(bevel); g.drawLine(1, 1, getSize().width-2, 1); g.drawLine(1, 1, 1, getSize().height-2); } bevel = ModifyColor(bevel, -d_c); { g.setColor(bevel); g.drawLine(0, 0, getSize().width, 0); g.drawLine(0, 0, 0, getSize().height); } bevel = ModifyColor(HoverColor, d_c); { g.setColor(bevel); g.drawLine(3, getSize().height-3, getSize().width-4, getSize().height-3); g.drawLine(getSize().width-3, getSize().height-3, getSize().width-3, 3); } bevel = ModifyColor(bevel, d_c); { g.setColor(bevel); g.drawLine(2, getSize().height-2, getSize().width-3, getSize().height-2); g.drawLine(getSize().width-2, getSize().height-2, getSize().width-2, 2); } bevel = ModifyColor(bevel, d_c); { g.setColor(bevel); g.drawLine(1, getSize().height-1, getSize().width-2, getSize().height-1); g.drawLine(getSize().width-1, getSize().height-1, getSize().width-1, 1); } g.setColor(ForeColor); int l = w/2 - UsedMetrics.stringWidth(Text)/2; int t = h/2 + UsedFont.getSize()/2; g.drawString(Text, l-4, t-3); } { PressedImage = createImage(w, h); Graphics g = PressedImage.getGraphics(); Color PressColor = ModifyColor(HoverColor, 40); g.setColor(PressColor); g.fillRect(0, 0, w, h); Color bevel = ModifyColor(PressColor, -d_c); { g.setColor(bevel); g.drawLine(2, 2, getSize().width-3, 2); g.drawLine(2, 2, 2, getSize().height-3); } bevel = ModifyColor(bevel, -d_c); { g.setColor(bevel); g.drawLine(1, 1, getSize().width-2, 1); g.drawLine(1, 1, 1, getSize().height-2); } bevel = ModifyColor(bevel, -d_c); { g.setColor(bevel); g.drawLine(0, 0, getSize().width, 0); g.drawLine(0, 0, 0, getSize().height); } bevel = ModifyColor(PressColor, d_c); { g.setColor(bevel); g.drawLine(3, getSize().height-3, getSize().width-4, getSize().height-3); g.drawLine(getSize().width-3, getSize().height-3, getSize().width-3, 3); } bevel = ModifyColor(bevel, d_c); { g.setColor(bevel); g.drawLine(2, getSize().height-2, getSize().width-3, getSize().height-2); g.drawLine(getSize().width-2, getSize().height-2, getSize().width-2, 2); } bevel = ModifyColor(bevel, d_c); { g.setColor(bevel); g.drawLine(1, getSize().height-1, getSize().width-2, getSize().height-1); g.drawLine(getSize().width-1, getSize().height-1, getSize().width-1, 1); } int c_r = ForeColor.getRed(); int c_g = ForeColor.getGreen(); int c_b = ForeColor.getBlue(); Color FontColor = new Color(0xFF-c_r, 0xFF-c_g, 0xFF-c_b); g.setColor(FontColor); int l = w/2 - UsedMetrics.stringWidth(Text)/2; int t = h/2 + UsedFont.getSize()/2; g.drawString(Text, l-4, t-3); } CurrentImage = 1; } public String getAppletInfo() { return "Title: Hover-Applet \nAuthor: Klaus Burgstaller \nlast modified on 15.11.2001 \ncopyright (c) by Klaus Burgstaller. \nwww.crosswinds.net/~burgstaller"; } public String[][] getParameterInfo() { String[][] info = { {"text", "string", "Contains text to be displayed. Has no default!! The component doesn't work otherwise!"}, {"foreground", "hex color", "Foreground color in hex format: RRGGBB"}, {"background", "hex color", "Works the same way for the back ground color"}, {"hovercolor", "hex color", "Works the same way for the back ground color"}, {"target", "string", "Optional, may define target frame."}, {"url", "string", "No defualt!! Contains URL to be linked."}, {"font", "font size, font name, font style", "contains description about the font style: \nfirst parameter is font size, second font name, \nadditional font stylings can be given: BOLD, ITALIC, PLAIN. \nYou may write the font stylings upper od lower case. \nDefault = Arial, plain, size:12 ."} }; return info; } }