/* * Created on Feb 2, 2005 */ package towerHw3B.tstarter; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.*; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.BevelBorder; import javax.swing.border.Border; import mvc373Hw3B.ITowerController; import mvc373Hw3B.ITowerModel; import mvc373Hw3B.ITowerStarter; import mvc373Hw3B.ITowerView; import towerHw3B.tcontroller.TowerController; import towerHw3B.tmodel.TowerModel; import towerHw3B.tview.TowerView; /** This class provides a way to start the Homework #3 Towers of Hanoi * program. */ public class TowerStarter implements ITowerStarter { public static final String AUTHOR = "Martin D."; public static final String SHORT_DESCRIPTION = "NEW and IMPROVED starter! "; public static final String LONG_DESCRIPTION = "" + "Creates a window with the View on top, " + "then the Controller, " + "
" + "followed by a panel with a 'REFRESH' button " + "interspersed information about the modules"; /** Create the components and the Window to hold them, then * start the game. * @param args */ public static void main(String[] args) { //new TowerStarter(); ITowerModel tmodel = new TowerModel(); ITowerView tview = new TowerView(tmodel); ITowerController tcontroller = new TowerController(tmodel, tview); TowerFrame frame = new TowerFrame((JPanel) tcontroller, (JPanel) tview, tmodel); tcontroller.startUp(); //Ready to go } public TowerStarter() { } private static class TowerFrame extends JFrame { public static final int DEFAULT_WIDTH = 700; public static final int cpanelW = DEFAULT_WIDTH; public static final int cpanelH = 200; public static final int vpanelW = cpanelW; public static final int vpanelH = cpanelH; public static final int starterPanelH = 50; public static final int infoPanelW = DEFAULT_WIDTH; public static final int infoPanelH = 50; private final JPanel controlPanel; private final JPanel viewPanel; public static final String DEFAULT_TITLE = "Towers of Hanoi Game" + " Version B with Mass Move and Auto Stack!"; TowerFrame(JPanel controlPanel, JPanel viewPanel, ITowerModel tmodel) { assert controlPanel != null; assert viewPanel != null; assert tmodel != null; try { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } catch (Error e) { // appparently a problem running under MixMatcher } this.setTitle(DEFAULT_TITLE); Container contents = this.getContentPane(); contents.setLayout(new BoxLayout(contents, BoxLayout.Y_AXIS)); contents.setBackground(Color.BLACK); this.controlPanel = controlPanel; this.controlPanel.setBackground(new Color(240,240,190)); //light yellow this.controlPanel.setPreferredSize(new Dimension(cpanelW, cpanelH)); this.viewPanel = viewPanel; this.viewPanel.setBackground(new Color(200,255,200)); //light green this.viewPanel.setPreferredSize(new Dimension(vpanelW, vpanelH)); //Set up a little starter control panel JPanel starterPanel = new JPanel(); starterPanel.setBackground(Color.WHITE); // //starterPanel.setPreferredSize(new Dimension(vpanelW, starterPanelH)); JButton manualRefresh = new JButton ("Manual Refresh"); manualRefresh.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { TowerFrame.this.viewPanel.repaint(); TowerFrame.this.controlPanel.repaint(); } }); starterPanel.add(manualRefresh); String[] info = getASdLd(viewPanel); JComponent viewInfo = new LabelPanel(DEFAULT_WIDTH, new String[] { "View " + "by " + info[0], info[1], info[2]}); info = getASdLd(controlPanel); JComponent trollerInfo = new LabelPanel(DEFAULT_WIDTH, new String[] { "Controller " + "by " + info[0], info[1], info[2]}); info = getASdLd(tmodel); JComponent modelInfo = new LabelPanel(DEFAULT_WIDTH, new String[] { "Model " + "by " + info[0], info[1], info[2]}); /* info = getASdLd(TowerStarter.this); JComponent starterInfo = new LabelPanel(DEFAULT_WIDTH, new String[] { "Starter " + "by " + info[0], info[1], info[2]}); */ contents.add(viewInfo); contents.add(new JScrollPane(viewPanel)); contents.add(new JScrollPane(controlPanel)); contents.add(trollerInfo); contents.add(modelInfo); contents.add(starterPanel); //contents.add(starterInfo); this.pack(); this.setVisible(true); } JPanel getControlPanel() { assert this.controlPanel != null; return this.controlPanel; } JPanel getViewPanel() { assert this.viewPanel != null; return this.viewPanel; } public String[] getASdLd(Object obj) { return new String[] {getStringFieldValue(obj, "AUTHOR"), getStringFieldValue(obj, "SHORT_DESCRIPTION"), getStringFieldValue(obj, "LONG_DESCRIPTION")}; } public String getStringFieldValue(Object obj, String fieldname) { assert obj != null; assert fieldname != null; Class oclass = obj.getClass(); String fvalue = ""; try { Field theField = oclass.getField(fieldname); Object aobject = theField.get(obj); if (aobject != null) { fvalue = (String) aobject; } } catch (NoSuchFieldException e) { fvalue = "[no " + fieldname + " field]"; } catch (IllegalAccessException e) { fvalue = "[no access to " + fieldname + " field]"; } return fvalue; } } } /** A panel with some labels side by side. * */ class LabelPanel extends JPanel { /** Add a label for text text string supplied. * * @param labelText a non-array of strings, each of which may be empty * but not null. Each string, empty or not, is displayed in a label. */ public LabelPanel(int maxWidth, String[] labelText) { assert labelText != null; this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); Border labelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED); this.setBackground(Color.BLUE); int prefHeight = 0; int strutWidth = 2; for (int s = 0; s < labelText.length; s++) { this.add(Box.createHorizontalStrut(strutWidth)); String text = labelText[s]; assert text!= null; JLabel thisLabel = new JLabel(text); thisLabel.setToolTipText(text); thisLabel.setBackground(Color.WHITE); thisLabel.setOpaque(true); thisLabel.setBorder(labelBorder); this.add(thisLabel); prefHeight = Math.max(prefHeight, thisLabel.getPreferredSize().height); prefHeight = Math.max(prefHeight, this.getPreferredSize().height); this.add(Box.createHorizontalStrut(strutWidth)); } //set preferred to dimension to the height of the last label. int prefWidth = Math.min(maxWidth, this.getPreferredSize().width); this.setPreferredSize(new Dimension(prefWidth, prefHeight+2)); } }