/** * Search Engine Applet 1.0.1 * @author Robert John Morton * @version 22 October 1999 modified 10 July 2009, 10 April 2012 * @copyright Robert John Morton (all rights reserved) */ import java.applet.*; // all the gubbins to make the applet work import javax.swing.*; // Swing GUI import java.awt.*; // for graphics operations (GUI) import java.util.*; // for stream tokenizer in formatText() public class img1panel extends JPanel { // TITLE & SUMMARY DISPLAY PANEL /* Declare two fonts for the lettering used on this panel. The first is for the title text and the second is for URLs and the summary text. */ private static final Font font1 = new Font("Sans",Font.PLAIN,18), font2 = new Font("Sans",Font.PLAIN,17); private FontMetrics fm, // common FontMetrics variable fm1, // FontMetrics for title font fm2; // FontMetrics for summary font private search hf; // instance reference of the search applet private Color bg; // background colour for panel // Declare the colour for the title font. private static final Color titleBlue = new Color(16,32,192); private static final int Xt = 5, // inset for drawing title lines Xs = 15; // inset for drawing summary lines private int fh1, // font height for title font fh2, // font height for summary font space, // horizontal number of pixels that equals one space character sp1, // space size for title font sp2, // space size for summary font W, H, // width and height of panel's display area Wt, // width of title lines Ws, // width of summary lines Qt, // index pf first summary line Qs, // total number of lines in title + summary q; // index of first line of summary text (after the title) // Accommodation for up to 10 lines of title + summary text private String S[] = new String[10]; img1panel(int W, int H, Color bg, search hf) { this.bg = bg; // copy background colour to local variable this.W = W; // copy panel width to local variable this.H = H; // copy panel height to local variable this.hf = hf; // copy applet's instance reference to local variable Wt = W - Xt - Xt; // maximum width of text on title lines Ws = W - Xt - Xs; // maximum width of text on summary lines /* For each of the lettering fonts font1 and font 2, get its measurements (in pixels), the height of a line of text + 2 for interline spacing pixels, and the width of a 'space' character. */ fm1 = getFontMetrics(font1); fm2 = getFontMetrics(font2); fh1 = fm1.getHeight() + 2; fh2 = fm2.getHeight() + 2; sp1 = fm1.stringWidth(" "); sp2 = fm2.stringWidth(" "); } // DISPLAY THIS DOCUMENT'S TITLE AND DESCRIPTION void showSummary(String Title, String Descr) { // clear the title + summary array for(int i = 0; i < S.length; i++) S[i] = ""; q = 0; // index number of first title line space = sp1; // set space size for title font fm = fm1; // set FontMetrics for title font Qt = formatText(Title, Wt, 3); // form the title lines space = sp2; // set space size for summary font fm = fm2; // set FontMetrics for summary font Qs = formatText(Descr, Ws, 9); // form the description lines repaint(); // schedule a repaint via the event despatching thread } // DISPLAY THE DOCUMENT'S TITLE, DESCRIPTION AND URL public void paint(Graphics g) { // draw the background image onto the applet canvas g.drawImage(hf.IMG1,0,0,this); g.setFont(font1); // set title font g.setColor(titleBlue); // set text foreground colour for title int δY = fh1; // vertical position of current line for(int i = 0; i < Qt; i++) { g.drawString(S[i], Xt, δY); // display each line of the title δY += fh1; // inch down to the next title line } g.setFont(font2); // set summary font g.setColor(Color.black); // set text foreground colour for summary for(int i = Qt; i < Qs; i++) { g.drawString(S[i], Xs, δY); // display each line of the summary δY += fh2; // inch down to the next summary line } } /* SET A STRING OF TEXT INTO A MULTI-LINE ARRAY OF A SPECIFIED MAXIMUM LINE WIDTH */ private int formatText(String text, int width, int E) { // This enables you to pick of one word at a time from the input string StringTokenizer st = new StringTokenizer(text); String s = ""; // current output line int x = 0; // current width of output line String word; // current word /* While there is still another word available in the input string, pick the new word from the string and find its width in pixels. */ while(st.hasMoreTokens()) { word = st.nextToken(); int w = fm.stringWidth(word); /* If adding this word to the current line of text will cause the line to overshoot the maximum line width, reset the line width to the width of the left margin and the line length to zero. */ if((x + space + w > width) && q < E) {S[q++] = s; s = ""; x = 0;} s += " " + word; // add a space + new word to current line x += space + w; // set line width to include space + new word } S[q++] = s; // final short line return q; // number of lines used in the string array S } }