// Displaying a Palindrome import java.applet.*; import java.awt.*; import java.net.URL; public class awcpart1 extends Applet { String msgs[]= { "A man, a plan, a canal, Panama!", "Rats live on no evil star", "Rise to vote sir", "Flee to me remote elf", "Never odd or even", "Too bad, I hid a boot" }; int ptsize[] = new int[6]; // point size int current=0; public String getAppletInfo() { String s= "Palindrome applet (part 1) by Al Williams"; return s; } protected Color getColorParam(String name) { String hexstring=getParameter(name); try { return new Color(Integer.parseInt(hexstring,16)); } catch (Exception e) { return null; } } private String[][] paraminfo= { { "background", "hex color value (RRGGBB)", "background color" }, { "foreground", "hex color value (RRGGBB)", "foreground color" }, { "nexturl","URL","Next URL" } }; public String[][] getParameterInfo() { return paraminfo; } public void init() { Color clr=getColorParam("Background"); if (clr==null) clr=new Color(128,128,128); // gray default setBackground(clr); clr=getColorParam("Foreground"); if (clr!=null) { setForeground(clr); } } private void calcptsize(Graphics g, Dimension d) { FontMetrics fm; boolean ok=false; // font fits int maxsiz=64; Font f; g.setFont(f=new Font("Helvetica", Font.PLAIN, maxsiz)); fm=g.getFontMetrics(); while (ok==false) { if ((fm.getHeight()<=d.height) && fm.stringWidth(msgs[current])<=d.width) { ok=true; } else { maxsiz-=2; g.setFont(f=new Font("Helvetica",Font.PLAIN,maxsiz)); fm=g.getFontMetrics(); } } ptsize[current]=maxsiz; } public void paint(Graphics g) { Dimension d=size(); FontMetrics fm; if (ptsize[current]==0) calcptsize(g,d); else g.setFont(new Font("Helvetica",Font.PLAIN,ptsize[current])); fm=g.getFontMetrics(); g.drawString(msgs[current],0, d.height/2+fm.getMaxAscent()/2); } public void next() { if (++current>=msgs.length) { String nxturl=getParameter("nexturl"); boolean enroute=false; current‹; if (nxturl!=null) { URL nurl=null; // set to null to suppress warning try { nurl= new URL(getDocumentBase(),nxturl); enroute=true; } catch (Exception e) { } getAppletContext().showDocument(nurl); } // if there is no URL, or a bad URL, start over if (enroute==false) { current=0; } } repaint(); } public boolean mouseEnter(Event e, int x, int y) { AppletContext ctx=getAppletContext(); ctx.showStatus("Here¹s a palindrome"); return true; } public boolean mouseExit(Event e, int x,int y) { AppletContext ctx=getAppletContext(); ctx.showStatus(""); return true; } }