/* * The URLMenu class -(c) Suresh Kumar Nalluru */ /* * Assumptions: * 1. The 3 images are of the same size. * 2. There are atmost 50 URL's per applet. * 3. There is atleast one URL per applet. * 4. No check is made if the whole string is displayable * or not. In such cases it just chops off the string * and displays whatever is visible. * 5. If the Font cant be loaded, then some default font is used * instead of giving the error messages, * 6. If a image cant be loaded, it silently goes to no-image mode * instead of complaining. * 7. If statustext is given then it is tried to display these strings * and in case , the number of strings are not equal to the * number of text, then the default method is used. * * 8. Showstatus: * 0 : No status text * 1 : Text in applet: URL format * 2 : Statustext is displayed */ import java.applet.*; import java.awt.*; import java.net.*; public class URLMenu extends Applet implements Runnable { /* * butCnt No. of buttons * images Display images? * horiStyle Horizontal style? * activeIndex Which of the buttons is active * n_URLMenu The thread of the applet * n_Graphics The raphics context of the thread * n_Images The array of the loaded images * n_fAllLoaded Are all the images loaded? * mouse_down Is mouse pressed down? * NUM_IMAGES The number of images in the images array * n_nImgWidth The width of each of the image * n_nImgHeight The height of each of the image * w,h Width and Height of each of active region * fontsize The fontsize */ private int butCnt = 0; private boolean images = true; private int showstatus = 1; private boolean horiStyle = false; private int activeIndex = -1; private Thread n_URLMenu = null; private Graphics n_Graphics; private Image n_Images[]; private boolean n_fAllLoaded = false; private boolean mouse_down = false; private final int NUM_IMAGES = 3; private int n_nImgWidth = 0; private int n_nImgHeight = 0; private int w , h; private int fontsize = 12; // assume atmost 50 URl's private String n_url[] = new String[50]; private String n_text[] = new String[50]; private String n_statustext[] = new String[50]; private String n_frame[] = new String[50]; // default foreground and background colors private Color n_fgColor = new Color(255,255,40); private Color n_bkColor2 = new Color(42,42,140); private Color n_bkColor1 = new Color(58,185,224); // strings to get the parameters private final String param_url = "url"; private final String param_frame = "frame"; private final String param_text = "text"; private final String param_showstatus = "showstatus"; private final String param_statustext = "statustext"; private final String param_bkColor1 = "bkColor1"; private final String param_bkColor2 = "bkColor2"; private final String param_fgColor = "fgColor"; private final String param_font = "font"; private final String param_fontsize = "fontsize"; private final String param_style = "style"; private final String param_images = "images"; private final String DEFAULT_font = "Helvetica"; public String getAppletInfo() { return "Name: URLMenu\r\n" + "Author: Suresh Kumar Nalluru\r\n" ; } /* * Given a string it will parse it by commas. If will * fill the destination array with the sub strings * and will return the number of sub strings that are * extracted from the Main string */ private int parse(String dst[], String src) { int i=0,x=0; while(true) { int n = src.indexOf(',', x); if(n==-1) { dst[i] = src.substring(x); break; } else dst[i] = src.substring(x, n); dst[i] = dst[i].trim(); x = n+1; i++; } return i+1; } /* * It will initialise the various varibles from the * parameters passed to the applet. Note the initialisation * of h,w by dividing the applet size with the number or URLs */ public void init() { String param; String col[] = new String[3]; int i; Color c; param = getParameter(param_url); if (param != null) parse(n_url, param); try { param = getParameter(param_bkColor1); if (param != null) { i=parse(col,param); // is in the format R,G,B ?? if(i==3) { c = new Color(Integer.parseInt(col[0]), Integer.parseInt(col[1]), Integer.parseInt(col[2])); // Only when there is no error if(c!=null) n_bkColor1 = c; } } param = getParameter(param_bkColor2); if (param != null) { i=parse(col,param); if(i==3) { c = new Color(Integer.parseInt(col[0]), Integer.parseInt(col[1]), Integer.parseInt(col[2])); if(c!=null) n_bkColor2 = c; } } param = getParameter(param_fgColor); if (param != null) { i=parse(col,param); if(i==3) { c = new Color(Integer.parseInt(col[0]), Integer.parseInt(col[1]), Integer.parseInt(col[2])); if(c!=null) n_fgColor = c; } } } catch(Exception e) { }; param = getParameter(param_text); if (param != null) butCnt = parse(n_text, param); param = getParameter(param_showstatus); if (param != null) showstatus = Integer.parseInt(param); param = getParameter(param_statustext); if (param != null) { i = parse(n_statustext, param); if(i!= butCnt && showstatus==2) showstatus = 1; } else if(showstatus ==2) showstatus = 1; param = getParameter(param_frame); if (param != null) parse(n_frame, param); param = getParameter(param_fontsize); if (param != null) fontsize = Integer.parseInt(param); param = getParameter(param_font); if (param != null) setFont(new Font(param,Font.PLAIN,fontsize)); else setFont(new Font(DEFAULT_font,Font.PLAIN,fontsize)); param = getParameter(param_images); if (param != null) images = (Integer.parseInt(param)==1); param = getParameter(param_style); if (param != null) horiStyle = (Integer.parseInt(param)==1); Dimension d=size(); if(horiStyle) { w=(d.width+1)/butCnt; h=d.height; } else { w= d.width; h=(d.height+1)/butCnt; }; } /* * this routine draws one region. n specifies the active region * to be drawn. Style is the style with which it is drawn. * * style: * 0: Default * 1: Mouse on an URL * 2: Selected URL ,by mouse click */ private void draw1(Graphics g, int n,int stage) { // for boundary conditions if(n>=butCnt) return; if(n<0) return; // the rectangle to be painted Rectangle r; if(horiStyle) r = new Rectangle(w*n,0,w,h); else r = new Rectangle(0,n*h,w,h); if(r==null) return; // Only when all images are loaded , then do the // fashion things if ((!images) || n_fAllLoaded) { // draw the appropriate background color if(stage==0) { g.setColor(n_bkColor1); g.fill3DRect(r.x, r.y, r.width, r.height,false); } else { g.setColor(n_bkColor2); g.fill3DRect(r.x, r.y, r.width, r.height,true); } // draw the image if(images) { if(horiStyle) g.drawImage(n_Images[stage], w*n+5, (h - n_nImgHeight) / 2, null); else g.drawImage(n_Images[stage], 5, n*h+(h - n_nImgHeight) / 2, null); } } // draw the string g.setColor(n_fgColor); g.drawString(n_text[n], r.x+10+n_nImgWidth, r.y+r.height/2 + g.getFontMetrics().getAscent()/2); if(activeIndex>=0) { if (showstatus==1) showStatus(n_text[n]+" : " + n_url[n]); else if(showstatus ==2) showStatus(n_statustext[n]); } } public void paint(Graphics g) { for(int i=0; i=h) { activeIndex = -1; return true; } } else { if(x<0 || x>= w) { activeIndex = -1; return true; } }; temp=xy2ai(x,y); if(temp != activeIndex && ((!images) || n_fAllLoaded)) { if(activeIndex!=-1) draw1(n_Graphics, activeIndex, 0); activeIndex = temp; draw1(n_Graphics, activeIndex, 2); } return true; } public boolean mouseMove(Event evt, int x, int y) { validateAI(x,y); return true; } public boolean mouseExit(Event evt, int x, int y) { if((!images) || n_fAllLoaded) { draw1(n_Graphics, activeIndex, 0); activeIndex = -1; } return true; } }