Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class PointTestBeale2
  7. {
  8. public static void main(String[] args)
  9. {
  10. GUI window = new GUI();
  11. window.setVisible(true);
  12. }
  13.  
  14. static class GUI extends JFrame
  15. {
  16. public GUI() {
  17. JFrame frame = new JFrame();
  18. JPanel content = new JPanel(new GridLayout(0,1));
  19.  
  20. content.add(new JLabel("<html>Enter the X, Y coordinates of Line 1 and Line 2 <br>to calculate lengths and intersect points.</html>");
  21.  
  22. JPanel linespanel = new JPanel(new GridLayout(1,2));
  23.  
  24. line1.add(new JLabel("Line 1");
  25. line1.add(new JLabel("Line 2");
  26.  
  27. content.add(linespanel);
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. setContentPane(content);
  35. pack();
  36.  
  37. setTitle("PointTestBeale");
  38. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. setLocationRelativeTo(null);
  40. }
  41. }
  42.  
  43. class Calculate implements ActionListener
  44. {
  45. public void actionPerformed(ActionEvent submit)
  46. {
  47.  
  48. double $d1, $d2;
  49.  
  50. try
  51. {
  52. // Checking that input was type double
  53. Point line1p1 = new Point();
  54. Point line1p2 = new Point();
  55. Point line2p1 = new Point();
  56. Point line2p1 = new Point();
  57.  
  58. line1p1.set(Double.parseDouble(jtf1X1.getText()),Double.parseDouble(jtf1Y1.getText()));
  59. line1p2.set(Double.parseDouble(jtf1X2.getText()),Double.parseDouble(jtf1Y2.getText()));
  60. line2p1.set(Double.parseDouble(jtf2X1.getText()),Double.parseDouble(jtf2Y1.getText()));
  61. line2p2.set(Double.parseDouble(jtf2X2.getText()),Double.parseDouble(jtf2Y2.getText()));
  62.  
  63. // if the code gets to here, it was recognizable as a double
  64. d1 = getLength(line1p1.getX, line1p1.getY, line1p2.getX, line1p2.getY);
  65. d2 = getLength(line2p1.getX, line2p1.getY, line2p2.getX, line2p2.getY);
  66. Point.getIntersect(line1p1.getX, line1p1.getY, line1p2.getX, line1p2.getY, line2p1.getX, line2p1.getY, line2p2.getX, line2p2.getY);
  67.  
  68. if (d1 == 0 || d2 == 0)
  69. {
  70. JOptionPane.showMessageDialog(null, "Your coordinates suggest a line of zero length. Please recheck.");
  71. }
  72. else
  73. {
  74. JOptionPane.showMessageDialog(null, Point.toString(d1, d2);
  75. }
  76. catch
  77. {
  78. JOptionPane.showMessageDialog(null, "You input improper values. Please try again.");
  79. }
  80. }
  81.  
  82. public double getLength(double x1, double y1, double x2, double y2)
  83. {
  84. return Math.sqrt((Math.pow(x1 - x2, 2)) + (Math.pow(y1 - y2, 2)));
  85. }
  86.  
  87. public void getIntersect(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1, double l2x2, double l2y2)
  88. {
  89. double a1, a2, b1, b2, c1, c2, xint, yint;
  90. a1 = l1y2 - l1y1;
  91. b1 = l1x1 - l1x2;
  92. c1 = (l1y1 * l1x2) - (l1y2 * l1x1);
  93.  
  94. a2 = l2y2 - l2y1;
  95. b2 = l2x1 - l2x2;
  96. c2 = (l2y1 * l2x2) - (l2y2 * l2x1);
  97.  
  98. Point intersect = new Point();
  99.  
  100. if (((a1 * b2) - (a2 * b1)) == 0)
  101. {
  102. intersect.set(null, null);
  103. }
  104. else
  105. {
  106. intersect.setX = ((b1 * c2) - (b2 * c1)) / ((a1 * b2) - (a2 * b1));
  107. intersect.setY = ((c1 * a2) - (c2 * a1)) / ((a1 * b2) - (a2 * b1));
  108. }
  109. }
  110.  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement