/** * Name & Address Applet * @author Robert John Morton * @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 javax.swing.*; // the Swing GUI system import java.awt.*; // abstract windowing toolkit import java.awt.image.BufferedImage; public class address extends JPanel implements Runnable { private boolean FTT = true; // 'fist time through' flag. int ls = 0; /* Load Switch says from where to load image and data files: 0 = from local directory 1 = from jar file 2 = from remote server */ private int X = 185, // width of display window (in pixels) Y = -4, // height of display window (in pixels) V = 24, // vertical displacement of image areas XE = 515, // Horizontal extent of window and JFrame. 513 + 2 YE = 328, // Vertical extent of window and JFrame. 303 + 25 k = 0, // plot number AdrNum = 0; /* Address Number of address whose post area is currently being displayed. */ private long p = 4000, // inter cycle wait time 4 seconds t; // System Time that time current cycle is due to end private BufferedImage win1, // reference for an off-screen image buffer win2, // reference for an off-screen image buffer win3, // reference for an off-screen image buffer map; // reference for coastline and post areaa map BufferedImage img; // reference for loaded background image private Graphics2D //graphics contexts for the off-screen image ga, // name and company gb, // reference number gc, // name and address gd; // UK coastline private String cb = "", // code base pa, // two-letter post area 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"} }; private imgldr il; // instance of the image loader private Thread T; // declare a thread reference variable address(int XE, int YE, int ls, String cb) { this.XE = XE; this.YE = YE; this.ls = ls; this.cb = cb; /* Create, and get the graphics context for 4 off-screen buffered images for 1) name + company field, 2) Reference Number field, 3) the name and address and 4) the UK coastline map. */ win1 = new BufferedImage(403, 16,BufferedImage.TYPE_INT_RGB); win2 = new BufferedImage( 45, 16,BufferedImage.TYPE_INT_RGB); win3 = new BufferedImage(300,160,BufferedImage.TYPE_INT_RGB); map = new BufferedImage(300,160,BufferedImage.TYPE_INT_RGB); gc = win3.createGraphics(); gb = win2.createGraphics(); ga = win1.createGraphics(); gd = map.createGraphics(); // paint the UK map coastline onto this image uk_coast.paint(gd,Color.green,X + 80,Y + 80); // set background colour for while image is loading setBackground(Color.black); /* create a loader instance for the background image, which will start automatically to load the background image. */ il = new imgldr(this,ls,cb); // Set the System Time at which the following time frame must end. t = System.currentTimeMillis() + p; T = new Thread(this); // create a run() thread T.start(); // and start it running } public void run() { while(T != null) { // permanent loop broken by external event if(il.finished()) runloop(); // update the display /* get time remaining in cycle time frame, imposing a madatory mini- mum sleep time of 5 milliseconds. Then sleep for the remainder of the time frame, allowing external events to interrupt the thread. After sleeping, set System Time at which the next time frame will end. */ long s = t - System.currentTimeMillis(); if(s < 5) s = 5; try {Thread.sleep(s);} catch(InterruptedException e){} t = System.currentTimeMillis() + p; } } private void runloop() { if(FTT) { // if first time through System.out.println("Image loaded."); System.out.println("Running ..."); FTT = false; } else { atualizar(); // update the off-screen images repaint(); // update the display /* if just displayed the last address in the list, cycle back to the beginning of the list again. */ if(++AdrNum >= S.length) AdrNum = 0; } } public void paint(Graphics g) { g.drawImage(img,0,0,this); // draw the static background image 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 } private void atualizar() { // paint the blank UK coast line onto the off-screen map image gc.drawImage(map,0,0,this); /* get the two-letter post area code for this address and then paint in this post area onto the off-screen map image. */ pa = S[AdrNum][7].substring(0,2); uk_areas.paintMap(gc,Color.yellow,pa,X+80,Y+80); // Paint the name and address onto the off-screen image ing [gc] gc.setColor(Color.white); // text colour int sx = 3, // x-pixel for current line sy = 16; // y-pixel for current line // Draw the text string for each line of the name and address for(k = 0; k < 9; k++, sy += 16) { gc.drawString(S[AdrNum][k],sx,sy); 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 the post area letters gc.setColor(Color.white); // then reset text colour to white } } /* Paint the name + company field onto off-screen image win1 [ga] */ 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 ga.drawString( S[AdrNum][0] // show the person's name + ", " // add the comma + 2 spaces + S[AdrNum][2],3,11 // then append the company name ); /* Paint the Reference Number field onto the off-screen imagewin2 [gb] */ gb.setColor(Color.white); // set background colour for Ref field gb.fillRect(0,0,50,18); // clear the Ref field gb.setColor(Color.black); // set text colour // convert the integer to a string String s = String.valueOf(AdrNum + 1); // add in the ref number with leading zeros int i = s.length(); if(i > 5) i = 5; gb.drawString(Zeros[i - 1] + s,3,11); } }