import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import javax.swing.border.*; public class CatenaryController extends JFrame { private JLabel weightLabel, yMinLabel, poleXLabel, poleYLabel, tensionLabel; private JTextField weightField, yMinField, poleXField, poleYField; private JButton calcButton, quitButton; private JPanel inputHolder; private JPanel fieldHolder; private JPanel buttonHolder; private Container contentPane; private ScrollPane scrollPane; private JPanel canvas ; private CatenaryView drawArea = null; private CatenaryModel analysis = null; public static final int X = 900 ; // Size of canvas (x-dimension) public static final int Y = 600 ; // Size of canvas (y-dimension) public CatenaryController() { //Get outer pane contentPane = getContentPane() ; setSize( X, Y + 120 ) ; // '120' for button holder setTitle( "Catenary Curve" ) ; //Create scrollpane scrollPane = new ScrollPane(); contentPane.add(scrollPane); //Create canvas to draw 2D graph canvas = new JPanel(); canvas.setLayout(new BorderLayout()); scrollPane.add(canvas); //Create input (input field/label/button) holder inputHolder = new JPanel(); inputHolder.setLayout(new BorderLayout()); canvas.add(inputHolder, BorderLayout.SOUTH); inputHolder.setBorder(new EtchedBorder()); inputHolder.setPreferredSize(new Dimension(900, 70)); //Create field (input field and labels) holder fieldHolder = new JPanel(); inputHolder.add(fieldHolder, BorderLayout.NORTH); // Text field for cable weight w weightLabel = new JLabel( "Cable weight (N/m) =" ) ; fieldHolder.add( weightLabel ) ; weightField = new JTextField( 10 ) ; weightField.setText("10"); fieldHolder.add( weightField ) ; // Text field for min elevation ymin yMinLabel = new JLabel( " Min height (m) =" ) ; fieldHolder.add( yMinLabel ) ; yMinField = new JTextField( 10 ) ; yMinField.setText("5"); fieldHolder.add( yMinField ) ; // Text field for pole x poleXLabel = new JLabel( " Pole x =" ) ; fieldHolder.add( poleXLabel ) ; poleXField = new JTextField( 10 ) ; poleXField.setText("50"); fieldHolder.add( poleXField ) ; // Text field for pole y poleYLabel = new JLabel( " Pole y =" ) ; fieldHolder.add( poleYLabel ) ; poleYField = new JTextField( 10 ) ; poleYField.setText("15"); fieldHolder.add( poleYField ) ; // Create button holder buttonHolder = new JPanel() ; inputHolder.add( buttonHolder, BorderLayout.SOUTH) ; // Calculate button calcButton = new JButton( "Calculate" ) ; calcButton.setPreferredSize(new Dimension(120, 27)); buttonHolder.add( calcButton ) ; calcButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // Input data String input; double w = 0; double yMin = 0; double xPole = 0; double yPole = 0; try{ input = weightField.getText() ; w = Double.parseDouble( input ) ; input = yMinField.getText() ; yMin = Double.parseDouble( input ) ; input = poleXField.getText() ; xPole = Double.parseDouble( input ) ; input = poleYField.getText() ; yPole = Double.parseDouble( input ) ; //Check input validity if(w <= 0 || xPole <= 0 || yPole <= 0){ JOptionPane.showMessageDialog(contentPane, "Weight, Pole X and Pole Y must be positive!"); return; } else if(yMin >= yPole){ JOptionPane.showMessageDialog(contentPane, "Pole Y must be greater than minimum height!"); return; } } catch(Exception ex){ JOptionPane.showMessageDialog(contentPane, "Invalid input!"); return; } ////Create analysis and drawArea objects //analysis = new CatenaryModel(w, yMin, xPole, yPole) ; //drawArea = new CatenaryView( analysis ) ; // Improvements to the above solution // Don't create new CatenaryModel object each time; // Use setXXX() methods (write them) on one object // Don't need a new CatenaryView object each time either // Just create one in this constructor the first time if(analysis == null) analysis = new CatenaryModel(w, yMin, xPole, yPole) ; else{ analysis.setW(w); analysis.setYMin(yMin); analysis.setXPole(xPole); analysis.setYPole(yPole); analysis.setTension(); } if(drawArea == null) drawArea = new CatenaryView( analysis ) ; else drawArea.setCatenaryModel(analysis); drawArea.setSize( X, Y ) ; canvas.add( drawArea, BorderLayout.NORTH ) ; drawArea.repaint() ; } } ) ; // Quit button quitButton = new JButton( "Quit" ) ; quitButton.setPreferredSize(new java.awt.Dimension(120, 27)); buttonHolder.add( quitButton ) ; quitButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { System.exit( 0 ) ; } } ) ; } public static void main(String[] args) { CatenaryController application = new CatenaryController() ; application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; application.setResizable( false ) ; application.setVisible( true ) ; } }