Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class PointTestBeale2
- {
- public static void main(String[] args)
- {
- GUI window = new GUI();
- window.setVisible(true);
- }
- static class GUI extends JFrame
- {
- public GUI() {
- JFrame frame = new JFrame();
- JPanel content = new JPanel(new GridLayout(0,1));
- content.add(new JLabel("<html>Enter the X, Y coordinates of Line 1 and Line 2 <br>to calculate lengths and intersect points.</html>");
- JPanel linespanel = new JPanel(new GridLayout(1,2));
- line1.add(new JLabel("Line 1");
- line1.add(new JLabel("Line 2");
- content.add(linespanel);
- setContentPane(content);
- pack();
- setTitle("PointTestBeale");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- }
- }
- class Calculate implements ActionListener
- {
- public void actionPerformed(ActionEvent submit)
- {
- double $d1, $d2;
- try
- {
- // Checking that input was type double
- Point line1p1 = new Point();
- Point line1p2 = new Point();
- Point line2p1 = new Point();
- Point line2p1 = new Point();
- line1p1.set(Double.parseDouble(jtf1X1.getText()),Double.parseDouble(jtf1Y1.getText()));
- line1p2.set(Double.parseDouble(jtf1X2.getText()),Double.parseDouble(jtf1Y2.getText()));
- line2p1.set(Double.parseDouble(jtf2X1.getText()),Double.parseDouble(jtf2Y1.getText()));
- line2p2.set(Double.parseDouble(jtf2X2.getText()),Double.parseDouble(jtf2Y2.getText()));
- // if the code gets to here, it was recognizable as a double
- d1 = getLength(line1p1.getX, line1p1.getY, line1p2.getX, line1p2.getY);
- d2 = getLength(line2p1.getX, line2p1.getY, line2p2.getX, line2p2.getY);
- Point.getIntersect(line1p1.getX, line1p1.getY, line1p2.getX, line1p2.getY, line2p1.getX, line2p1.getY, line2p2.getX, line2p2.getY);
- if (d1 == 0 || d2 == 0)
- {
- JOptionPane.showMessageDialog(null, "Your coordinates suggest a line of zero length. Please recheck.");
- }
- else
- {
- JOptionPane.showMessageDialog(null, Point.toString(d1, d2);
- }
- catch
- {
- JOptionPane.showMessageDialog(null, "You input improper values. Please try again.");
- }
- }
- public double getLength(double x1, double y1, double x2, double y2)
- {
- return Math.sqrt((Math.pow(x1 - x2, 2)) + (Math.pow(y1 - y2, 2)));
- }
- public void getIntersect(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1, double l2x2, double l2y2)
- {
- double a1, a2, b1, b2, c1, c2, xint, yint;
- a1 = l1y2 - l1y1;
- b1 = l1x1 - l1x2;
- c1 = (l1y1 * l1x2) - (l1y2 * l1x1);
- a2 = l2y2 - l2y1;
- b2 = l2x1 - l2x2;
- c2 = (l2y1 * l2x2) - (l2y2 * l2x1);
- Point intersect = new Point();
- if (((a1 * b2) - (a2 * b1)) == 0)
- {
- intersect.set(null, null);
- }
- else
- {
- intersect.setX = ((b1 * c2) - (b2 * c1)) / ((a1 * b2) - (a2 * b1));
- intersect.setY = ((c1 * a2) - (c2 * a1)) / ((a1 * b2) - (a2 * b1));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement