&l t;PARAM NAME="topic2" VALUE=" Java|http://www.developer.com/directories/pages/dir.java.html"> & lt;PARAM NAME="topic4" VALUE="News & Features|http://www.developer.com/news/news.html"> import java.applet.Applet; import java.awt.*; import java.net.*; public class TopicIndex extends Applet { TextField keyword; List topics; Button find; String urls[]; int pickOld; public String getAppletInfo() { return "Name: TopicIndex\n"; } public void init() { // toCol is a local function that converts the hexadecimal code to an RGB color setBackground(toCol(getParameter("color"))); pickOld = -1; // // Create components for search // keyword = new TextField(); topics = new List(); find = new Button(getParameter("button")); // // Get topics and urls from parameters // int length = 1; while (getParameter("topic" + String.valueOf(length)) != null) { length++; } urls = new String[length]; for (int i = 1; i < length; i++) { String inString = getParameter("topic" + String.valueOf(i)); topics.addItem(inString.substring(0, inString.indexOf("|"))); urls[i] = inString.substring(inString.indexOf("|") + 1,inString.length()); } // // Assign the Layouts. // int cols[] = {size().width - 20}; int rows[] = {35, size().height - 120, 35}; GridBagLayout gbl = new GridBagLayout(); gbl.columnWidths = cols; gbl.rowHeights = rows; GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbl); gbc.gridx = GridBagConstraints.RELATIVE; gbc.insets = new Insets(1, 1, 1, 1); gbc.gridwidth = 1; gbc.anchor = GridBagConstraints.WEST; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbl.setConstraints(keyword, gbc); gbc.gridy = 1; gbc.fill = GridBagConstraints.BOTH; gbl.setConstraints(topics, gbc); gbc.gridy = 2; gbc.fill = GridBagConstraints.NONE; gbl.setConstraints(find, gbc); add(keyword); add(topics); add(find); } public void start() { } public boolean handleEvent (Event e) { if (e.target == find && e.id == Event.ACTION_EVENT) { if (topics.getSelectedIndex() != -1) { try { URL url = http://www.geocities.com/lindachariotnetau/new URL(urls[topics.getSelectedIndex() + 1]); getAppletContext().showDocument(url); } catch (MalformedURLException err) {} } } if (e.target == keyword && e.id == Event.KEY_RELEASE) { int pick = -1; int ceil = 0; // // Array which deals with matches, fill with zeros. // int hits[] = new int[topics.countItems()]; for (int i = 0; i < topics.countItems(); i++) { hits[i] = 0; } // // Find number of match hits based on lowercase versions of terms. // if (keyword.getText().length() > 0) { for (int j = 0; j < topics.countItems(); j++) { String topicText = topics.getItem(j).toLowerCase(); int floor = (topicText.length() < keyword.getText().length()) ? topicText.length() : keyword.getText().length(); for (int i = 0; i < floor; i++) { if (topicText.charAt(i) == keyword.getText().toLowerCase().charAt(i)) { hits[j]++; } else { break; } } } // // Rank highest hit rate. // for (int i = 0; i < hits.length; i++) { if (hits[i] > ceil) { ceil = hits[pick = i]; } } // // Select highest rated hit topic in list. // if (ceil > 0 && pick != pickOld) { topics.select(pick); topics.makeVisible(pick); pickOld = pick; } } } return false; } // // Convert hex colors to rgb // public Color toCol(String c) { int val = 0; try { if (c.startsWith("#")) { return new Color(Integer.parseInt(c.substring(1), 16)); } else if (c.startsWith("0") && c.length() > 1) { return new Color(Integer.parseInt(c.substring(1), 8)); } else { return new Color(Integer.parseInt(c, 10)); } } catch (NumberFormatException e) {} return null; } } 1
Hosted by www.Geocities.ws