/** * Name & Address Applet * @author Robert John Morton UK-YE572246C * @version 29 July 1998 */ /* This applet is a rolling demonstrator the real Name & Address applet which is part of the my Personal Link package. This demonstrator applet contains some of the actual functionality of the real applet, including the map coastline and post area display functions. */ import java.awt.*; public class address extends java.applet.Applet implements Runnable { int X = 185, // dimensions of display window (in pixels) Y = -4, V, // vertical displacement of image areas k = 0, // plot number AN = 0; // Number of address whose post area is being displayed long TF = 200, // inter cycle wait time T; // time a new cycle is due to begin boolean error = false, // image load error flag loading = true, // image loading in progress shown = false; // true when background image has been displayed // for the first time. Image win1, win2, win3, // reference for an off-screen image buffer img, map; // reference for loaded background image Graphics ga, gb, gc, gd; // graphics context for the off-screen image Thread TH; // declare a thread reference variable String Zeros[] = {"0000","000","00","0",""}, S[][] = { {"Ms Tara MacGreggor", "Field Service Manager", "Upland Networks Ltd", "Grants Farm Industrial Estate", "Rockall Road", "KINCODDY", "Angus", "DD14 8NB", "UK"}, {"Mr Stanley Trunnion", "Design Engineer", "Alternative Systems Ltd", "Solartron House", "16 Earman Street", "ARDWORK", "Nothumberland", "TS12 4QC", "UK"}, {"Ms Dorothy Ramsbottom", "Support Engineer", "Dork Networks Ltd", "Braithwait House", "23 Mill Street", "DINKLEY", "Lancashire", "BB25 9QR", "UK"}, {"Mr Jack Parkhurst", "Software Engineer", "Fortex Control Systems plc", "Fortune House", "119 Harpington Road", "LEXTONBURY", "Leicestershire", "LE15 4ET", "UK"}, {"Mrs Josephine Early", "Spectral Analyst", "Ecofield Research Ltd", "Hempton Farm", "Housefield Road", "HEDGINGTON", "Herefordshire", "HR23 2JQ", "UK"}, {"Miss Elizabeth Rook", "Communications Specialist", "Masthead Communications Ltd", "Enterprise House", "23, Gaston Business Park", "RADDINGTON", "Hampshire", "SO15 4SQ", "UK"}, {"Mr R S Hesslip", "Consultant", "Port Cullis Security Ltd", "Knox House", "19, Thorburn Street", "STORTFORD", "Hertfordshire", "CM23 3NR", "UK"}, {"Mr George Harry", "Consultant", "Elton Enterprises", "Pearly House", "23, Byle Street", "SUNHILL", "London", "N 15 4ST", "UK"}, {"Mr Paddy McIrvine", "Systems Engineer", "Neigh Productions Ltd", "River Farm", "17 Londonderry Road", "FERRISCREE", "Northern Ireland", "BT17 2QZ", "UK"}, {"Ms Catriona McTavish", "Researcher", "Sutherland Productions Ltd", "High Croft", "3 Ross Road", "PEERS GLEN", "Cromarty", "IV9 4TY", "UK"} }, imgname, //image file name pa; //two-letter post area string public void init() { /* Create 3 images in which to build the name + company field, the Ref number field and name & address + map and the map's coastline getting the graphics context reference for each. */ win1 = createImage(403, 16); ga = win1.getGraphics(); win2 = createImage( 45, 16); gb = win2.getGraphics(); win3 = createImage(300, 160); gc = win3.getGraphics(); map = createImage(300, 160); gd = map.getGraphics(); // paint the map coastline onto the last image uk_coast.paint(gd, Color.green, X + 80, Y + 80); // set applet background colour for while image is loading. setBackground(Color.black); // Get the following applet tag parameters: imgname = getParameter("img"); //the file name of the image file // the vertical displacement of the drawing images V = Integer.parseInt(getParameter("V")); // the background image of the proposed real applet img = getImage(getDocumentBase(), imgname); // Set the finishing time of the first time frame. T = System.currentTimeMillis() + TF; } /* Paint the whole thing when the image has just finished loading or whenever the window has just become unelcipsed. */ public void paint(Graphics g) { /* If image could not be loaded successfully, show an error message in black on a red background. */ if(error) { Dimension d = getSize(); g.setColor(Color.red); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.black); g.drawString("Image not found: " + imgname, 10, d.height/2); // Else draw the static background image and the window infills } else { g.drawImage(img,0,0,this); if(shown) showInfill(g); } } public boolean imageUpdate( Image img, int flags, int x, int y, int w, int h) { error = ((flags & ABORT) != 0); // image not acquired loading = ((flags & (ALLBITS | ABORT)) == 0); return loading; // whether or not still loading image } public void update(Graphics g) { // (re)draw the screen image if(loading) return; // exit if image still loading pa = S[AN][7].substring(0,2); // two-letter post area code gc.drawImage(map, 0, 0, this); // lay in the blank coastline map // Fill in the appropriate post area with solid colour. uk_areas.paint(gc,Color.yellow,pa,X + 80,Y + 80); displayNameAndAddress(); // Display the name and address displayNameAndRefFields(); // Display name/company and the 'Ref' field if(shown) // if background image has been displayed, showInfill(g); // show the 3 window infills. /* Else, display it, set flag to verify that it has been displayed, increase the size of the time frame and the time remaining in the current time frame. */ else { g.drawImage(img,0,0,this); shown = true; TF = 2500; T += 2300; } } void displayNameAndAddress() { // Display the name and address gc.setColor(Color.white); // text colour int sx = 3, sy = 16; // y-pixel for current line // for each line of the name and address for(k = 0; k < 9; k++, sy += 16) { gc.drawString(S[AN][k],sx,sy); // draw the text string if(k == 7) { // If this is the postcode line, gc.setColor(Color.yellow); // change the text colour to yellow, gc.drawString(pa, sx, sy); // highlight post area letters in yellow gc.setColor(Color.white); // then reset text colour back to white. } } } // Display the name + company field and the Ref field void displayNameAndRefFields() { ga.setColor(Color.white); // background colour of name+company field ga.fillRect(0, 0, 403, 18); // clear the field ga.setColor(Color.black); // set text colour /* Show the person's name then add a comma and two spaces and append the company name. */ ga.drawString(S[AN][0] + ", " + S[AN][2],3,11); // Fill the 'Ref' field with its background colour gb.setColor(Color.white); gb.fillRect(0,0,50,18); // Get the 'Ref' number as an integer and convert it to a string String s = String.valueOf(AN + 1); int i = s.length(); if(i > 5) i = 5; /* Set the 'Ref' field's foreground colour and then print the 'Ref' number complete with leading zeros. */ gb.setColor(Color.black); gb.drawString(Zeros[i - 1] + s,3,11); } void showInfill(Graphics g) { // Display the content of the 3 windows: g.drawImage(win1, 14, V + 11, null); // name + company image g.drawImage(win2, 454, V + 11, null); // Ref number image g.drawImage(win3, 184, V + 54, null); // main name/address/map image } public void run() { while(TH != null) { // permanent loop while thread is alive /* Get the time remaining in the current time frame, imposing a minimum of 5 milliseconds, then put the thread to sleep for this remaining time. Catch any interrupt exceptions that occur while the thread is asleep but do nothing about them because they will be external events from the browser. */ long s = T - System.currentTimeMillis(); if(s < 5) s = 5; try { TH.sleep(s); } catch (InterruptedException e) { } // Set the finishing time of next time frame. T = System.currentTimeMillis() + TF; // If done the last address, loop back to the first one. if(++AN >= S.length) AN = 0; repaint(); //sets up a call to update() } } /* Start the program thread by creating a thread object and starting it running. This method returns a call to run(). */ public void start() { TH = new Thread(this); TH.start(); } public void stop() { TH=null; } // Stop program thread. }