/** * 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 YEAR-SELECTOR BUTTONS CLASS import java.awt.*; // for graphics operations (GUI) import java.awt.event.*; // for the new-fangled 1.1 event handling import javax.swing.*; // swing GUI widgets library public class selyear extends JPanel { private static final boolean Leap[] = { false, true, false, false, false, true, false, false, false, true, false }; private savlim1 ap; // instance reference of the mail applet private mscale ms; // instance reference of the month scale panel private graph gp; // instance reference of the graph panel selyear(savlim1 ap, mscale ms, graph gp) { this.ap = ap; // copy instance reference of the main applet this.ms = ms; // copy instance reference of the month scale panel this.gp = gp; // copy instance reference of the graph panel setLayout(null); // to allow the control panels to be laid out manually // Create YEAR label, add it to the panel and set its position and size. JLabel yl = new JLabel("YEAR"); add(yl); yl.setBounds(17, 0, 100, 20); ButtonGroup CBG = new ButtonGroup(); //create group for radio buttons //CREATE EACH YEAR SELECTOR RADIO BUTTON AND ADD IT TO THE GROUP JRadioButton CB00 = new JRadioButton("1991",false); CBG.add(CB00); JRadioButton CB01 = new JRadioButton("1992",false); CBG.add(CB01); JRadioButton CB02 = new JRadioButton("1993",false); CBG.add(CB02); JRadioButton CB03 = new JRadioButton("1994",false); CBG.add(CB03); JRadioButton CB04 = new JRadioButton("1995",false); CBG.add(CB04); JRadioButton CB05 = new JRadioButton("1996",false); CBG.add(CB05); JRadioButton CB06 = new JRadioButton("1997",false); CBG.add(CB06); JRadioButton CB07 = new JRadioButton("1998",false); CBG.add(CB07); JRadioButton CB08 = new JRadioButton("1999",false); CBG.add(CB08); JRadioButton CB09 = new JRadioButton("2000",true); CBG.add(CB09); JRadioButton CB10 = new JRadioButton("2001",false); CBG.add(CB10); int k = 20, j = 23, w = 100, h = 15; // ADD BUTTONS TO PANEL AND CREATE AN EVENT LISTENER FOR EACH add(CB00); CB00.setBounds(0,j,w,h); j += k; add(CB01); CB01.setBounds(0,j,w,h); j += k; add(CB02); CB02.setBounds(0,j,w,h); j += k; add(CB03); CB03.setBounds(0,j,w,h); j += k; add(CB04); CB04.setBounds(0,j,w,h); j += k; add(CB05); CB05.setBounds(0,j,w,h); j += k; add(CB06); CB06.setBounds(0,j,w,h); j += k; add(CB07); CB07.setBounds(0,j,w,h); j += k; add(CB08); CB08.setBounds(0,j,w,h); j += k; add(CB09); CB09.setBounds(0,j,w,h); j += k; add(CB10); CB10.setBounds(0,j,w,h); // CREATE A LISTENER FOR EACH YEAR RADIO BUTTON CB00.addItemListener(new selyearlistener( 0,this)); CB01.addItemListener(new selyearlistener( 1,this)); CB02.addItemListener(new selyearlistener( 2,this)); CB03.addItemListener(new selyearlistener( 3,this)); CB04.addItemListener(new selyearlistener( 4,this)); CB05.addItemListener(new selyearlistener( 5,this)); CB06.addItemListener(new selyearlistener( 6,this)); CB07.addItemListener(new selyearlistener( 7,this)); CB08.addItemListener(new selyearlistener( 8,this)); CB09.addItemListener(new selyearlistener( 9,this)); CB10.addItemListener(new selyearlistener(10,this)); } void setYear(int i) { // called only by the listener class below int year = 1991 + i; boolean leap = Leap[i]; // whether or not selected year is a leap year ms.setLeap(leap); // inform month-scale panel gp.setYear(year); // inform graph panel which year has been selected gp.setLeap(leap); // and whether or not it is a leap year ap.loadData(year); // inform main applet which year has been selected } } //LISTENS FOR EVENTS FROM YEAR SELECTOR BUTTONS class selyearlistener implements ItemListener { int id; // one of the above events selyear sy; // the application that called: always the above applet! /* Constructor for a new checkbox event: sets 'id' number pertaining to this instance of 'selyearlistener' and the reference to the instance of the above class from which it came. (there will only be one instance anyway). */ public selyearlistener(int id, selyear sy) { this.id = id; this.sy = sy; } public void itemStateChanged(ItemEvent e) { switch(id) { // an event has occurred from button 'id' /* Execute the method in the above applet that deals with the radio-button on whose instance of 'selyearlistener' this method was invoked. */ case 0: sy.setYear( 0); break; case 1: sy.setYear( 1); break; case 2: sy.setYear( 2); break; case 3: sy.setYear( 3); break; case 4: sy.setYear( 4); break; case 5: sy.setYear( 5); break; case 6: sy.setYear( 6); break; case 7: sy.setYear( 7); break; case 8: sy.setYear( 8); break; case 9: sy.setYear( 9); break; case 10: sy.setYear(10); break; } } }