/* MoonCalendar.java Paul Carlisle - November 13, 1997 The main program. Assembles and controls the three main program elements: the calendar, help and detail panel. Also determines user's screen resolution, and what fonts to use throughout the program. */ import java.awt.*; import java.applet.*; import java.net.*; import java.lang.*; import java.util.*; import DateConstants; import JDate; import Calendar; import textHelpPanel; import detailPanel; public class MoonCalendar extends Applet { private int displayWidth; private int displayHeight; private JDate currentDate; private Calendar cal; private Font weekdayFont, daycellFont, inputFont; private Calendar calendarGrid; private Panel mainPanel; private CardLayout mainLayout; private textHelpPanel helpPanel; private detailPanel detPanel; private String currentPanel; private boolean help; private Image moonImage; private MediaTracker tracker; private Thread detailThread; // Experiment. public boolean handleEvent(Event e) { if ( (e.id != Event.ACTION_EVENT) && (e.id != Event.MOUSE_DOWN) ) return true; // See who generated this event. if (calendarGrid.getMessage() == true) { if (calendarGrid.getMessageID() == Calendar.DATE_SELECTED) { // Need to flip to detail screen here. // Save the time from the current date. double temp = currentDate.getFracTime(); // Get the selected date from the calendar grid. // This will be a day, with time set to midnight. JDate tempJD = new JDate(); tempJD = calendarGrid.getSelectedDate(); // Add the time back to the JD. currentDate.setJD(tempJD.getJD() + temp); detPanel.setDate(currentDate); detPanel.setControls(); currentPanel = "Detail"; detPanel.redraw(true); detPanel.thread = null; detPanel.thread = new Thread(detPanel); detPanel.thread.start(); mainLayout.show(mainPanel, currentPanel); } else if (calendarGrid.getMessageID() == Calendar.HELP_REQUESTED) { help = true; currentPanel = "Calendar"; mainLayout.show(mainPanel, "Help"); } } else if (helpPanel.getMessage() == true) { if (currentPanel == "Detail") { detPanel.thread = null; detPanel.thread = new Thread(detPanel); detPanel.setControls(); detPanel.thread.start(); } help = false; mainLayout.show(mainPanel, currentPanel); } else if (detPanel.getMessage() == true) { if (detPanel.getMessageID() == detailPanel.CALENDAR) { currentDate.setJD( (detPanel.getDate()).getJD() ); detPanel.thread.stop(); currentPanel = "Calendar"; detPanel.setControls(); calendarGrid.redraw(currentDate); mainLayout.show(mainPanel, currentPanel); } else if (detPanel.getMessageID() == detailPanel.HELP_REQUESTED) { detPanel.thread.stop(); help = true; currentPanel = "Detail"; mainLayout.show(mainPanel, "Help"); } } // Flush any pending messages out. calendarGrid.getMessage(); helpPanel.getMessage(); detPanel.getMessage(); return true; } public void init() { // Experiment - see how Properties work... System.out.println("Properties: " + System.getProperty("java.version")); // Call zero-arg constructor to set date to system's // idea of date. currentDate = new JDate(); // Find resolution of user's screen. Dimension d = new Dimension(); d = getToolkit().getScreenSize(); // Resize to fit in common browser window. displayHeight = (3 * d.height) / 4; displayWidth = (3 * displayHeight) / 2; // OK, that didn't work, because Netscape doesn't honor sizing request. // Try explicitly setting the size from the applet boundaries. d = size(); displayWidth = d.width; displayHeight = d.height; // And finally, give up and declare the size explicitly. displayWidth = 675;//450; displayHeight = 450;//300; setBackground(Color.lightGray); // Set fonts. weekdayFont = new Font("Helvetica", Font.PLAIN, 12); daycellFont = new Font("Helvetica", Font.PLAIN, 8); inputFont = new Font("Helvetica", Font.BOLD, 16); // Create a panel to hold the "cards" we want to display. mainPanel = new Panel(); mainLayout = new CardLayout(); mainPanel.setLayout(mainLayout); // Get the URL from which this applet was loaded. URL home = getCodeBase(); AppletContext context = getAppletContext(); helpPanel = new textHelpPanel(displayWidth, displayHeight, inputFont, home, context); mainPanel.add("Help", helpPanel); // Create the calendar card. calendarGrid = new Calendar(currentDate, displayWidth, displayHeight, helpPanel, daycellFont, weekdayFont, inputFont); // Add it to the deck. mainPanel.add("Calendar", calendarGrid); // Add the deck to the main display. add(mainPanel); help = false; currentPanel = "Calendar"; mainLayout.show(mainPanel, currentPanel); show(); // Get the image to display on the detail screen. tracker = new MediaTracker(this); context.showStatus("Downloading image..."); moonImage = getImage(getDocumentBase(), "Moon204.gif"); tracker.addImage(moonImage, 0); try { tracker.waitForID(0); context.showStatus(""); } catch (InterruptedException e) { context.showStatus("");} if (tracker.isErrorID(0)) { // If we can't download the image, substitute a white circle. moonImage = createImage(204, 204); Graphics g = moonImage.getGraphics(); g.setColor(Color.black); g.fillRect(0, 0, 204, 204); g.setColor(Color.white); g.fillOval(2, 2, 200, 200); } detPanel = new detailPanel(displayWidth, displayHeight, inputFont, weekdayFont, daycellFont, moonImage, helpPanel); mainPanel.add("Detail", detPanel); } // Start up again if they return. public void start() { if (currentPanel == "Detail") { detPanel.setControls(); detPanel.thread = null; detPanel.thread = new Thread(detPanel); detPanel.thread.start(); } } // Be polite, and stop the animation when the user leaves the page. public void stop() { if (detPanel.thread != null) { detPanel.thread.stop(); detPanel.thread = null; } } public void update(Graphics g) { paint(g); } }