/** * Current Account Balances: Graphical Display Applet * @author Robert J Morton UK-YE572246C * @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 // number of horizontal pixels per year private static final int H = 5; // inset to start of year-annotation private int SW[] = new int[decade.length]; 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 (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] = (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, display decade label and decade boundary mark then jump across 10 years. */ for(int i = 0; i < decade.length; i++) { g.drawString(decade[i], SW[i] + w, 20); g.drawLine(w, 0, w, 5); w += 10 * H; } 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;} }