/** * 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 PANEL ON WHICH THE HORIZONTAL MONTH SCALE IS DISPLAYED import java.awt.*; // for graphics operations (GUI) import javax.swing.*; // swing GUI widgets library public class mscale extends JPanel { // font for month-name lettering private Font font = new Font("Sans",Font.BOLD,12); private FontMetrics fm; // dimensions of the above font private int SW[] = new int[12]; // array to hold the insets // of the month-name strings private boolean leap = false; // true when the currently selected // year is a leap year private static final int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; private static final String month[] = { "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" }; mscale() { // INSTANCE CONSTRUCTOR fm = getFontMetrics(font); // measurements of the font for(int i = 0; i < 12; i++) // for each of the 12 months /* Inset each month-name by the number of days in the month (one day per pixel) minus the string-width of the month name (in pixels) plus 1 to counter possible rounding down all divided by 2. */ SW[i] = (days[i] - fm.stringWidth(month[i]) + 1) / 2; } void setLeap(boolean b) { // set by selyear leap = b; // true if a leap year is selected repaint(); // repaint the month scale } int getDays(int i) { // called by graph panel return days[i]; // return number of days in month i } public void paint(Graphics g) { int w = 0; // set to first day of the year g.setFont(font); // font for month scale g.setColor(Color.black); // colour for month scale for(int i = 0; i < 12; i++) { // for each month shown int d = days[i]; // get number of days in this month if(d == 1 && leap) d++; // add a day if leap year february g.drawString(month[i],w + SW[i],20); // draw month name g.drawLine(w,0,w,5); // month boundary mark w += d; // accumulated number of days } g.drawLine(w,0,w,5); // final year boundary mark g.drawLine(0,0,w,0); // horizontal axis } }