A simple java codes to create a table for making a straight line equation, y = mx + c from two sets of coordinates (x,y). Three classes – equation, point, line.
In any of its form, the equation of a straight lines can be determined as follows :
- Given the slope m and “y intercept” c, the equation of the line is y = mx + c
- Given the slope of the line m and one point P1 = (x1,y1) through which the line passes, we can formulate the equation as : (y-y1)/(x-x1) = m
- This equation is written in a form which makes it clear that the slope calculated between any point (x,y) on the line and the given point P1 is the same. However, the relationship generally gets simplified algebraically to : y = m(x-x1) + y1
- The above form is tidier, but conceals the simple truth behind how the equation was crafted, namely as statement about the slope of the line.
- Given that the line passes through two points P1 = (x1,y1) and P2 = (x2,y2), we first find that the slope of the line is: slope:m = (y2-y1)/(x2-x1)
Below source code has two classes, namely Point and Line classes. These classes are used to create the Line objects constructed from two points of the line. The main class displays the string representing the equation of the line when users enter two points P1 and P2 in a GUI window.
===========================================
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class Equation extends JFrame implements ActionListener
{
JLabel m_labelP1;
JLabel m_labelP2;
JPanel m_panelInputP1;
JPanel m_panelP1;
JTextField m_textfieldP1X;
JTextField m_textfieldP1Y;
JPanel m_panelInputP2;
JPanel m_panelP2;
JTextField m_textfieldP2X;
JTextField m_textfieldP2Y;
JButton m_buttonCompute;
JPanel m_panelInput;
JPanel m_panelResult;
JLabel m_labelResult;
public Equation ()
{
//initialize GUI
setTitle(”Assignment 1 – Straight Line Equation”);
m_labelP1=new JLabel(”Point1 (x1,y1):”);
m_labelP2=new JLabel(”Point2 (x2,y2):”);
m_panelP1=new JPanel(new GridLayout(1,2));//row,column
m_textfieldP1X=new JTextField(”0.0″);
m_textfieldP1Y=new JTextField(”0.0″);
m_panelP1.add(m_textfieldP1X);
m_panelP1.add(m_textfieldP1Y);
m_panelInputP1=new JPanel(new GridLayout(1,2));
m_panelInputP1.add(m_labelP1);
m_panelInputP1.add(m_panelP1);
m_panelP2=new JPanel(new GridLayout(1,2));
m_textfieldP2X=new JTextField(”0.0″);
m_textfieldP2Y=new JTextField(”0.0″);
m_panelP2.add(m_textfieldP2X);
m_panelP2.add(m_textfieldP2Y);
m_panelInputP2=new JPanel(new GridLayout(1,2));
m_panelInputP2.add(m_labelP2);
m_panelInputP2.add(m_panelP2);
m_buttonCompute=new JButton(”
COMPUTE
“);
m_buttonCompute.addActionListener(this);
m_panelInput=new JPanel(new GridLayout(3,1));
m_panelInput.add(m_panelInputP1);
m_panelInput.add(m_panelInputP2);
m_panelInput.add(m_buttonCompute);
m_labelResult=new JLabel(”
Y = mX + C:
“);
m_panelResult=new JPanel();
m_panelResult.add(m_labelResult);
//set arrangement position of the controls
setLayout(new BorderLayout(1,1));
getContentPane().add(m_panelResult,BorderLayout.NORTH);
getContentPane().add(m_panelInput,BorderLayout.CENTER);
//display GUI
setSize(400,200);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==m_buttonCompute){
String strP1X=m_textfieldP1X.getText();
String strP1Y=m_textfieldP1Y.getText();
String strP2X=m_textfieldP2X.getText();
String strP2Y=m_textfieldP2Y.getText();
double dP1X=0.0;double dP1Y=0.0;double dP2X=0.0;double dP2Y=0.0;
try{
dP1X=Double.parseDouble(strP1X);
dP1Y=Double.parseDouble(strP1Y);
dP2X=Double.parseDouble(strP2X);
dP2Y=Double.parseDouble(strP2Y);
}
catch(NumberFormatException ex){
System.out.println(”Input not a double”);
System.exit(1);
}
String strResult=”";
if(!(dP1X==0.0 && dP1Y==0.0 && dP2X==0.0 && dP2Y==0.0)){
System.out.println(”P1 x:”+strP1X+” P1 y:”+strP1Y+” P2 x:”+strP2X+” P2 y:”+strP2Y);
System.out.println(”P1 x:”+dP1X+” P1 y:”+dP1Y+” P2 x:”+dP2X+” P2 y:”+dP2Y);
Point objPoint1=new Point(dP1X,dP1Y);
Point objPoint2=new Point(dP2X,dP2Y);
Line objLine=new Line(objPoint1,objPoint2);
objLine.calculateSlope();
objLine.calculateIntercept();
strResult=”Y = “+objLine.getSlope()+”X + “+objLine.getIntercept();
m_labelResult.setText(”
“+strResult+”
“);
}
else
{
strResult=”Invalid Input”;
m_labelResult.setText(”
“+strResult+”
“);
}
}
}
public static void main(String args[])
{
new Equation ();
}
}
class Point
{
public double x;
public double y;
public Point(double x1, double y1)
{
x=x1;
y=y1;
}
}
class Line
{
private Point p1;
private Point p2;
private double slope;
private double intercept;
public Line(Point pt1, Point pt2)
{
p1=pt1;
p2=pt2;
}
public void calculateSlope()
{
slope=(p2.y-p1.y)/(p2.x-p1.x);
}
public void calculateIntercept()
{
//take p1 as point to calculate intercept
intercept=p1.y-(slope*p1.x);
}
public double getSlope()
{
return slope;
}
public double getIntercept()
{
return intercept;
}
}












Leave Your Response