/* timeSelector.java Paul Carlisle This is a control panel which allows the user to set the time. Time is based, for display purposes, on a twelve-hour, AM/PM clock. */ import java.awt.*; import java.util.*; import java.lang.*; public class timeSelector extends Panel { // Time is stored as a fraction of a twenty-four hour day; // For example, 1:00 pm = 0.5416667 private double currentTime; // Drop-down lists for the digits. private Choice hours, minutesHi, minutesLow; // ... and for AM/PM. private Choice AmPm; private GridBagLayout selectorLayout; private GridBagConstraints selectorConstraints; private int Width; private boolean gotMessage = false; public timeSelector(double time, Font inputFont, Font checkFont, Font labelFont, int width) { Width = width; setBackground(Color.lightGray); // Set layout formatting, color and font. //selectorLayout = new GridLayout(1,0,0,0); selectorLayout = new GridBagLayout(); selectorConstraints = new GridBagConstraints(); setLayout(selectorLayout); setBackground(Color.lightGray); setFont(inputFont); // Instantiate the drop-down lists, and set their background // color to white. hours = new Choice(); hours.setBackground(Color.white); minutesHi = new Choice(); minutesHi.setBackground(Color.white); minutesLow = new Choice(); minutesLow.setBackground(Color.white); AmPm = new Choice(); AmPm.setBackground(Color.white); // Hours digits can be in range 1 through 12. for (int i = 1; i <= 12; i++) hours.addItem(new String(Integer.toString(i)) ); // Hi digit of minutes can only be 0 through 5. for (int i = 0; i <= 5; i++) minutesHi.addItem(new String(Integer.toString(i)) ); // Low digit of minutes can be 0 through 9. for (int i = 0; i <= 9; i++) minutesLow.addItem(new String(Integer.toString(i)) ); // The AM/PM selector. AmPm.addItem("AM"); AmPm.addItem("PM"); // Resize, and add to the panel. resize(Width, 50); selectorConstraints.gridy = 0; selectorConstraints.anchor = GridBagConstraints.CENTER; selectorConstraints.fill = GridBagConstraints.BOTH; Panel pad = new Panel(); selectorConstraints.gridx = 0; selectorLayout.setConstraints(pad,selectorConstraints); add(pad); selectorConstraints.gridx = 1; selectorLayout.setConstraints(hours,selectorConstraints); add(hours); Label colon = new Label(":", Label.CENTER); selectorConstraints.gridx = 2; selectorConstraints.fill = GridBagConstraints.NONE; selectorLayout.setConstraints(colon, selectorConstraints); add(colon); selectorConstraints.gridx = 3; selectorConstraints.fill = GridBagConstraints.BOTH; selectorLayout.setConstraints(minutesHi,selectorConstraints); add(minutesHi); selectorConstraints.gridx = 4; selectorLayout.setConstraints(minutesLow,selectorConstraints); add(minutesLow); selectorConstraints.gridx = 5; selectorLayout.setConstraints(AmPm,selectorConstraints); add(AmPm); // Set the display to the selected year. setTime(time); } // Handles user manipulation of the display. public boolean action(Event e, Object what) { double temp = currentTime; // If the user changed a digit... if ( (e.target == hours) || (e.target == minutesHi) || (e.target == minutesLow) || (e.target == AmPm) ) { // Read in all the digit settings, and form a new // numeric time value from them. // Get numeric value of hour. double tempHours = hours.getSelectedIndex() + 1; // Get numeric value of minute. double tempMinutes = (double)(minutesHi.getSelectedIndex() * 10 + minutesLow.getSelectedIndex() ) / 60; temp = tempHours + tempMinutes; // Adjust for Am/Pm selection. if ( (AmPm.getSelectedItem() == "PM") && (tempHours < 12) ) temp = temp + 12; else if ( (AmPm.getSelectedItem() == "AM") && (tempHours == 12) ) temp = temp - 12; temp = temp / 24; } // Set the year, and reset the display. setTime(temp); gotMessage = true; // Pass on all events. return false; } public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } // Returns the numeric value of the time displayed, as a fraction // of a twenty-four hour day. This will be a number between 0 and // not-quite-one. public double getTime() { return currentTime; } // Sets the display to reflect the time value. // WARNING! NO ERROR CHECKING! // Value submitted should be in range 0 <= time < 1. public void setTime(double time) { // Set the digits to correspond to the passed value. // Forenoon. We treat Midnight as AM. if (time < 0.5) AmPm.select("AM"); else AmPm.select("PM"); // Calculate and set the value of the hour digits. int tempHours = (int)(time * 24 + 1e-7); if (tempHours > 12) tempHours = tempHours - 12; if (tempHours == 0) hours.select(11); else hours.select(tempHours - 1); // Calculate and set the value of the minute digits. double temp = (time * 24) - (int)(time * 24); int tempMinutes = (int)Math.round(temp * 60) % 60; minutesHi.select(tempMinutes / 10); minutesLow.select(tempMinutes % 10); currentTime = time; } // Reduce flickering on redraws. public void update(Graphics g) { paint(g); } }