/** * Current Account Balances: Graphical Display Applet * @author Robert J Morton UK-YE572246C * @version 12 July 2000, 23 April 2012 * @copyright July 2000 Robert J Morton (all rights reserved) */ // THE MAXIMUM, MINIMUM, AVERAGE AND SWING DISPLAY PANEL import java.awt.*; // for graphics operations (GUI) import javax.swing.*; // swing GUI widgets library public class maxmin extends JPanel { // larger font for displaying the statistics figures private Font font = new Font("Sans", Font.BOLD,14); // main background colour for this panel private static final Color bg = new Color(238,238,238); private String Max = "", // string to hold the maximum balance over the year Min = "", // string to hold the minimum balance over the year Av = "", // string to hold the average balance over the year Sw = ""; // string to hold the swing in the balance over the year void atualizar(int max, int min, int a, int w) { /* Convert the maximum, minimum & average balances and the maximum to minimum swing to strings. */ Max = money(max); Min = money(min); Av = money((int)((double)a / (double)w)); Sw = money(max - min); repaint(); // redisplay the statistics figures } // MONEY DISPLAY FORMATTER (takes integral pence) String money(int x) { double X = (double)x; // Convert integral pence X /= 100; //into decimal pounds Sterling. String s = "" + X; // form it into a string int c = '.'; // decimal point character int i = s.lastIndexOf(c); // find position of decimal point in string if(i == -1) // if no decimal point is found, s += ".00"; // tack on a ".00" pence. else { // Otherwise there is a decimal point, so int e = s.length() - i; /* compute how far the decimal point is from the end of the string. */ if(e == 1) // If it is at the end of the string, s += "00"; // add two zeros after it; else if(e == 2) // if it is the last-but-one character, s += "0"; // add only one zero after it; } // otherwise don't add any zeros. return s; // return the formatted string } public void paint(Graphics g) { // clear the average balance panel to background colour g.setColor(bg); g.fillRect(0,0,350,50); // Display the statistical data in a large black font. g.setColor(Color.black); g.setFont(font); g.drawString("Max",0,22); g.drawString("£" + Max,40,22); g.drawString("Min",0,42); g.drawString("£" + Min,40,42); g.drawString("Average",146,22); g.drawString("£" + Av,220,22); g.drawString("Swing",146,42); g.drawString("£" + Sw,220,42); } }