Coordinates of Two Points using an Applet
Question: Write an applet that enables a user to calculate the distance between two points (x1,y1) and (x2, y2) All numbers and return values should be of type double and the result should give the coordinates of the points.
This is an applet that calculates the distance between two points using method distance to find the two points.
The Program
//import declarations.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Coordinates extends JApplet implements ActionListener {
JLabel x1Label,x2Label,y1Label, y2Label, resultLabel; //initialize variable for the gui
JTextField x1Field, x2Field, y1Field, y2Field, resultField;
JButton getButton;
//set up GUI components
public void init()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
//create numberLabel and attach it to the content pane
x1Label = new JLabel ("Enter x1 Cordinate");
container.add(x1Label);
//create numberField and attach it to the content pane
x1Field = new JTextField(10);
container.add(x1Field);
//create numberLabel and attach it to the content pane
y1Label = new JLabel ("Enter y1 Cordinate");
container.add(y1Label);
//create numberField and attach it to the content pane
y1Field = new JTextField(10);
container.add(y1Field);
//create numberLabel and attach it to the content pane
x2Label = new JLabel ("Enter x2 Cordinate");
container.add(x2Label);
//create numberField and attach it to the content pane
x2Field = new JTextField(10);
container.add(x2Field);
//create numberLabel and attach it to the content pane
y2Label = new JLabel ("Enter y2 Cordinate");
container.add(y2Label);
//create numberField and attach it to the content pane
y2Field = new JTextField(10);
container.add(y2Field);
//create button user clicks to add number
getButton = new JButton("Get Distance");
container.add(getButton);
//register this applet as addButton ActionListener
getButton.addActionListener(this);
//create resultLabel and attach it to the content pane
resultLabel = new JLabel("Distance is");
container.add(resultLabel);
//create resultField and attach it to the content pane
resultField = new JTextField(15);
resultField.setEditable(false);
container.add(resultField);
}
public void actionPerformed (ActionEvent event )
{
double DistanceValue = 0; double x1, x2, y1, y2;
//obtain user's input and convert to long
x1 = Double.parseDouble(x1Field.getText() );
x2 = Double.parseDouble(x2Field.getText() );
y1 = Double.parseDouble(y1Field.getText() );
y2 = Double.parseDouble(y2Field.getText() );
showStatus("Calculating.........");
//Calculate DistanceValue for number input
DistanceValue = distance(x1,x2,y1,y2); //method distance is called here to calculate the distance
//btweeen the two point and return result
//indicate processing complete and display result
showStatus("Done!");
resultField.setText(Double.toString(DistanceValue) );
}
public double distance (double a, double b, double c, double d) //method distance
{
return Math.sqrt( ((d+b)*(d+b)) + ((c+a)*(c+a)) );
}
}
0 comments :
Post a Comment