// --------------------------------------------------------------- // Viewer // --------------------------------------------------------------- import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Viewer extends JFrame implements ActionListener, UpdateErrorObserver, AngleChangeObserver { private JTextField mExpression = null; private DrawPanel mDrawPanel = null; private AngleControl mXAngleControl = null; private AngleControl mYAngleControl = null; private AngleControl mZAngleControl = null; private static final int X_ANGLE_DEFAULT = 140; private static final int Y_ANGLE_DEFAULT = 160; private static final int Z_ANGLE_DEFAULT = 70; private static final int X_DIVISIONS = 20; private static final int Y_DIVISIONS = 20; private static final double VALUE = 100.0; private static final double X_START = -VALUE; private static final double Y_START = -VALUE; private static final double X_END = VALUE; private static final double Y_END = VALUE; public Viewer() { addWindowListener(new AppCloser()); setSize(640, 480); JPanel contentPane = (JPanel)getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add("South", makeBottomPanel()); contentPane.add("Center", mDrawPanel = new DrawPanel()); pack(); mExpression.requestFocusInWindow(); setVisible(true); } private JPanel makeBottomPanel() { // Create a panel containing the expression entry text field // and its label JPanel p1 = new JPanel(new FlowLayout()); mExpression = new JTextField(40); mExpression.addActionListener(this); p1.add(new JLabel("Enter a function of x and y: ")); p1.add(mExpression); p1.setBorder(BorderFactory.createEtchedBorder()); // Create a panel displaying the angle controls JPanel p2 = new JPanel(new GridLayout(3, 1)); mXAngleControl = new AngleControl(X_ANGLE_DEFAULT, "xAxis", this); mYAngleControl = new AngleControl(Y_ANGLE_DEFAULT, "yAxis", this); mZAngleControl = new AngleControl(Z_ANGLE_DEFAULT, "zAxis", this); p2.add(mXAngleControl); p2.add(mYAngleControl); p2.add(mZAngleControl); p2.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Set Viewing Angles" ) ); // Combine the two panels JPanel p3 = new JPanel(new BorderLayout()); p3.add("South", p1); p3.add("North", p2); return(p3); } private void draw() { String theString = mExpression.getText().trim(); if(theString.equals("")) return; Node n = null; try { n = Node.getNode(theString); } catch(Throwable t) { JOptionPane.showMessageDialog( this, t.getMessage(), "Parse Error", JOptionPane.WARNING_MESSAGE ); return; } mDrawPanel.update( mXAngleControl.getAngle(), mYAngleControl.getAngle(), mZAngleControl.getAngle(), X_DIVISIONS, Y_DIVISIONS, X_START, Y_START, X_END, Y_END, n, this ); } public void angleChanged(int newAngle) { draw(); } public void actionPerformed(ActionEvent e) { draw(); } public void updateErrorOccurred(String s) { JOptionPane.showMessageDialog( this, s, "Error Occurred", JOptionPane.WARNING_MESSAGE ); } public static void main(String args[]) { new Viewer(); } }