/** * Current Account Balances: Graphical Display Applet * @author Robert J Morton UK-YE572246C * @version 12 July 2000, 27 April 2012 * @copyright July 2000 Robert J Morton (all rights reserved) */ // THE PANEL ON WHICH THE ACCOUNT GRAPH IS DISPLAYED import java.awt.*; // for graphics operations (GUI) import javax.swing.*; // swing GUI widgets library import java.io.*; // for stream handling for the above public class trace extends JPanel { private static final Color bg2 = new Color(112,112,112), // graph background colour bg3 = new Color(160,160,160), // graticule colour fg1 = new Color(255,255,0), // uninflated fg2 = new Color(0,255,255); // inflated private boolean showac = false, // show the uninflated account showic = true; // show inflation-corrected account private static final int Y = 200, // vertical bias from top edge of applet to start of y-axis H = 5, // number of horizontal pixels per year FYB = 6; // number of the year from the start of the graph which // is the first year for which there is data. private int d, // number of decades displayed w = 0, // year counter used on horizontal axis of graph sd, // scaling divisor in use Yb; // Y-bias for when Y-scale starts at less than zero private DataInputStream D; // data input stream that holds // the downloaded account data trace(graphs ap, horiz HZ) { // INSTANCE CONSTRUCTOR d = HZ.getDacades(); int SD[] = { 25000,12500,5000,2500,1250,500, // scaling divisors 25000,25000,25,25,2500,25000 }, YB[] = { 0,0,0,0,0,0,40, // Y-bias for when Y-scale 120,0,40,0,0, // starts at less than zero }, // get the scale indicator parameter sf = Integer.valueOf(ap.getParameter("scale")).intValue(); sd = SD[sf]; // set up the appropriate scaling divisor Yb = YB[sf]; // Y-bias for when Y-scale starts at less than zero if(sf > 9) { // If a non-money scale were specified, showic = false; // show only the main showac = true; // (non-inflation-corrected) trace. } else { // else, a money-trace was selected showic = true; // so show only the inflation-corrected showac = false; // trace by default. } } public void paint(Graphics g) { //DRAW THE VERTICAL AND HORIZONTAL GRATICULE LINES // Clear the graph area to its background colour. g.setColor(bg2); g.fillRect(0,0,201,201); int // Create the i, // horizontal variable q = H * 10, // horizontal increment L = d * q; // number of decades covered * q g.setColor(bg3); // set graticule colour for(i = 0; i < L; i += q) // Draw a vertical graticule g.drawLine(i,0,i,200); // line for each decade. g.drawLine(i,0,i,200); // draw final vertical graticule line for(int j = 0; j < 240; j += 40) // for each £10,000 graduation, g.drawLine(0, j, i, j); // draw a horizontal graticule line. //PLOT THE GRAPH try { // try for IOException D.reset(); // reset byte array pointer to start of data boolean pr = false, // logic latches for pi = false; // start of valid plots int py = 0, // y-value of previous real plot piy = 0, // y-value of previous inflated plot p = FYB * H; // horizontal pixel position of start of graph w = 0; // the initial year number try { // try for EOFException while(true) { // infinite loop broken only by try failure //read the next input integer into a floating-point double double x = (double)D.readInt(); int ph = p - H, // the previous value of 'p' r = (int)(x / sd), // the real current value scaled to pixels y = Y - Yb - r; //'r' converted to reverse vertical pixel // position // Omit the first value because we are drawing FROM it. if(w > 0 && pr && x != 0 && showac) { g.setColor(fg1); // set the trace colour to blue // Draw horizontal part of previous year's value g.drawLine(ph - H,py,ph,py); g.drawLine(ph,py,ph,y); // its vertical join to this year's value g.drawLine(ph,y,p,y); // horizontal part of this year's value } py = y; // note income for next pass if(r != 0) // set logic latch pr = true; // for start of valid plots /* Find the amount in £2K and scale it to pixels then convert it to a reverse vertical pixel position. */ i = (int)(infln.getValue(x, w) / sd); int iy = Y - Yb - i; // Omit the first value because we are drawing FROM it. if(w > 0 && pi && x != 0 && showic) { g.setColor(fg2); // set the trace colour to cyan /* Draw the horizontal part of the previous year's value plus its vertical join to this year's value and horizontal part of this year's value. */ g.drawLine(ph - H,piy,ph,piy); g.drawLine(ph,piy,ph,iy); g.drawLine(ph,iy,p,iy); } piy = iy; // note inflation-corrected income for next pass if(i != 0) // set logic latch pi = true; // for start of valid plots w++; // increment the year number p += H; // advance to next year's horizontal pixel position } } catch(EOFException f) { // terminate loop when attempt is made } // to read past end of byte array } catch(Exception e) { // readByte() can throw an IOException } // or ArrayOutOfBoundsException } void setD(DataInputStream D) { this.D = D; } void setTrace(int i) { if(i == 0) { showac = true; showic = false; } else if(i == 1) { showac = false; showic = true; } else { showac = true; showic = true; } repaint(); } }