-
Language
Java using Swing
-
Description
Given three points (x1, y1), (x2, y2), and (x3, y3), check if all the three points fall on one straight line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ThreePointsOnOneStraightLine extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField field_x1, field_x2, field_x3, field_y1, field_y2, field_y3;
private JLabel label_slope1, label_slope2;
private JButton button;
public Algorithm() {
field_x1 = new JTextField(4);
field_x2 = new JTextField(4);
field_x3 = new JTextField(4);
field_y1 = new JTextField(4);
field_y2 = new JTextField(4);
field_y3 = new JTextField(4);
label_slope1 = new JLabel("", JLabel.RIGHT);
label_slope2 = new JLabel("", JLabel.RIGHT);
button = new JButton("Process");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel, subpanel;
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(6, 1));
subpanel.add(new JLabel("Enter the value of x1:"));
subpanel.add(new JLabel("Enter the value of x2:"));
subpanel.add(new JLabel("Enter the value of x3:"));
subpanel.add(new JLabel("Enter the value of y1:"));
subpanel.add(new JLabel("Enter the value of y2:"));
subpanel.add(new JLabel("Enter the value of y3:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(6, 1));
subpanel.add(field_x1);
subpanel.add(field_x2);
subpanel.add(field_x3);
subpanel.add(field_y1);
subpanel.add(field_y2);
subpanel.add(field_y3);
panel.add(subpanel);
pane.add(panel, BorderLayout.NORTH);
panel = new JPanel(new FlowLayout());
panel.add(button);
pane.add(panel);
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(2, 1));
subpanel.add(new JLabel("Value of slope1:"));
subpanel.add(new JLabel("Value of slope2:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(2, 1));
subpanel.add(label_slope1);
subpanel.add(label_slope2);
panel.add(subpanel);
pane.add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
double slope1, slope2, x1, x2, x3;
double y1, y2, y3;
try {
x1 = Double.parseDouble(field_x1.getText());
x2 = Double.parseDouble(field_x2.getText());
x3 = Double.parseDouble(field_x3.getText());
y1 = Double.parseDouble(field_y1.getText());
y2 = Double.parseDouble(field_y2.getText());
y3 = Double.parseDouble(field_y3.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
slope1=(y1-y2)/(x1-x2);
slope2=(y2-y3)/(x2-x3);
if(slope1==slope2&&y1-slope1*x1&&y3-slope2*x3)
javax.swing.JOptionPane.showMessageDialog(this, "The three points fall on one straight line.");
else
javax.swing.JOptionPane.showMessageDialog(this, "The three points do not fall on one straight line.");
label_slope1.setText(String.valueOf(slope1));
label_slope2.setText(String.valueOf(slope2));
pack();
}
public static void main(String[] args) {
new Algorithm().setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
public class ThreePointsOnOneStraightLine extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField field_x1, field_x2, field_x3, field_y1, field_y2, field_y3;
private JLabel label_slope1, label_slope2;
private JButton button;
public Algorithm() {
field_x1 = new JTextField(4);
field_x2 = new JTextField(4);
field_x3 = new JTextField(4);
field_y1 = new JTextField(4);
field_y2 = new JTextField(4);
field_y3 = new JTextField(4);
label_slope1 = new JLabel("", JLabel.RIGHT);
label_slope2 = new JLabel("", JLabel.RIGHT);
button = new JButton("Process");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel, subpanel;
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(6, 1));
subpanel.add(new JLabel("Enter the value of x1:"));
subpanel.add(new JLabel("Enter the value of x2:"));
subpanel.add(new JLabel("Enter the value of x3:"));
subpanel.add(new JLabel("Enter the value of y1:"));
subpanel.add(new JLabel("Enter the value of y2:"));
subpanel.add(new JLabel("Enter the value of y3:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(6, 1));
subpanel.add(field_x1);
subpanel.add(field_x2);
subpanel.add(field_x3);
subpanel.add(field_y1);
subpanel.add(field_y2);
subpanel.add(field_y3);
panel.add(subpanel);
pane.add(panel, BorderLayout.NORTH);
panel = new JPanel(new FlowLayout());
panel.add(button);
pane.add(panel);
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(2, 1));
subpanel.add(new JLabel("Value of slope1:"));
subpanel.add(new JLabel("Value of slope2:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(2, 1));
subpanel.add(label_slope1);
subpanel.add(label_slope2);
panel.add(subpanel);
pane.add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
double slope1, slope2, x1, x2, x3;
double y1, y2, y3;
try {
x1 = Double.parseDouble(field_x1.getText());
x2 = Double.parseDouble(field_x2.getText());
x3 = Double.parseDouble(field_x3.getText());
y1 = Double.parseDouble(field_y1.getText());
y2 = Double.parseDouble(field_y2.getText());
y3 = Double.parseDouble(field_y3.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
slope1=(y1-y2)/(x1-x2);
slope2=(y2-y3)/(x2-x3);
if(slope1==slope2&&y1-slope1*x1&&y3-slope2*x3)
javax.swing.JOptionPane.showMessageDialog(this, "The three points fall on one straight line.");
else
javax.swing.JOptionPane.showMessageDialog(this, "The three points do not fall on one straight line.");
label_slope1.setText(String.valueOf(slope1));
label_slope2.setText(String.valueOf(slope2));
pack();
}
public static void main(String[] args) {
new Algorithm().setVisible(true);
}
}