/** * 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.net.*; // for downloading data from the remote server public class img2panel extends JPanel { // MESSAGE DISPLAY PANEL /* Declare two fonts for the lettering used on this panel. The first is for the mesages summary number and the second is for the URL. */ private static final Font font1 = new Font("Sans",Font.PLAIN,18), font2 = new Font("Sans",Font.PLAIN,12); private Color bg; // background colour for the message panel private String msg = ""; // string to hold the displayed message text private search hf; // instance reference of the search applet private boolean // WHEN SET 'TRUE', THESE FLAGS INDICATE THAT redMsg = false, // the message must be displayed in red lettering imagesLoaded = false, // the background images have been loaded smallFont = true; // messgae must be printed in the small font size private int W, H, // width and height of message panel h, // height of the text base-line common h1, // height of the text base-line for big font h2; // height of the text base-line for small font img2panel(int W, int H, Color bg, search hf) { // INSTANCE CONSTRUCTOR this.W = W; // set width of message panel to local variable this.H = H; // set height of message panel to local variable this.bg = bg; // set panel's background colour to local variable this.hf = hf; // copy applet's instance reference to local variable /* compute the baseline height 'h' for each font for lettering within a panel of height 'H' */ FontMetrics fm1 = getFontMetrics(font1); h1 = fm1.getAscent() + (H - fm1.getHeight()) / 2; FontMetrics fm2 = getFontMetrics(font2); h2 = fm2.getAscent() + (H - fm2.getHeight()) / 2; } void setImagesLoaded() {imagesLoaded = true;} // NORMAL MESSAGES (INCLUDING ERROR MESSAGES) void showMsg(String msg, boolean redMsg) { this.msg = msg; // copy message and its display colour switch this.redMsg = redMsg; // to local variables if(redMsg) // A red error message tends to be long, smallFont = true; // so display it in the small font, else // otherwise it is a normal message smallFont = false; // so display it in the large font. repaint(); // schedule a repaint via the event despatching thread } // SHOW THE URL OF THE HTML PAGE FROM WHICH THIS SUMMARY WAS RETRIEVED void showURL(URL url) { msg = "" + url; // convert the URL to a string redMsg = false; // URLs do not appear in red smallFont = true; // they tend to be long, so use a small font repaint(); // schedule a repaint via the event despatching thread } // DISPLAY THE MESSAGE IN THE MESSAGE PANEL public void paint(Graphics g) { if(imagesLoaded) // Provided the window background images g.drawImage(hf.IMG2,0,0,this); // have all been loaded, draw them else { // onto the applet canvas, otherwise set g.setColor(bg); // background colour for message dislpay g.fillRect(0,0,W,H); // area and wipe it of any previous text. } if(smallFont){ // If using the small font, g.setFont(font2); // set it as the active font h = h2; // set its baseline height as active baseline height } else { // Otherwise, g.setFont(font1); // set the large font as the active font h = h1; // set its baseline height as active baseline height } /* , . */ if(redMsg) // If message must be displayed in red g.setColor(Color.red); // set text's foreground colour to 'red' else // otherwise g.setColor(Color.black); // set it to 'black' g.drawString(msg, 5, h); // draw the message string } }