/* infoField.java Paul Carlisle Displays the information generated by the Moon calculations. Distance, rise, set for Sun and Moon. Only redraws items which are different from those already set; this cuts down on screen flicker in some browsers. */ import java.awt.*; import java.lang.*; import JDate; import textHelpPanel; public class infoField extends Panel { public static int NONE = 0; public static int HELP_REQUESTED = 1; private textHelpPanel help; private int messageID = NONE; private boolean gotMessage = false; private Label date, time; private Label moonDistLabel, moonDist; private Label moonRiseLabel, moonRise; private Label moonSetLabel, moonSet; private Label sunDistLabel, sunDist; private Label sunRiseLabel, sunRise; private Label sunSetLabel, sunSet; private Panel holdingPanel; private GridBagLayout mainLayout; private GridBagConstraints mainConstraints; private GridBagLayout myLayout; public infoField(Font font, textHelpPanel helpPanel) { help = helpPanel; myLayout = new GridBagLayout(); setLayout(myLayout); setFont(font); setBackground(Color.gray); // If you've been following along with the rest of the // modules, this sort of layout code should look pretty // familiar by now... holdingPanel = new Panel(); mainLayout = new GridBagLayout(); mainConstraints = new GridBagConstraints(); holdingPanel.setLayout(mainLayout); holdingPanel.setFont(font); holdingPanel.setBackground(Color.gray); date = new Label(" ", Label.RIGHT); time = new Label(" ", Label.LEFT); moonDistLabel = new Label("Moon's Distance : ", Label.RIGHT); moonDist = new Label(" ", Label.LEFT); moonRiseLabel = new Label("Moonrise : ", Label.RIGHT); moonRise = new Label(" ", Label.LEFT); moonSetLabel = new Label("Moonset : ", Label.RIGHT); moonSet = new Label(" ", Label.LEFT); sunDistLabel = new Label("Sun's Distance : ", Label.RIGHT); sunDist = new Label(" ", Label.LEFT); sunRiseLabel = new Label("Sunrise : ", Label.RIGHT); sunRise = new Label(" ", Label.LEFT); sunSetLabel = new Label("Sunset : ", Label.RIGHT); sunSet = new Label(" ", Label.LEFT); mainConstraints.fill = GridBagConstraints.NONE; mainConstraints.insets = new Insets(0, 0, 0, 0); mainConstraints.gridy = 0; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(date, mainConstraints); holdingPanel.add(date); mainConstraints.gridy = 0; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(time, mainConstraints); holdingPanel.add(time); mainConstraints.gridy = 1; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(moonDistLabel, mainConstraints); holdingPanel.add(moonDistLabel); mainConstraints.gridy = 1; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(moonDist, mainConstraints); holdingPanel.add(moonDist); mainConstraints.gridy = 2; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(moonRiseLabel, mainConstraints); holdingPanel.add(moonRiseLabel); mainConstraints.gridy = 2; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(moonRise, mainConstraints); holdingPanel.add(moonRise); mainConstraints.gridy = 3; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(moonSetLabel, mainConstraints); holdingPanel.add(moonSetLabel); mainConstraints.gridy = 3; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(moonSet, mainConstraints); holdingPanel.add(moonSet); mainConstraints.gridy = 4; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(sunDistLabel, mainConstraints); holdingPanel.add(sunDistLabel); mainConstraints.gridy = 4; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(sunDist, mainConstraints); holdingPanel.add(sunDist); mainConstraints.gridy = 5; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(sunRiseLabel, mainConstraints); holdingPanel.add(sunRiseLabel); mainConstraints.gridy = 5; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(sunRise, mainConstraints); holdingPanel.add(sunRise); mainConstraints.gridy = 6; mainConstraints.gridx = 0; mainConstraints.anchor = GridBagConstraints.EAST; mainLayout.setConstraints(sunSetLabel, mainConstraints); holdingPanel.add(sunSetLabel); mainConstraints.gridy = 6; mainConstraints.gridx = 1; mainConstraints.anchor = GridBagConstraints.WEST; mainLayout.setConstraints(sunSet, mainConstraints); holdingPanel.add(sunSet); setLayout(myLayout); mainConstraints.gridy = 0; mainConstraints.gridx = 0; mainConstraints.fill = GridBagConstraints.NONE; mainConstraints.anchor = GridBagConstraints.CENTER; mainConstraints.insets = new Insets(1, 1, 1, 1); myLayout.setConstraints(holdingPanel, mainConstraints); add(holdingPanel); } public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } public int getMessageID() { return messageID; } public boolean handleEvent(Event e) { if (e.id != Event.MOUSE_DOWN) return true; boolean result = false; messageID = HELP_REQUESTED; gotMessage = true; help.getHelp("Info"); return result; } public void paint(Graphics g) { Dimension d = size(); g.setColor(Color.black); g.drawRect(0, 0, d.width - 1, d.height - 1); g.dispose(); } // Redraw items which have changed. public void reset(String dt, String tm, double moonD, String mr, String ms, double sunD, String sr, String ss) { String md = ' ' + Integer.toString((int)moonD) + " km"; String sd = ' ' + Integer.toString((int)sunD) + " km"; // Update text fields, if they changed. if (date.getText() != dt) date.setText(dt + " : "); if (time.getText() != tm) time.setText(tm); if (moonDist.getText() != md) moonDist.setText(md); if (moonRise.getText() != mr) moonRise.setText(mr); if (moonSet.getText() != ms) moonSet.setText(ms); if (sunDist.getText() != sd) sunDist.setText(sd); if (sunRise.getText() != sr) sunRise.setText(sr); if (sunSet.getText() != ss) sunSet.setText(ss); } public void update(Graphics g) { repaint(); } }