import java.awt.*; import java.net.*; import java.applet.Applet; //import jahuwaldt.gui.*; /** * Applet to calculate the equation of a line from two input points. This * is the 1st Applet I've ever written. * *
Modified by: Joseph A. Huwaldt
* * @author Joseph A. Huwaldt Date: November 22, 1997 * @version November 26, 2003 **/ public class LineEquation extends Applet { static final short fWidth = 290; // Application frame width & height. static final short fHeight = 200; // Define class variables here. Label yeqlabel, xeqlabel; // Y equation and X equation output labels. ParamInput x1pi, y1pi, x2pi, y2pi; // Input panels for this program. //----------------------------------------------------------------------------------- /** * This is the "main" routine that is called when an application first * starts up. This routine instantiates the applet when using this * object as an application. If you are using this object as an applet * then this routine is never called. * * @param args: The arguments passed to the main routine from the command line. **/ public static void main (String args[]) { Frame frame = new Frame("Linear Equation Calculator"); frame.setSize(fWidth, fHeight); frame.setLocation(20,15); LineEquation applet1 = new LineEquation(); frame.add("Center", applet1); applet1.init(); applet1.start(); frame.show(); } // end main() /** * Reads the inputs from the TextFields and calculates the * new line equations. These equations are then output in * the x & y equation labels. **/ void CalcLineEq() { float x1,y1, x2,y2; float m,b; // Line equation parameters String aString; // First read in the input values. try { x1 = x1pi.getInput(); y1 = y1pi.getInput(); x2 = x2pi.getInput(); y2 = y2pi.getInput(); // Check to see if any inputs are "blank". if (Float.isNaN(x1) || Float.isNaN(y1) || Float.isNaN(x2) || Float.isNaN(y2) ) throw new NumberFormatException(); } catch (NumberFormatException e) { yeqlabel.setText("Error: All inputs must be floating point numbers!"); xeqlabel.setText(""); return; } // Check for degenerative case. if (x2 - x1 == 0f) { yeqlabel.setText("A vertical line!"); aString = new String(" x = " + x1); xeqlabel.setText(aString); } else { // Calculate slope m = (y2 - y1)/(x2 - x1); // Calculate axis intercept. b = y1 - m*x1; // Output the results. if (m == 0f) { aString = new String(" y = " + y1); yeqlabel.setText(aString); xeqlabel.setText(" A horizontal line!"); } else { aString = new String(" y = (" + m + ")x "); if (b > 0f) aString += "+ " + b; else aString += " " + b; yeqlabel.setText(aString); b *= -1; aString = new String(" x = (" + 1f/m + ")y "); if (b/m > 0f) aString += "+ " + b/m; else aString += " " + b/m; xeqlabel.setText( aString ); } } } // end CalcLineEq() /** * Fills in the GridBagConstraints record for a given component with input items. * * @param gx,gy Grid (cell) indicies for this compenent in the x and y dir * (1,3 == 2nd column, 4th row). * @param gw,gy The number of cells this component spans. * @param wx,wy Proportional width/height of this grid cell compared to others. **/ void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) { gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; } /** * We are adding an inset to our Applet panel. This is called by the * container to determine our insets. * * @return An Inset object is returned **/ public Insets getInsets() { return new Insets(10,5, 0,25); } /** * Initialization that occures when applet is first loaded. **/ public void init() { GridBagConstraints constraints = new GridBagConstraints(); GridBagLayout gridbag = new GridBagLayout(); setLayout(gridbag); try { // X1 parameter buildConstraints(constraints, 0,0, 1,1, 50,15); constraints.fill = GridBagConstraints.BOTH; // How the component should fill cell. constraints.anchor = GridBagConstraints.CENTER; // Justification in cell x1pi = new ParamInput( new Param("x1") ); gridbag.setConstraints(x1pi, constraints); add(x1pi); // Y1 parameter buildConstraints(constraints, 1,0, 1,1, 50,15); constraints.fill = GridBagConstraints.BOTH; // How the component should fill cell. constraints.anchor = GridBagConstraints.CENTER; // Justification in cell y1pi = new ParamInput( new Param("y1") ); gridbag.setConstraints(y1pi, constraints); add(y1pi); // X2 parameter buildConstraints(constraints, 0,1, 1,1, 50,15); constraints.fill = GridBagConstraints.BOTH; // How the component should fill cell. constraints.anchor = GridBagConstraints.CENTER; // Justification in cell x2pi = new ParamInput( new Param("x2") ); gridbag.setConstraints(x2pi, constraints); add(x2pi); // Y2 parameter buildConstraints(constraints, 1,1, 1,1, 50,15); constraints.fill = GridBagConstraints.BOTH; // How the component should fill cell. constraints.anchor = GridBagConstraints.CENTER; // Justification in cell y2pi = new ParamInput( new Param("y2") ); gridbag.setConstraints(y2pi, constraints); add(y2pi); // Calculate Button buildConstraints(constraints, 0,2, 2,1, 90,30); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; Button calcb = new Button("Calculate"); gridbag.setConstraints(calcb, constraints); add(calcb); // Y Equation output label. buildConstraints(constraints, 0,3, 2,1, 100,20); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; yeqlabel = new Label(" Enter two points, the equation for the line "); gridbag.setConstraints(yeqlabel, constraints); add(yeqlabel); // X Equation output label. buildConstraints(constraints, 0,4, 2,1, 100,20); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; xeqlabel = new Label(" will be calculated. "); gridbag.setConstraints(xeqlabel, constraints); add(xeqlabel); } catch (Exception e) { System.out.println("An Error occured: " + e.getMessage() ); e.printStackTrace(); } } // end init() /** * Action events to our applet are handled here. * * @param evt Object containing the action event that occured. * @param arg Object containing any arguments. **/ public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { CalcLineEq(); // Handle calc button action events. return true; } return(false); } } // end class LineEquation