/** * 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 HORIZONTAL MONTH SCALE IS DISPLAYED import java.awt.*; // for graphics operations (GUI) import javax.swing.*; // swing GUI widgets library public class horiz extends JPanel { private static final String decade[] = {"1960s","1970s","1980s","1990s"}; // decade labels private static final int H = 5; // number of horizontal pixels per year private int SW[] = new int[decade.length]; // inset to start of year-annotation private Font font = new Font("Sans",Font.BOLD,13); horiz() { // INSTANCE CONSTRUCTOR FontMetrics fm = getFontMetrics(font); // measurements of the font for(int i = 0; i < decade.length; i++) //for each of the 12 months /* Inset each month-name by the number of days in the month (1 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] = (H * 10 - fm.stringWidth(decade[i]) + 1) / 2; } public void paint(Graphics g) { g.setFont(font); g.setColor(Color.black); // set pen (foreground) colour int w = 0; // set to first year // for each year in current decade for(int i = 0; i < decade.length; i++) { g.drawString(decade[i],SW[i] + w,20); // decade label g.drawLine(w,0,w,5); // decade boundary mark w += 10 * H; // jump across 10 years } g.drawLine(w,0,w,5); // draw final year boundary mark g.drawLine(0,0,w,0); // draw horizontal axis } int getDacades() {return decade.length;} }