- Joined
- Aug 2, 2008
- Sythe Gold
- 0
- Messages
- 734
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Triangle extends JFrame implements ActionListener,
Runnable {
Thread go;
JTextField asquared = new JTextField(3);
JLabel plus = new JLabel(" + ");
JTextField bsquared = new JTextField(3);
JLabel equals = new JLabel(" = ");
JTextField csquared = new JTextField(3);
JButton solve = new JButton("Solve");
JButton clear = new JButton("Clear");
ButtonGroup option = new ButtonGroup();
JLabel checkbox = new JLabel("Which part of the triangle are you missing?");
JCheckBox quickpick = new JCheckBox("Leg", false);
JCheckBox personal = new JCheckBox("Hypotenuse", true);
JTextArea an = new JTextArea(8,30);
boolean a = false;
boolean b = false;
boolean c = false;
double valuea;
double valueb;
double valuec;
int selected = 0;
int whichone = 0;
Triangle() {
super("Edward's Pathagorean Therom Solver");
setSize(430, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
BorderLayout bord = new BorderLayout();
content.setLayout(bord);
an.setEditable(false);
an.setLineWrap(true);
JScrollPane textPane = new JScrollPane(an);
csquared.setEnabled(false);
asquared.addActionListener(this);
bsquared.addActionListener(this);
csquared.addActionListener(this);
solve.addActionListener(this);
clear.addActionListener(this);
JPanel centerPanel = new JPanel();
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
bottomPanel.add(checkbox);
bottomPanel.add(quickpick);
bottomPanel.add(personal);
centerPanel.add(an);
topPanel.add(asquared);
topPanel.add(plus);
topPanel.add(bsquared);
topPanel.add(equals);
topPanel.add(csquared);
topPanel.add(solve);
topPanel.add(clear);
asquared.setText("a²");
bsquared.setText("b²");
csquared.setText("c²");
quickpick.addActionListener(this);
personal.addActionListener(this);
option.add(quickpick);
option.add(personal);
content.add(topPanel, BorderLayout.NORTH);
content.add(centerPanel, BorderLayout.CENTER);
content.add(bottomPanel, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if(source == quickpick) {
bsquared.setEnabled(false);
csquared.setEnabled(true);
bsquared.setText("b²");
selected += 1;
}else if(source == personal) {
bsquared.setEnabled(true);
csquared.setEnabled(false);
csquared.setText("c²");
selected -= 1;
}
else if(source == solve) {
quickpick.setEnabled(false);
personal.setEnabled(false);
solve.setEnabled(false);
asquared.setEnabled(false);
bsquared.setEnabled(false);
csquared.setEnabled(false);
if (go == null) {
go = new Thread(this);
go.start();
}
}else if(source == clear){
go = null;
solve.setEnabled(true);
quickpick.setEnabled(true);
personal.setEnabled(true);
asquared.setEnabled(true);
asquared.setText("a²");
bsquared.setText("b²");
csquared.setText("c²");
an.setText(null);
a = false;
b = false;
c = false;
if(selected == 0){
csquared.setEnabled(false);
bsquared.setEnabled(true);
csquared.setText("c²");
}else if(selected == 1) {
bsquared.setEnabled(false);
csquared.setEnabled(true);
}
}
}
public void run() {
if(selected == 0){
double geta = Integer.parseInt(asquared.getText());
double getb = Integer.parseInt(bsquared.getText());
if(geta <= 0 || getb <= 0) {
an.setText("You have entered either a negative number" + "\n" + "or 0 for one of the fields" + "\n" + "press clear and try again");
solve.setEnabled(false);
asquared.setEnabled(false);
bsquared.setEnabled(false);
csquared.setEnabled(false);
}else if(geta > 0 && getb > 0){
double asquareit = geta * geta;
double bsquareit = getb * getb;
double combined = asquareit + bsquareit;
double rooted = Math.sqrt(combined);
an.setText("a² + b² = c² " + "\n" + geta +"² + " + getb + "² = c²" + "\n" + asquareit + " + " + bsquareit + " = c²" +"\n" + combined + " = c²" + " put square root sign over both numbers" +"\n" + rooted + " = c");
}
}
else if(selected == 1){
double geta = Integer.parseInt(asquared.getText());
double getc = Integer.parseInt(csquared.getText());
if(geta <= 0 || getc <= 0) {
an.setText("You have entered either a negative number" + "\n" + "or 0 for one of the fields" + "\n" + "press clear and try again");
solve.setEnabled(false);
asquared.setEnabled(false);
bsquared.setEnabled(false);
csquared.setEnabled(false);
}
else if(geta > 0 && getc > 0){
double asquareit = geta * geta;
double csquareit = getc * getc;
double subtracted = csquareit - asquareit;
double rooted = Math.sqrt(subtracted);
an.setText("a² + b² = c²" +"\n" + geta + "² + b² = " + getc + "²" +"\n" + asquareit + " + b² = " + csquareit + "\n" + csquareit + " - " + asquareit + " = b²" + "\n" + subtracted + " = b²" + "\n" + rooted + " = b");
}
}
}
public static void main(String[] arguments) {
Triangle frame = new Triangle();
}
}











