/** * Search Engine Applet 1.0.1 * @author Robert John Morton * @version 22 October 1999 modified 10 July 2009, 10 April 2012 JFramed Sat 04 Mar 2017 11:35:32 BRT * @copyright Robert John Morton (all rights reserved) */ 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 private static final Font // for url and summary number 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 private static final Color titleBlue = new Color(16,32,192); // colour for title font 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) private String S[] = new String[10]; // up to 10 lines of title + summary text 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 fm1 = getFontMetrics(font1); // get the font1's dimensions fh1 = fm1.getHeight() + 2; // get the height of a line of text + 2 // for interline spacing sp1 = fm1.stringWidth(" "); // get the width of the space character fm2 = getFontMetrics(font2); // get font2's dimensions fh2 = fm2.getHeight() + 2; // get the height of a line of text + 2 // for interline spacing sp2 = fm2.stringWidth(" "); // get the width of the space character } // DISPLAY DOCUMENT'S TITLE, DESCRIPTION void showSummary(String Title, String Descr) { for(int i = 0; i < S.length; i++) S[i] = ""; // clear the title + summary array 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 DOCUMENT'S TITLE, DESCRIPTION & URL public void paint(Graphics g) { g.drawImage(hf.IMG1,0,0,this); // draw the background image // onto the applet canvas 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, OF A SPECIFIED MAX- IMUM LINE WIDTH, INTO A MULTI-LINE ARRAY */ private int formatText(String text, int width, int E) { // to pick one word at a time from 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 another word is available from the input string while(st.hasMoreTokens()) { word = st.nextToken(); // get new word from input string int w = fm.stringWidth(word); // get its width // if adding this word will cause overshoot if((x + space + w > width) && q < E) { S[q++] = s; s = ""; // reset line to its inset value x = 0; // reset its length to zero } 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 } }