/** * Current Account Balances: Graphical Display Applet * @author Robert J Morton * @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); private static final Color bg = new Color(238,238,238); // main background colour for this panel 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) { Max = money(max); // convert the maximum balance figure to a string Min = money(min); // convert the minimum balance figure to a string Sw = money(max - min); // convert the swing figure to a string Av = money((int)((double)a/(double)w)); // convert the average repaint(); // redisplay the statistics figures } String money(int x) { // MONEY DISPLAY FORMATTER (takes integral pence) double X = (double)x; X /= 100; // form into decimal pounds String s = "" + X; // form into a string int c = '.'; // decimal point character i = s.lastIndexOf(c); // find position of decimal point in string if(i == -1) // if no decimal point found s += ".00"; // tack on a .00 pence else { // otherwise there is a decimal point, so int e = s.length() - i; // how far decimal point is from end of 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 you don't add any zeros return s; // return the formatted string } public void paint(Graphics g) { g.setColor(bg); g.fillRect(0,0,350,50); // clear the average balance panel g.setColor(Color.black); // display average balance for the year in black g.setFont(font); // print the statistics data in black 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); } }