CS 201 - Computer Science I - Spring 2003
Examples


Loyola College > Department of Computer Science > CS 201 > Examples > Line Calculator

source code

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Calculates the equation of a line between two points.
 *
 * @author Jim Glenn
 * @version 0.1 3/19/2003
 */

public class LineCalculator extends Applet implements ActionListener
{
    private TextField p1xInput;
    private TextField p1yInput;
    private TextField p2xInput;
    private TextField p2yInput;
    private TextField outputField;

    /**
     * Sets up the user interface for this applet.
     */

    public void init()
    {
	// make a panel and components for the first point input

	Panel p1Panel;
	p1Panel = new Panel();

	p1Panel.add(new Label("Point 1"));
	p1Panel.add(new Label("x:"));
	p1xInput = new TextField(3);
	p1Panel.add(p1xInput);
	p1Panel.add(new Label("y:"));
	p1yInput = new TextField(3);
	p1Panel.add(p1yInput);

	// make a panel and components for the second point input

	Panel p2Panel;
	p2Panel = new Panel();

	p2Panel.add(new Label("Point 2"));
	p2Panel.add(new Label("x:"));
	p2xInput = new TextField(3);
	p2Panel.add(p2xInput);
	p2Panel.add(new Label("y:"));
	p2yInput = new TextField(3);
	p2Panel.add(p2yInput);

	// make a panel for the button and make the button

	Panel buttonPanel;
	buttonPanel = new Panel();
	Button calcButton;
	calcButton = new Button("Calculate");
	calcButton.addActionListener(this);
	buttonPanel.add(calcButton);

	// make a panel for the output

	Panel outputPanel;
	outputPanel = new Panel(new BorderLayout());
	outputPanel.add(new Label("Equation:"), BorderLayout.WEST);
	outputField = new TextField();
	outputField.setEditable(false);
	outputPanel.add(outputField, BorderLayout.CENTER);

	// fit the four panels together

	Panel topPanel;
	topPanel = new Panel(new BorderLayout());
	topPanel.add(p1Panel, BorderLayout.NORTH);
	topPanel.add(p2Panel, BorderLayout.SOUTH);

	Panel bottomPanel;
	bottomPanel = new Panel(new BorderLayout());
	bottomPanel.add(buttonPanel, BorderLayout.NORTH);
	bottomPanel.add(outputPanel, BorderLayout.SOUTH);

	Panel bigPanel;
	bigPanel = new Panel(new BorderLayout());
	bigPanel.add(topPanel, BorderLayout.NORTH);
	bigPanel.add(bottomPanel, BorderLayout.SOUTH);

	this.setLayout(new BorderLayout());
	this.add(bigPanel, BorderLayout.NORTH);
	this.add(new Label("Line Calculator", Label.CENTER), BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e)
    {
 	 	// read the four values entered by the user
		// we use doubles here to allow the user to
		// enter non-integer coordinates
	 
	 	double x1, x2, y1, y2;
		x1 = Double.parseDouble(p1xInput.getText());
		y1 = Double.parseDouble(p1yInput.getText());
		x2 = Double.parseDouble(p2xInput.getText());
		y2 = Double.parseDouble(p2yInput.getText());
		
		// there are three cases:
		// 1) everything works out normally
		// 2) the line is vertical
		// 3) the points are the same and so many different lines work
		// the if/else if/else conditions distinguish between the 3 cases
		
		if (x1 != x2)
			{
				// x coordinates were different; use the normal formula for a line
			
				double m, b;
				m = (y2 - y1) / (x2 - x1);
				b = y1 - m * x1;
		
				outputField.setText("y = " + m + "x + " + b);
			}
		else if (y1 != y2)
			{
				// x coodinates were the same but y's were different;
				// line is vertical
			
				outputField.setText("x = " + x1);
			}
		else
			{
				// the user entered the same point twice
			
				outputField.setText("Please enter two different points");
			}
   }
}