/** * Current Account Balances: Graphical Display Applet * @author Robert J Morton * @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 int SCALE[] = {0,4,2,6,9,7,3,1,10,5,11,5}; 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 year from start of graph which is first one 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 sf; // scaling factor // data input stream that holds the downloaded account data private DataInputStream D; private graphs ap; // instance of main panel trace(graphs ap, horiz HZ, selfile SF, int v) { // INSTANCE CONSTRUCTOR this.ap = ap; d = HZ.getDacades(); int // scaling divisors SD[] = {25000,12500,5000,2500,1250,500,25000,25000,125,25,2500,25000}, // Y-bias for when Y-scale starts at less than zero YB[] = {0,0,0,0,0,0,40,120,0,40,0,0}; sf = SCALE[v]; // get the scale indicator parameter 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; showac = true; // show only the main (non-inflation-corrected) trace } else { // if a money-trace were selected showic = true; // show only the inflation-corrected trace by default showac = false; } } public void paint(Graphics g) { // DRAW THE VERTICAL AND HORIZONTAL GRATICULE LINES g.setColor(bg2); // set graph background colour g.fillRect(0,0,201,201); // clear the graph area g.setColor(bg3); // set graticule colour int i, // horizontal variable q = H * 10, // horizontal increment L = d * q; // number of decades covered * q for(i = 0; i < L; i += q) // for each decade g.drawLine(i,0,i,200); // draw vertical graticule line 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 horizontal graticule line if(ap.loading()) return; // exit if no data to display // PLOT THE GRAPH try { // try for IOException D.reset(); // reset byte array pointer to start of data boolean pr = false, // logic latch for start of valid real plot pi = false; // logic latch for start of valid inflated plot 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; // initial year number try { // try for EOFException while(true) { // infinite loop broken only by try failure [EOF] double // Read next input integer into x = (double)D.readInt(); // a floating-point double. int ph = p - H, // previous value of p r = (int)(x / sd), // real current value scaled to pixels y = Y - Yb - r; // convert to reverse vertical pixel position /* Miss out first value because we are drawing from it. Set trace colour to blue. Draw the horizontal part of the previous year's value, the vertical join to this year's value and the horizontal part of this year's value. */ if(w > 0 && pr && x != 0 && showac) { g.setColor(fg1); g.drawLine(ph-H,py,ph,py); g.drawLine(ph,py,ph,y); g.drawLine(ph,y,p,y); } py = y; // note income for next pass if(r != 0) // logic latch for start of valid plots pr = true; /* Find the amount in £2K, scale it to pixels and convert it to a reverse vertical pixel position. */ i = (int)(infln.getValue(x, w) / sd); int iy = Y - Yb - i; /* Skip the first pass because we are drawing from the first value. Set the trace colour to cyan. Draw the horizontal part of the previous year's value, the vertical join to this year's value and the horizontal part of this year's value. */ if(w > 0 && pi && x != 0 && showic) { g.setColor(fg2); 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) // logic latch for start of valid plots pi = true; 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(loader ld){ D = new DataInputStream(new ByteArrayInputStream(ld.getArray())); } 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(); } int getScale(){return sf;} }