/** * 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 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 // array to hold the insets of the month-name strings private int SW[] = new int[12]; // true when the currently selected year is a leap year private boolean leap = false; private static final int //number of days in each month days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; private static final String //abbreviated names of the months month[] = { "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" }; mscale() { // INSTANCE CONSTRUCTOR fm = getFontMetrics(font); // get the 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; } /* Set by selyear: leap = true if a leap year is selected; repaint the month scale. */ void setLeap(boolean b) { leap = b; repaint(); } // Called by graph panel: returns number of days in month 'i' int getDays(int i) { return days[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 } }